@@ -115,39 +115,48 @@ cpp::result<uint64_t, std::string> DownloadService::GetFileSize(
115115cpp::result<bool , std::string> DownloadService::AddAsyncDownloadTask (
116116 DownloadTask& task,
117117 std::optional<OnDownloadTaskSuccessfully> callback) noexcept {
118- auto verifying_result = VerifyDownloadTask (task);
119- if (verifying_result.has_error ()) {
120- return cpp::fail (verifying_result.error ());
118+
119+ if (std::find (download_task_list_.begin (), download_task_list_.end (),
120+ task.id ) != download_task_list_.end ()) {
121+ return cpp::fail (" Download task already exists: " + task.id );
122+ }
123+
124+ download_task_list_.push_back (task.id );
125+ download_task_map_.insert ({task.id , task});
126+
127+ {
128+ // verify download task
129+ auto result = VerifyDownloadTask (task);
130+ if (result.has_error ()) {
131+ CleanUp (task.id );
132+ return cpp::fail (result.error ());
133+ }
121134 }
122135
123136 auto execute_download_async = [&, task, callback]() {
124- std::optional<std::string> dl_err_msg = std::nullopt ;
137+ active_download_task_id_ = task.id ;
138+ std::optional<std::string> err_msg = std::nullopt ;
125139 for (const auto & item : task.items ) {
140+ active_download_item_id_ = item.id ;
126141 CTL_INF (" Start downloading: " + item.localPath .filename ().string ());
127-
128- if (event_queue_.has_value ()) {
129- event_queue_.value ()->enqueue (" download-started" ,
130- Event{.message = task.ToString ()});
131- event_queue_.value ()->process ();
132- CLI_LOG (" Enqueued download-started event: " << task.ToString ());
133- }
134-
135142 auto result = Download (task.id , item, false );
136143 if (result.has_error ()) {
137- dl_err_msg = result.error ();
144+ err_msg = result.error ();
138145 break ;
139146 }
140147 }
141148
142- if (dl_err_msg.has_value ()) {
143- CTL_ERR (dl_err_msg.value ());
149+ if (err_msg.has_value ()) {
150+ CTL_ERR (err_msg.value ());
151+ CleanUp (task.id );
144152 return ;
145153 }
146154
147155 if (callback.has_value ()) {
148156 CTL_INF (" Download success, executing post download lambda!" );
149157 callback.value ()(task);
150158 }
159+ CleanUp (task.id );
151160 };
152161
153162 std::thread t (execute_download_async);
@@ -223,6 +232,9 @@ cpp::result<bool, std::string> DownloadService::Download(
223232 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0L );
224233 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
225234
235+ curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progressCallback);
236+ curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this );
237+
226238 if (mode == " ab" ) {
227239 auto local_file_size = GetLocalFileSize (download_item.localPath );
228240 if (local_file_size != -1 ) {
@@ -260,3 +272,17 @@ curl_off_t DownloadService::GetLocalFileSize(
260272 fclose (file);
261273 return file_size;
262274}
275+
276+ void DownloadService::CleanUp (const std::string& task_id) {
277+ CTL_INF (" Cleaning up download task: " << task_id);
278+ // TODO: might need to be wrap with mutex
279+ // remove from task list
280+ download_task_list_.erase (std::remove (download_task_list_.begin (),
281+ download_task_list_.end (), task_id),
282+ download_task_list_.end ());
283+ // remove from task map
284+ download_task_map_.erase (task_id);
285+
286+ active_download_task_id_ = std::nullopt ;
287+ active_download_item_id_ = std::nullopt ;
288+ }
0 commit comments