aboutsummaryrefslogtreecommitdiff
path: root/src/DownloadUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/DownloadUtils.cpp')
-rw-r--r--src/DownloadUtils.cpp25
1 files changed, 25 insertions, 0 deletions
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<CommandArg> &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<CommandArg> 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<CommandArg> &additional_args, bool use_tor, bool use_browser_useragent, bool fail_on_error) {
sf::Clock timer;