aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-05-30 07:30:36 +0200
committerdec05eba <dec05eba@protonmail.com>2021-05-30 07:30:36 +0200
commit196fd3182672b03f8468e5654736c614242b0f64 (patch)
tree029a83d6e0d27e6e60d3b4eab4b95ed6a6e31661 /src
parentd52f89bc8ab581ded9cbcecc921a355ab9638a55 (diff)
Use sha256 for path for string cache download
Diffstat (limited to 'src')
-rw-r--r--src/DownloadUtils.cpp23
-rw-r--r--src/QuickMedia.cpp3
2 files changed, 10 insertions, 16 deletions
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 <unistd.h>
#include <limits.h>
#include <SFML/System/Clock.hpp>
@@ -162,15 +162,15 @@ namespace QuickMedia {
}
DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector<CommandArg> &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");