aboutsummaryrefslogtreecommitdiff
path: root/src/Cache.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-04-22 05:58:44 +0200
committerdec05eba <dec05eba@protonmail.com>2018-04-22 05:59:18 +0200
commit1e0e68f9cda51c881b32a54d9eece71c1428f7ac (patch)
treeb8faa1d971c245e3fcf046aa1d2daa1fa601e0f9 /src/Cache.cpp
parent424b02609fa34175a4e2aadb95e68b3c9c8dc93c (diff)
Add video and gif support
Gif streams from url. Todo: Add play controls to video
Diffstat (limited to 'src/Cache.cpp')
-rw-r--r--src/Cache.cpp87
1 files changed, 52 insertions, 35 deletions
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 <boost/filesystem/convenience.hpp>
#include <unordered_map>
#include <process.hpp>
@@ -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);