diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-11-20 10:11:55 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-11-20 10:11:55 +0100 |
commit | df15b21559b50dbe8f7b45cb922960ac7eaf3211 (patch) | |
tree | 4a0f558fd36e0c4cc1a1905f99ec7ac5d4250663 /src | |
parent | e9ac949e65f5ffb54614dd733da27d9dbb879792 (diff) |
Add static image
Diffstat (limited to 'src')
-rw-r--r-- | src/Cache.cpp | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp index cf9aa8a..84bee97 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -159,7 +159,7 @@ namespace dchat return success; } - static ContentByUrlResult loadImageFromFile(const boost::filesystem::path &filepath, bool loadFromCache, CreateGifFunc createGifFunc) + static ContentByUrlResult loadImageFromFile(const boost::filesystem::path &filepath, bool loadFromCache, CreateGifFunc createGifFunc, CreateStaticImageFunc createStaticImageFunc) { StringView fileContent; try @@ -186,6 +186,7 @@ namespace dchat } else { + /* if(!loadFromCache) { if(!downscaleImage(filepath, (void*)fileContent.data, fileContent.size, 100, 100)) @@ -193,12 +194,12 @@ namespace dchat fprintf(stderr, "Failed to resize image: %s, using original file\n", filepath.c_str()); } } - + */ + StaticImage *image = createStaticImageFunc(filepath); delete[] fileContent.data; fileContent.data = nullptr; - return { new boost::filesystem::path(filepath), ContentByUrlResult::Type::CACHED }; + return { image, ContentByUrlResult::Type::CACHED }; } - break; } else if(state.step_result == PREVIEW_FOUND_TITLE) { @@ -225,14 +226,16 @@ namespace dchat { fprintf(stderr, "Failed to load image %s, reason: %s\n", filepath.string().c_str(), e.what()); } - return { (boost::filesystem::path*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; + return { (StaticImage*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; } - Cache::Cache(CreateGifFunc _createGifFunc) : + Cache::Cache(CreateGifFunc _createGifFunc, CreateStaticImageFunc _createStaticImageFunc) : alive(true), - createGifFunc(_createGifFunc) + createGifFunc(_createGifFunc), + createStaticImageFunc(_createStaticImageFunc) { assert(createGifFunc); + assert(createStaticImageFunc); downloadWaitThread = thread([this] { while(alive) @@ -250,11 +253,11 @@ namespace dchat bool failed = exitStatus != 0; if(failed) { - contentByUrlResult = { (boost::filesystem::path*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; + contentByUrlResult = { (StaticImage*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; } else { - contentByUrlResult = loadImageFromFile(filepath, false, createGifFunc); + contentByUrlResult = loadImageFromFile(filepath, false, createGifFunc, createStaticImageFunc); contentByUrlResult.lastAccessed = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count(); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { @@ -417,7 +420,7 @@ namespace dchat } // TODO: Do not load content in this thread. Return LOADING status and load it in another thread, because with a lot of images, chat can freeze - ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath, true, createGifFunc); + ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath, true, createGifFunc, createStaticImageFunc); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { contentByUrlResult.lastAccessed = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count(); @@ -432,7 +435,7 @@ namespace dchat } createFileIgnoreError(downloadingFilepath); - ContentByUrlResult result((boost::filesystem::path*)nullptr, ContentByUrlResult::Type::DOWNLOADING); + ContentByUrlResult result((StaticImage*)nullptr, ContentByUrlResult::Type::DOWNLOADING); contentUrlCache[url] = result; string downloadLimitBytesStr = to_string(downloadLimitBytes); |