From 1569d02aa38baa53d5442b3babdbf1a3aaa3aaa0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 19 Oct 2020 14:44:55 +0200 Subject: Load thumbnails with multiple threads, use sha256 for saving image to path instead of base64 (filename limit is 256 on linux...) --- src/DownloadUtils.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/DownloadUtils.cpp') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index 8782020..fd7e7d1 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -80,6 +80,31 @@ namespace QuickMedia { } } + // TODO: Use this everywhere we want to save to file (such as manga download) + DownloadResult download_to_file(const std::string &url, const std::string &destination_filepath, const std::vector &additional_args, bool use_tor, bool use_browser_useragent) { + Path tmp_filepath = destination_filepath; + tmp_filepath.append(".tmp"); + + // TODO: Optimize with temporary '\0' + size_t dir_end = tmp_filepath.data.rfind('/'); + if(dir_end != std::string::npos && create_directory_recursive(tmp_filepath.data.substr(0, dir_end)) != 0) + return DownloadResult::ERR; + + std::vector args = additional_args; + args.push_back({ "-o", tmp_filepath.data.c_str() }); + + std::string dummy; + DownloadResult res = download_to_string(url, dummy, std::move(args), use_tor, use_browser_useragent); + if(res != DownloadResult::OK) return res; + + if(rename(tmp_filepath.data.c_str(), destination_filepath.c_str()) != 0) { + perror("rename"); + return DownloadResult::ERR; + } + + return DownloadResult::OK; + } + // TODO: Add timeout DownloadResult download_to_json(const std::string &url, rapidjson::Document &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent, bool fail_on_error) { sf::Clock timer; -- cgit v1.2.3