diff options
Diffstat (limited to 'src/Cache.cpp')
-rw-r--r-- | src/Cache.cpp | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp index cba346b..c795595 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -196,22 +196,32 @@ namespace dchat int exitStatus; if(it->process->try_get_exit_status(exitStatus)) { + ContentByUrlResult contentByUrlResult; bool failed = exitStatus != 0; - if(!failed) + if(failed) + { + contentByUrlResult = { (sf::Texture*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; + } + else { boost::filesystem::path filepath = getImagesDir(); odhtdb::Hash urlHash(it->url.data(), it->url.size()); filepath /= urlHash.toString(); - ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath); - imageDownloadMutex.lock(); - contentUrlCache[it->url] = contentByUrlResult; - imageDownloadMutex.unlock(); + contentByUrlResult = loadImageFromFile(filepath); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { printf("Download content from url: %s\n", it->url.c_str()); } } + + imageDownloadMutex.lock(); + contentUrlCache[it->url] = contentByUrlResult; + boost::filesystem::path downloadingFilepath = getImagesDir() / ".downloading"; + // Intentionally ignore failure, program should not crash if we fail to remove these files... + boost::system::error_code err; + boost::filesystem::remove(downloadingFilepath, err); + imageDownloadMutex.unlock(); it = imageDownloadProcesses.erase(it); } else @@ -243,6 +253,18 @@ namespace dchat downloadWaitThread.join(); } + void replaceFileIgnoreError(const boost::filesystem::path &path) + { + try + { + fileReplace(path, StringView()); + } + catch(FileException &e) + { + fprintf(stderr, "Failed to replace file: %s, reason: %s\n", path.string().c_str(), e.what()); + } + } + const ContentByUrlResult Cache::getContentByUrl(const string &url, int downloadLimitBytes) { lock_guard<mutex> lock(imageDownloadMutex); @@ -255,6 +277,15 @@ namespace dchat odhtdb::Hash urlHash(url.data(), url.size()); filepath /= urlHash.toString(); + boost::filesystem::path downloadingFilepath = getImagesDir() / ".downloading"; + if(boost::filesystem::exists(downloadingFilepath)) + { + // Intentionally ignore failure, program should not crash if we fail to remove these files... + boost::system::error_code err; + boost::filesystem::remove(filepath, err); + boost::filesystem::remove(downloadingFilepath, err); + } + ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { @@ -268,6 +299,7 @@ namespace dchat return contentByUrlResult; } + replaceFileIgnoreError(downloadingFilepath); ContentByUrlResult result((sf::Texture*)nullptr, ContentByUrlResult::Type::DOWNLOADING); contentUrlCache[url] = result; |