From 6fc3f55d26748c277ce5f90d2d1cf89ed777a46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be> Date: Sun, 12 Sep 2010 11:26:22 +0200 Subject: Create avatar cache dir lazily. diff --git a/Swiften/Avatars/AvatarFileStorage.cpp b/Swiften/Avatars/AvatarFileStorage.cpp index b6630e9..f76adee 100644 --- a/Swiften/Avatars/AvatarFileStorage.cpp +++ b/Swiften/Avatars/AvatarFileStorage.cpp @@ -12,12 +12,6 @@ namespace Swift { AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& path) : path_(path) { - try { - boost::filesystem::create_directories(path_); - } - catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - } } bool AvatarFileStorage::hasAvatar(const String& hash) const { @@ -25,7 +19,16 @@ bool AvatarFileStorage::hasAvatar(const String& hash) const { } void AvatarFileStorage::addAvatar(const String& hash, const ByteArray& avatar) { - boost::filesystem::ofstream file(getAvatarPath(hash), boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); + boost::filesystem::path avatarPath = getAvatarPath(hash); + if (!boost::filesystem::exists(avatarPath.parent_path())) { + try { + boost::filesystem::create_directories(avatarPath.parent_path()); + } + catch (const boost::filesystem::filesystem_error& e) { + std::cerr << "ERROR: " << e.what() << std::endl; + } + } + boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); file.write(avatar.getData(), avatar.getSize()); file.close(); } -- cgit v0.10.2-6-g49f6