This commit is contained in:
2022-06-10 04:28:38 +00:00
committed by GitHub
parent a5d582c67b
commit defea9b761
2 changed files with 41 additions and 0 deletions

View File

@@ -38,6 +38,9 @@ lazy_static! {
static ref MAGIC_WORDS: Vec<u8> = 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();

View File

@@ -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<T: Seek + Write + Send + Sync + ClearF
None => {}
}
}
handle_download(d, result).await
}
/// Handle download process
pub async fn handle_download<T: Seek + Write + Send + Sync + ClearFile>(d: Arc<DownloaderInternal<T>>, 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(())
}