diff --git a/src/downloader/pd_file/enums.rs b/src/downloader/pd_file/enums.rs index a5c979a..553b502 100644 --- a/src/downloader/pd_file/enums.rs +++ b/src/downloader/pd_file/enums.rs @@ -27,6 +27,12 @@ impl PdFileStatus { pub fn is_downloading(&self) -> bool { *self == PdFileStatus::Downloading } + + #[inline] + /// Returns true if the download is started. + pub fn is_started(&self) -> bool { + *self == PdFileStatus::Started + } } /// The type of the downloader. diff --git a/src/downloader/pd_file/file.rs b/src/downloader/pd_file/file.rs index d8d5538..c4e3e95 100644 --- a/src/downloader/pd_file/file.rs +++ b/src/downloader/pd_file/file.rs @@ -224,6 +224,12 @@ impl PdFile { self.status.get_ref().is_downloading() } + #[inline] + /// Returns true if the download is started. + pub fn is_started(&self) -> bool { + self.status.get_ref().is_started() + } + #[inline] /// Returns true if stored in memory only. fn is_mem_only(&self) -> bool { diff --git a/src/downloader/tasks.rs b/src/downloader/tasks.rs index 387c051..261515e 100644 --- a/src/downloader/tasks.rs +++ b/src/downloader/tasks.rs @@ -142,6 +142,25 @@ pub async fn create_download_tasks_simple< handle_download(d, result).await } +/// Do first job when download in multiple mode. +pub async fn create_download_tasks_multi_first< + T: Seek + Write + Send + Sync + ClearFile + GetTargetFileName, +>( + d: Arc>, +) -> Result<(), DownloaderError> { + let result = d + .client + .get(d.url.deref().clone(), d.headers.as_ref().clone()) + .await + .try_err(gettext("Failed to get url."))?; + let status = result.status(); + if status.as_u16() >= 400 { + return Err(DownloaderError::from(status)); + } + // # TODO + Ok(()) +} + /// Handle download process pub async fn handle_download( d: Arc>, @@ -224,6 +243,11 @@ pub async fn check_tasks< if !d.is_multi_threads() { let task = tokio::spawn(create_download_tasks_simple(Arc::clone(&d))); d.add_task(task); + } else { + if d.pd.is_started() { + let task = tokio::spawn(create_download_tasks_multi_first(Arc::clone(&d))); + d.add_task(task); + } } loop { check_dropped!(d);