diff --git a/src/downloader/pd_file/file.rs b/src/downloader/pd_file/file.rs index 9673a0b..ae14cd4 100644 --- a/src/downloader/pd_file/file.rs +++ b/src/downloader/pd_file/file.rs @@ -38,6 +38,9 @@ lazy_static! { static ref MAGIC_WORDS: Vec = vec![0x50, 0x44, 0xff, 0xff]; } +/// The offset of the status in pd file +const STATUS_OFFSET: SeekFrom = SeekFrom::Start(10); +/// The offset of the file_size in pd file const FILE_SIZE_OFFSET: SeekFrom = SeekFrom::Start(12); #[derive(Debug)] @@ -117,6 +120,20 @@ impl PdFile { Ok(()) } + /// Complete the download. + pub fn complete(&self) -> Result<(), PdFileError> { + self.set_completed(); + if !self.is_mem_only() { + self.need_saved.qstore(true); + let mut f = self.file.get_mut(); + let f = f.as_mut().unwrap(); + f.seek(STATUS_OFFSET)?; + f.write_le_u8(self.status.get_ref().int_value())?; + self.need_saved.qstore(false); + } + Ok(()) + } + /// Force close the file pub fn force_close(&self) { let mut f = self.file.get_mut(); diff --git a/src/downloader/tasks.rs b/src/downloader/tasks.rs index a7115ed..e87414a 100644 --- a/src/downloader/tasks.rs +++ b/src/downloader/tasks.rs @@ -3,7 +3,9 @@ use crate::ext::try_err::TryErr; use crate::gettext; use super::error::DownloaderError; use super::downloader::DownloaderInternal; +use futures_util::StreamExt; use http_content_range::ContentRange; +use reqwest::Response; use std::ops::Deref; use std::io::Seek; use std::io::Write; @@ -72,5 +74,27 @@ pub async fn create_download_tasks_simple {} } } + handle_download(d, result).await +} + +/// Handle download process +pub async fn handle_download(d: Arc>, re: Response) -> Result<(), DownloaderError> { + let mut stream = re.bytes_stream(); + loop { + let mut n = stream.next(); + let re = tokio::time::timeout(std::time::Duration::from_secs(10), &mut n).await; + match re { + Ok(s) => { + match s { + Some(data) => {} + None => { + d.pd.complete()?; + break; + } + } + } + Err(_) => {} // TODO: Timed out + } + } Ok(()) }