From 1e0e68f9cda51c881b32a54d9eece71c1428f7ac Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 22 Apr 2018 05:58:44 +0200 Subject: Add video and gif support Gif streams from url. Todo: Add play controls to video --- src/Cache.cpp | 87 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 35 deletions(-) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index ba57d4c..accd0c4 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1,6 +1,8 @@ #include "../include/Cache.hpp" #include "../include/env.hpp" #include "../include/ResourceCache.hpp" +#include "../include/FileUtil.hpp" +#include "../include/Gif.hpp" #include #include #include @@ -58,6 +60,40 @@ namespace dchat return dchatHomeDir; } + ImageByUrlResult loadImageFromFile(const boost::filesystem::path &filepath) + { + try + { + StringView fileContent = getFileContent(filepath); + if(Gif::isDataGif(fileContent)) + { + Gif *gif = new Gif(move(fileContent)); + return { gif, ImageByUrlResult::Type::CACHED }; + } + else + { + sf::Texture *texture = new sf::Texture(); + if(texture->loadFromMemory(fileContent.data, fileContent.size)) + { + delete fileContent.data; + texture->setSmooth(true); + texture->generateMipmap(); + return { texture, ImageByUrlResult::Type::CACHED }; + } + delete fileContent.data; + } + } + catch(FileException &e) + { + + } + catch(FailedToLoadResourceException &e) + { + + } + return { (sf::Texture*)nullptr, ImageByUrlResult::Type::FAILED_DOWNLOAD }; + } + Cache::Cache() { downloadWaitThread = thread([this] @@ -71,34 +107,24 @@ namespace dchat if(it->process->try_get_exit_status(exitStatus)) { bool failed = exitStatus != 0; - ImageByUrlResult &imageByUrlResult = imageUrlCache[it->url]; - if(!failed) { boost::filesystem::path filepath = getDchatDir(); odhtdb::Hash urlHash(it->url.data(), it->url.size()); filepath /= urlHash.toString(); - - try - { - sf::Texture *texture = ResourceCache::getTexture(filepath.string()); - imageByUrlResult.texture = texture; - imageByUrlResult.type = ImageByUrlResult::Type::CACHED; - printf("Image downloaded from url: %s, texture: %u\n", it->url.c_str(), texture); - } - catch(FailedToLoadResourceException &e) + + ImageByUrlResult imageByUrlResult = loadImageFromFile(filepath); + imageUrlCache[it->url] = imageByUrlResult; + switch(imageByUrlResult.type) { - fprintf(stderr, "%s\n", e.what()); - failed = true; + case ImageByUrlResult::Type::CACHED: + printf("Downloaded image from url: %s\n", it->url.c_str()); + break; + case ImageByUrlResult::Type::FAILED_DOWNLOAD: + printf("Failed to download and load image from url: %s\n", it->url.c_str()); + break; } } - - if(failed) - { - imageByUrlResult.type = ImageByUrlResult::Type::FAILED_DOWNLOAD; - fprintf(stderr, "Image download failed for url: %s\n", it->url.c_str()); - } - it = imageDownloadProcesses.erase(it); } else @@ -127,24 +153,15 @@ namespace dchat odhtdb::Hash urlHash(url.data(), url.size()); filepath /= urlHash.toString(); - // Check if file exists because we dont want sfml spam with "Failed to load image""... - if(boost::filesystem::exists(filepath)) + ImageByUrlResult imageByUrlResult = loadImageFromFile(filepath); + if(imageByUrlResult.type == ImageByUrlResult::Type::CACHED) { - try - { - sf::Texture *texture = ResourceCache::getTexture(filepath.string()); - ImageByUrlResult result { texture, ImageByUrlResult::Type::CACHED }; - imageUrlCache[url] = result; - printf("Loading image from file cache: %s\n", url.c_str()); - return result; - } - catch(FailedToLoadResourceException &e) - { - - } + imageUrlCache[url] = imageByUrlResult; + printf("Loaded image from file cache: %s, is gif: %s\n", url.c_str(), imageByUrlResult.isGif ? "yes" : "no"); + return imageByUrlResult; } - ImageByUrlResult result { nullptr, ImageByUrlResult::Type::DOWNLOADING }; + ImageByUrlResult result((sf::Texture*)nullptr, ImageByUrlResult::Type::DOWNLOADING); imageUrlCache[url] = result; string downloadLimitBytesStr = to_string(downloadLimitBytes); -- cgit v1.2.3