From 196fd3182672b03f8468e5654736c614242b0f64 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 30 May 2021 07:30:36 +0200 Subject: Use sha256 for path for string cache download --- src/DownloadUtils.cpp | 23 +++++++---------------- src/QuickMedia.cpp | 3 +++ 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index eb8fa63..9834390 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -2,7 +2,7 @@ #include "../include/Program.hpp" #include "../include/Storage.hpp" #include "../include/NetUtils.hpp" -#include "../external/cppcodec/base64_url.hpp" +#include "../external/hash-library/sha256.h" #include #include #include @@ -162,15 +162,15 @@ namespace QuickMedia { } DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_browser_useragent, DownloadErrorHandler error_handler, Path cache_path) { - Path media_dir; Path media_file_path; if(cache_path.data.empty()) { - media_dir = get_cache_dir().join("media"); - media_file_path = Path(media_dir).join(cppcodec::base64_url::encode(url)); + SHA256 sha256; + sha256.add(url.data(), url.size()); + media_file_path = get_cache_dir().join("media").join(sha256.getHash()); } else { - media_dir = cache_path.parent(); media_file_path = std::move(cache_path); } + if(get_file_type(media_file_path) == FileType::REGULAR) { if(file_get_content(media_file_path, result) == 0) { fprintf(stderr, "Loaded %s from cache\n", url.c_str()); @@ -183,17 +183,8 @@ namespace QuickMedia { DownloadResult download_result = download_to_string(url, result, additional_args, use_browser_useragent, error_handler ? false : true); if(download_result == DownloadResult::OK && error_handler) download_result = error_handler(result) ? DownloadResult::OK : DownloadResult::ERR; - if(download_result == DownloadResult::OK) { - Path media_file_path_tmp(media_file_path.data + ".tmp"); - if(create_directory_recursive(media_dir) == 0 && file_overwrite(media_file_path_tmp, result) == 0) { - if(rename_atomic(media_file_path_tmp.data.c_str(), media_file_path.data.c_str()) != 0) { - perror("rename"); - download_result = DownloadResult::ERR; - } - } else { - download_result = DownloadResult::ERR; - } - } + if(download_result == DownloadResult::OK) + download_result = file_overwrite_atomic(media_file_path, result) == 0 ? DownloadResult::OK : DownloadResult::ERR; return download_result; } } diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 43ccbaf..a61b836 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -659,6 +659,9 @@ namespace QuickMedia { */ fprintf(stderr, "Monitor hz: %d\n", monitor_hz); + if(create_directory_recursive(get_cache_dir().join("media")) != 0) + throw std::runtime_error("Failed to create media directory"); + if(create_directory_recursive(get_cache_dir().join("thumbnails")) != 0) throw std::runtime_error("Failed to create thumbnails directory"); -- cgit v1.2.3