aboutsummaryrefslogtreecommitdiff
path: root/src/AsyncImageLoader.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-04-02 23:29:33 +0200
committerdec05eba <dec05eba@protonmail.com>2021-04-02 23:29:33 +0200
commit3ca7ed72c2f3a046e94213a8c26d80eafde9585c (patch)
tree67959bc624c4af5ad9d2e9ae805295097ca0a4ea /src/AsyncImageLoader.cpp
parent42ab6b1f7ad01cf87fa611b22313172a30eaff51 (diff)
FileManager: show video thumbnails, update thumbnail if name is the same but the content has changed (last modified time changed)
Diffstat (limited to 'src/AsyncImageLoader.cpp')
-rw-r--r--src/AsyncImageLoader.cpp45
1 files changed, 33 insertions, 12 deletions
diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp
index b5a61ae..1c676c1 100644
--- a/src/AsyncImageLoader.cpp
+++ b/src/AsyncImageLoader.cpp
@@ -1,10 +1,13 @@
#include "../include/AsyncImageLoader.hpp"
+#include "../include/FileAnalyzer.hpp"
#include "../include/DownloadUtils.hpp"
#include "../include/Program.hpp"
#include "../include/ImageUtils.hpp"
#include "../include/Scale.hpp"
#include "../include/SfmlFixes.hpp"
#include "../external/hash-library/sha256.h"
+
+#include <sys/stat.h>
#include <assert.h>
namespace QuickMedia {
@@ -29,6 +32,20 @@ namespace QuickMedia {
// Create thumbnail and load it. On failure load the original image
static void create_thumbnail(const Path &thumbnail_path, const Path &thumbnail_path_resized, ThumbnailData *thumbnail_data, sf::Vector2i resize_target_size) {
+ FileAnalyzer file_analyzer;
+ if(!file_analyzer.load_file(thumbnail_path.data.c_str(), false)) {
+ fprintf(stderr, "Failed to convert %s to a thumbnail, using the original image\n", thumbnail_path.data.c_str());
+ thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
+ return;
+ }
+
+ if(is_content_type_video(file_analyzer.get_content_type())) {
+ if(video_get_first_frame(thumbnail_path.data.c_str(), thumbnail_path_resized.data.c_str(), resize_target_size.x, resize_target_size.y))
+ load_image_from_file(*thumbnail_data->image, thumbnail_path_resized.data);
+ thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
+ return;
+ }
+
if(create_thumbnail(thumbnail_path, thumbnail_path_resized, resize_target_size)) {
load_image_from_file(*thumbnail_data->image, thumbnail_path_resized.data);
} else {
@@ -115,11 +132,21 @@ namespace QuickMedia {
SHA256 sha256;
sha256.add(url.data(), url.size());
Path thumbnail_path = get_cache_dir().join("thumbnails").join(sha256.getHash());
- if(local && get_file_type(url) == FileType::REGULAR) {
+ if(local) {
+ struct stat file_stat;
+ if(stat(url.c_str(), &file_stat) != 0 || !S_ISREG(file_stat.st_mode)) {
+ thumbnail_data->image = std::make_unique<sf::Image>();
+ thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
+ return;
+ }
+
+ thumbnail_path.append("_" + std::to_string(file_stat.st_mtim.tv_sec));
thumbnail_data->loading_state = LoadingState::LOADING;
image_load_queue.push({ url, thumbnail_path, true, thumbnail_data, resize_target_size });
return;
- } else if(get_file_type(thumbnail_path) == FileType::REGULAR) {
+ }
+
+ if(get_file_type(thumbnail_path) == FileType::REGULAR) {
thumbnail_data->loading_state = LoadingState::LOADING;
image_load_queue.push({ url, thumbnail_path, false, thumbnail_data, resize_target_size });
return;
@@ -135,7 +162,7 @@ namespace QuickMedia {
download_image_thread[free_index].join();
// TODO: Keep the thread running and use conditional variable instead to sleep until a new image should be loaded. Same in ImageViewer.
- download_image_thread[free_index] = std::thread([this, free_index, thumbnail_path, url, local, resize_target_size, thumbnail_data]() mutable {
+ download_image_thread[free_index] = std::thread([this, free_index, thumbnail_path, url, resize_target_size, thumbnail_data]() mutable {
thumbnail_data->image = std::make_unique<sf::Image>();
Path thumbnail_path_resized = thumbnail_path;
@@ -149,22 +176,16 @@ namespace QuickMedia {
return;
}
- if(!local && get_file_type(thumbnail_path.data) != FileType::REGULAR && download_to_file(url, thumbnail_path.data, {}, true) != DownloadResult::OK) {
+ if(get_file_type(thumbnail_path.data) == FileType::FILE_NOT_FOUND && download_to_file(url, thumbnail_path.data, {}, true) != DownloadResult::OK) {
thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
loading_image[free_index] = false;
return;
}
- Path thumbnail_original_path;
- if(local)
- thumbnail_original_path = url;
- else
- thumbnail_original_path = thumbnail_path;
-
if(resize_target_size.x != 0 && resize_target_size.y != 0)
- create_thumbnail(thumbnail_original_path, thumbnail_path_resized, thumbnail_data.get(), resize_target_size);
+ create_thumbnail(thumbnail_path, thumbnail_path_resized, thumbnail_data.get(), resize_target_size);
else
- load_image_from_file(*thumbnail_data->image, thumbnail_original_path.data);
+ load_image_from_file(*thumbnail_data->image, thumbnail_path.data);
thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
loading_image[free_index] = false;