From df15b21559b50dbe8f7b45cb922960ac7eaf3211 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 20 Nov 2018 10:11:55 +0100 Subject: Add static image --- include/dchat/Cache.hpp | 16 ++++++++++------ include/dchat/StaticImage.hpp | 12 ++++++++++++ src/Cache.cpp | 25 ++++++++++++++----------- 3 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 include/dchat/StaticImage.hpp diff --git a/include/dchat/Cache.hpp b/include/dchat/Cache.hpp index 62df621..4d37b54 100644 --- a/include/dchat/Cache.hpp +++ b/include/dchat/Cache.hpp @@ -21,6 +21,7 @@ namespace TinyProcessLib namespace dchat { class Gif; + class StaticImage; class WebPagePreview; struct ContentByUrlResult @@ -35,20 +36,20 @@ namespace dchat enum class CachedType { NONE, - TEXTURE_FILEPATH, + STATIC_IMAGE, GIF, WEB_PAGE_PREVIEW }; - ContentByUrlResult() : textureFilePath(nullptr), type(Type::DOWNLOADING), cachedType(CachedType::NONE) {} - ContentByUrlResult(boost::filesystem::path *_textureFilePath, Type _type) : textureFilePath(_textureFilePath), type(_type), cachedType(CachedType::TEXTURE_FILEPATH) {} + ContentByUrlResult() : staticImage(nullptr), type(Type::DOWNLOADING), cachedType(CachedType::NONE) {} + ContentByUrlResult(StaticImage *_staticImage, Type _type) : staticImage(_staticImage), type(_type), cachedType(CachedType::STATIC_IMAGE) {} ContentByUrlResult(Gif *_gif, Type _type) : gif(_gif), type(_type), cachedType(CachedType::GIF) {} ContentByUrlResult(WebPagePreview *_webPagePreview, Type _type) : webPagePreview(_webPagePreview), type(_type), cachedType(CachedType::WEB_PAGE_PREVIEW) {} // @texture is null if @type is DOWNLOADING or FAILED_DOWNLOAD union { - boost::filesystem::path *textureFilePath; + StaticImage *staticImage; Gif *gif; WebPagePreview *webPagePreview; }; @@ -62,13 +63,15 @@ namespace dchat // @fileContent contains data allocated with new[], deallocate it with delete[] fileContent.data; // Returned gif should be allocated with @new using CreateGifFunc = std::function; + using CreateStaticImageFunc = std::function; class Cache { DISABLE_COPY(Cache) public: - // @createGifFunc can't be nullptr - Cache(CreateGifFunc createGifFunc); + // @createGifFunc can't be null + // @createStaticImageFunc can't be null + Cache(CreateGifFunc createGifFunc, CreateStaticImageFunc createStaticImageFunc); ~Cache(); // Creates directory if it doesn't exist (recursively). Throws boost exception on failure @@ -100,5 +103,6 @@ namespace dchat bool alive; std::unordered_map contentUrlCache; CreateGifFunc createGifFunc; + CreateStaticImageFunc createStaticImageFunc; }; } diff --git a/include/dchat/StaticImage.hpp b/include/dchat/StaticImage.hpp new file mode 100644 index 0000000..372f7ce --- /dev/null +++ b/include/dchat/StaticImage.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace dchat +{ + class StaticImage + { + public: + virtual ~StaticImage(){} + }; +} \ No newline at end of file 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::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::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); -- cgit v1.2.3