From fd2568fdabff5760b868090b4d2e9f15fc120565 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Sat, 23 Dec 2017 17:54:22 +0100 Subject: [PATCH] Let http synchronizations overwrite files --- include/openspace/util/httprequest.h | 17 +++++++++++--- modules/sync/syncs/httpsynchronization.cpp | 4 +++- src/util/httprequest.cpp | 27 +++++++++++++++------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/include/openspace/util/httprequest.h b/include/openspace/util/httprequest.h index 33ebc36ee8..7c4cc2942d 100644 --- a/include/openspace/util/httprequest.h +++ b/include/openspace/util/httprequest.h @@ -212,8 +212,10 @@ private: class HttpFileDownload : public virtual HttpDownload { public: + using Overwrite = ghoul::Boolean; + HttpFileDownload() = default; - HttpFileDownload(std::string destination); + HttpFileDownload(std::string destination, Overwrite = Overwrite::No); HttpFileDownload(HttpFileDownload&& d) = default; virtual ~HttpFileDownload() = default; protected: @@ -224,6 +226,7 @@ protected: static std::mutex _directoryCreationMutex; private: std::string _destination; + bool _overwrite; std::ofstream _file; }; @@ -251,7 +254,11 @@ public: // Synchronous download to file class SyncHttpFileDownload : public SyncHttpDownload, public HttpFileDownload { public: - SyncHttpFileDownload(std::string url, std::string destinationPath); + SyncHttpFileDownload( + std::string url, + std::string destinationPath, + HttpFileDownload::Overwrite = Overwrite::No + ); virtual ~SyncHttpFileDownload() = default; }; @@ -265,7 +272,11 @@ public: // Asynchronous download to file class AsyncHttpFileDownload : public AsyncHttpDownload, public HttpFileDownload { public: - AsyncHttpFileDownload(std::string url, std::string destinationPath); + AsyncHttpFileDownload( + std::string url, + std::string destinationPath, + HttpFileDownload::Overwrite = Overwrite::No + ); virtual ~AsyncHttpFileDownload() = default; }; diff --git a/modules/sync/syncs/httpsynchronization.cpp b/modules/sync/syncs/httpsynchronization.cpp index aa1882dbb7..1fa36c8d2e 100644 --- a/modules/sync/syncs/httpsynchronization.cpp +++ b/modules/sync/syncs/httpsynchronization.cpp @@ -206,7 +206,9 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) { ghoul::filesystem::FileSystem::PathSeparator + filename; - downloads.push_back(std::make_unique(line, fileDestination)); + downloads.push_back(std::make_unique( + line, fileDestination, HttpFileDownload::Overwrite::Yes)); + auto& fileDownload = downloads.back(); ++nDownloads; diff --git a/src/util/httprequest.cpp b/src/util/httprequest.cpp index be990b57ef..675ccb2f81 100644 --- a/src/util/httprequest.cpp +++ b/src/util/httprequest.cpp @@ -306,12 +306,15 @@ size_t HttpMemoryDownload::handleData(HttpRequest::Data d) { return d.size; } -HttpFileDownload::HttpFileDownload(std::string destination) { - _destination = std::move(destination); -} +HttpFileDownload::HttpFileDownload( + std::string destination, + HttpFileDownload::Overwrite overwrite) + : _destination(std::move(destination)) + , _overwrite(overwrite) +{} bool HttpFileDownload::initDownload() { - if (FileSys.fileExists(_destination)) { + if (!_overwrite && FileSys.fileExists(_destination)) { LERROR("File " << _destination << " already exists!"); return false; } @@ -352,18 +355,26 @@ SyncHttpMemoryDownload::SyncHttpMemoryDownload(std::string url) , HttpMemoryDownload() {} -SyncHttpFileDownload::SyncHttpFileDownload(std::string url, std::string destinationPath) +SyncHttpFileDownload::SyncHttpFileDownload( + std::string url, + std::string destinationPath, + HttpFileDownload::Overwrite overwrite +) : SyncHttpDownload(std::move(url)) - , HttpFileDownload(std::move(destinationPath)) + , HttpFileDownload(std::move(destinationPath), overwrite) {} AsyncHttpMemoryDownload::AsyncHttpMemoryDownload(std::string url) : AsyncHttpDownload(std::move(url)) {} -AsyncHttpFileDownload::AsyncHttpFileDownload(std::string url, std::string destinationPath) +AsyncHttpFileDownload::AsyncHttpFileDownload( + std::string url, + std::string destinationPath, + HttpFileDownload::Overwrite overwrite +) : AsyncHttpDownload(std::move(url)) - , HttpFileDownload(std::move(destinationPath)) + , HttpFileDownload(std::move(destinationPath), overwrite) {}