@@ -228,7 +228,7 @@ cpp::result<bool, std::string> DownloadService::Download(
228228
229229curl_off_t DownloadService::GetLocalFileSize (
230230 const std::filesystem::path& path) const {
231- FILE* file = fopen (path.string ().c_str (), " r" );
231+ auto file = fopen (path.string ().c_str (), " r" );
232232 if (!file) {
233233 return -1 ;
234234 }
@@ -237,7 +237,7 @@ curl_off_t DownloadService::GetLocalFileSize(
237237 return -1 ;
238238 }
239239
240- curl_off_t file_size = ftell64 (file);
240+ auto file_size = ftell64 (file);
241241 fclose (file);
242242 return file_size;
243243}
@@ -264,11 +264,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
264264 CTL_INF (" Processing task: " + task.id );
265265 std::vector<std::pair<CURL*, FILE*>> task_handles;
266266
267- downloading_data_ = std::make_shared<DownloadingData>(DownloadingData{
268- .item_id = " " ,
269- .download_task = &task,
270- .event_queue = event_queue_.get (),
271- });
267+ active_task_ = std::make_shared<DownloadTask>(task);
272268
273269 for (auto & item : task.items ) {
274270 CURL* handle = curl_easy_init ();
@@ -284,7 +280,13 @@ void DownloadService::ProcessTask(DownloadTask& task) {
284280 CTL_ERR (" Failed to open output file " + item.localPath .string ());
285281 return ;
286282 }
287- downloading_data_->item_id = item.id ;
283+
284+ auto dl_data_ptr = std::make_shared<DownloadingData>(DownloadingData{
285+ .item_id = item.id ,
286+ .download_service = this ,
287+ });
288+ downloading_data_map_.insert (std::make_pair (item.id , dl_data_ptr));
289+
288290 if (auto headers = CreateHeaders (item.downloadUrl ); headers) {
289291 curl_easy_setopt (handle, CURLOPT_HTTPHEADER, headers);
290292 }
@@ -294,7 +296,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
294296 curl_easy_setopt (handle, CURLOPT_FOLLOWLOCATION, 1L );
295297 curl_easy_setopt (handle, CURLOPT_NOPROGRESS, 0L );
296298 curl_easy_setopt (handle, CURLOPT_XFERINFOFUNCTION, ProgressCallback);
297- curl_easy_setopt (handle, CURLOPT_XFERINFODATA, downloading_data_ .get ());
299+ curl_easy_setopt (handle, CURLOPT_XFERINFODATA, dl_data_ptr .get ());
298300
299301 curl_multi_add_handle (multi_handle_, handle);
300302 task_handles.push_back (std::make_pair (handle, file));
@@ -329,6 +331,8 @@ void DownloadService::ProcessTask(DownloadTask& task) {
329331 fclose (pair.second );
330332 }
331333
334+ active_task_.reset ();
335+ downloading_data_map_.clear ();
332336 return ;
333337 }
334338
@@ -338,7 +342,8 @@ void DownloadService::ProcessTask(DownloadTask& task) {
338342 curl_easy_cleanup (pair.first );
339343 fclose (pair.second );
340344 }
341- downloading_data_.reset ();
345+ downloading_data_map_.clear ();
346+ active_task_.reset ();
342347
343348 RemoveTaskFromStopList (task.id );
344349
0 commit comments