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 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) (limited to 'src/DownloadUtils.cpp') 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; } } -- cgit v1.2.3