From 54ab8bc0db247f02af55e0e7d01e511c835c2033 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 19 Jun 2022 23:58:10 +0000 Subject: [PATCH] Update --- src/downloader/downloader.rs | 23 +++++++++++++++++++ src/downloader/pd_file/file.rs | 10 ++++++++ src/downloader/tasks.rs | 42 ++++++++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/downloader/downloader.rs b/src/downloader/downloader.rs index ba3eac8..1daa786 100644 --- a/src/downloader/downloader.rs +++ b/src/downloader/downloader.rs @@ -3,6 +3,7 @@ use super::enums::DownloaderStatus; use super::error::DownloaderError; use super::local_file::LocalFile; use super::pd_file::PdFile; +use super::pd_file::PdFilePartStatus; use super::pd_file::PdFileResult; use super::tasks::check_tasks; use crate::ext::atomic::AtomicQuick; @@ -462,6 +463,28 @@ impl DownloaderIn } Ok(()) } + + /// Write datas to the file. + /// * `data` - Data + /// * `pd` - The status of the writed part + /// * + pub fn write_part( + &self, + data: &[u8], + pd: &Arc, + index: usize, + ) -> Result<(), DownloaderError> { + match self.file.get_mut().deref_mut() { + Some(f) => { + let offset = + (self.get_part_size() as u64) * (index as u64) + (pd.downloaded_size() as u64); + f.seek(SeekFrom::Start(offset))?; + f.write_all(data)?; + } + None => {} + } + Ok(()) + } } /// A file downloader diff --git a/src/downloader/pd_file/file.rs b/src/downloader/pd_file/file.rs index 9762d79..ab0a01d 100644 --- a/src/downloader/pd_file/file.rs +++ b/src/downloader/pd_file/file.rs @@ -276,6 +276,16 @@ impl PdFile { Ok(()) } + /// Returns true if all parts are downloaded. + pub fn is_all_part_downloaded(&self) -> bool { + for part in self.part_datas.get_ref().iter() { + if !part.is_downloaded() { + return false; + } + } + return true; + } + #[inline] /// Returns true if the download is completed. pub fn is_completed(&self) -> bool { diff --git a/src/downloader/tasks.rs b/src/downloader/tasks.rs index 4d31040..d08492f 100644 --- a/src/downloader/tasks.rs +++ b/src/downloader/tasks.rs @@ -150,18 +150,32 @@ pub async fn create_download_tasks_multi_first< >( d: Arc>, ) -> Result<(), DownloaderError> { + #[cfg(test)] + { + println!("Created first download task in multiple thread mode."); + } 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(); + #[cfg(test)] + { + println!("HTTP status: {}", status); + } if status.as_u16() >= 400 { return Err(DownloaderError::from(status)); } match result.content_length() { Some(len) => match d.pd.set_file_size(len) { - Ok(_) => {} + Ok(_) => { + #[cfg(test)] + { + println!("Set the file size to {}", len); + println!("Is downloading: {}", d.pd.is_downloading()); + } + } Err(e) => { println!("{}", e) } @@ -212,7 +226,11 @@ pub async fn create_download_tasks_multi< if status.as_u16() != 206 { return Err(DownloaderError::from(status)); } - handle_download(d, result, Some(pd), Some(index)).await + let re = handle_download(d, result, Some(pd), Some(index)).await; + if re.is_err() { + // #TODO + } + re } /// Handle download process @@ -244,6 +262,7 @@ pub async fn handle_download { - if !dur.is_zero() { - tokio::time::sleep(dur).await; + if d.tasks.get_ref().len() == 0 { + match dur { + Some(dur) => { + if !dur.is_zero() { + tokio::time::sleep(dur).await; + } } + None => {} } - None => {} + let task = tokio::spawn(create_download_tasks_multi_first(Arc::clone(&d))); + d.add_task(task); } - let task = tokio::spawn(create_download_tasks_multi_first(Arc::clone(&d))); - d.add_task(task); } else { if d.tasks.get_ref().len() < (d.max_threads.qload() as usize) { add_new_multi_tasks(&d).await?; } + if d.pd.is_all_part_downloaded() { + need_break = true; + } } } if need_break {