88#include < ostream>
99#include < utility>
1010#include " download_service.h"
11+ #include " utils/curl_utils.h"
1112#include " utils/format_utils.h"
1213#include " utils/logging_utils.h"
1314#include " utils/result.hpp"
@@ -27,43 +28,9 @@ size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void* userdata) {
2728}
2829} // namespace
2930
30- cpp::result<void , std::string> DownloadService::VerifyDownloadTask (
31- DownloadTask& task) const noexcept {
32- CLI_LOG (" Validating download items, please wait.." );
33-
34- auto total_download_size{0 };
35- std::optional<std::string> err_msg = std::nullopt ;
36-
37- for (auto & item : task.items ) {
38- auto file_size = GetFileSize (item.downloadUrl );
39- if (file_size.has_error ()) {
40- err_msg = file_size.error ();
41- break ;
42- }
43-
44- item.bytes = file_size.value ();
45- total_download_size += file_size.value ();
46- }
47-
48- if (err_msg.has_value ()) {
49- CTL_ERR (err_msg.value ());
50- return cpp::fail (err_msg.value ());
51- }
52-
53- return {};
54- }
55-
5631cpp::result<bool , std::string> DownloadService::AddDownloadTask (
5732 DownloadTask& task,
5833 std::optional<OnDownloadTaskSuccessfully> callback) noexcept {
59- auto validating_result = VerifyDownloadTask (task);
60- if (validating_result.has_error ()) {
61- return cpp::fail (validating_result.error ());
62- }
63-
64- // all items are valid, start downloading
65- // if any item from the task failed to download, the whole task will be
66- // considered failed
6734 std::optional<std::string> dl_err_msg = std::nullopt ;
6835 bool has_task_done = false ;
6936 for (const auto & item : task.items ) {
@@ -87,10 +54,7 @@ cpp::result<bool, std::string> DownloadService::AddDownloadTask(
8754}
8855
8956cpp::result<uint64_t , std::string> DownloadService::GetFileSize (
90- const std::string& url,
91- const std::optional<
92- std::reference_wrapper<std::unordered_map<std::string, std::string>>>&
93- headers) const noexcept {
57+ const std::string& url) const noexcept {
9458
9559 auto curl = curl_easy_init ();
9660 if (!curl) {
@@ -101,10 +65,11 @@ cpp::result<uint64_t, std::string> DownloadService::GetFileSize(
10165 curl_easy_setopt (curl, CURLOPT_NOBODY, 1L );
10266 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
10367
68+ auto headers = curl_utils::GetHeaders (url);
10469 if (headers.has_value ()) {
10570 curl_slist* curl_headers = nullptr ;
10671
107- for (const auto & [key, value] : headers-> get ()) {
72+ for (const auto & [key, value] : headers. value ()) {
10873 auto header = key + " : " + value;
10974 curl_headers = curl_slist_append (curl_headers, header.c_str ());
11075 }
@@ -185,10 +150,11 @@ cpp::result<bool, std::string> DownloadService::Download(
185150 }
186151
187152 curl_easy_setopt (curl, CURLOPT_URL, download_item.downloadUrl .c_str ());
188- if (download_item.headers .has_value ()) {
153+ auto headers = curl_utils::GetHeaders (download_item.downloadUrl );
154+ if (headers.has_value ()) {
189155 curl_slist* curl_headers = nullptr ;
190156
191- for (const auto & [key, value] : download_item. headers .value ()) {
157+ for (const auto & [key, value] : headers.value ()) {
192158 auto header = key + " : " + value;
193159 curl_headers = curl_slist_append (curl_headers, header.c_str ());
194160 }
@@ -262,7 +228,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
262228
263229 active_task_ = std::make_shared<DownloadTask>(task);
264230
265- for (auto & item : task.items ) {
231+ for (const auto & item : task.items ) {
266232 auto handle = curl_easy_init ();
267233 if (handle == nullptr ) {
268234 // skip the task
@@ -282,10 +248,11 @@ void DownloadService::ProcessTask(DownloadTask& task) {
282248 });
283249 downloading_data_map_.insert (std::make_pair (item.id , dl_data_ptr));
284250
285- if (item.headers .has_value ()) {
251+ auto headers = curl_utils::GetHeaders (item.downloadUrl );
252+ if (headers.has_value ()) {
286253 curl_slist* curl_headers = nullptr ;
287254
288- for (const auto & [key, value] : item. headers .value ()) {
255+ for (const auto & [key, value] : headers.value ()) {
289256 auto header = key + " : " + value;
290257 curl_headers = curl_slist_append (curl_headers, header.c_str ());
291258 }
@@ -408,11 +375,6 @@ void DownloadService::ProcessCompletedTransfers() {
408375
409376cpp::result<DownloadTask, std::string> DownloadService::AddTask (
410377 DownloadTask& task, std::function<void (const DownloadTask&)> callback) {
411- auto validate_result = VerifyDownloadTask (task);
412- if (validate_result.has_error ()) {
413- return cpp::fail (validate_result.error ());
414- }
415-
416378 {
417379 std::lock_guard<std::mutex> lock (callbacks_mutex_);
418380 callbacks_[task.id ] = std::move (callback);
0 commit comments