This commit is contained in:
2022-05-28 01:09:31 +00:00
committed by GitHub
parent 4a0bb93f97
commit c9474bbab5
9 changed files with 97 additions and 2 deletions

1
Cargo.lock generated
View File

@@ -1169,6 +1169,7 @@ dependencies = [
"reqwest",
"spin_on",
"tokio",
"url",
"urlparse",
"utf16string",
"winapi",

View File

@@ -27,6 +27,7 @@ reqwest = { version = "0.11", features = ["brotli", "deflate", "gzip", "rustls-t
RustyXML = "0.3"
spin_on = "0.1.1"
tokio = { version = "1.18", features = ["rt", "macros", "rt-multi-thread", "time"] }
url = "2.2"
urlparse = "0.7"
utf16string = { version= "0.2", optional = true }

View File

@@ -7,7 +7,7 @@ use crate::data::json::JSONDataFile;
#[cfg(feature = "ugoira")]
use crate::data::video::get_video_metadata;
use crate::downloader::pd_file::enums::PdFileResult;
use crate::downloader::pd_file::file::PdFile;
use crate::downloader::pd_file::PdFile;
use crate::gettext;
use crate::opthelper::OptHelper;
use crate::pixiv_link::PixivID;

View File

@@ -0,0 +1,75 @@
use super::pd_file::PdFile;
use super::pd_file::PdFileResult;
use super::error::DownloaderError;
use crate::webclient::WebClient;
use crate::webclient::ToHeaders;
use reqwest::IntoUrl;
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use std::sync::Arc;
use std::sync::RwLock;
use url::Url;
/// A file downloader
pub struct Downloader {
/// The webclient
client: Arc<WebClient>,
/// The download status
status: Arc<PdFile>,
/// The url of the file
url: Arc<Url>,
/// The HTTP headers map
headers: Arc<HashMap<String, String>>,
/// The target file
file: RwLock<Option<File>>,
}
impl Downloader {
/// Create a new [Downloader] instance
/// * `url` - The url of the file
/// * `header` - HTTP headers
/// * `path` - The path to store downloaded file.
/// * `overwrite` - Whether to overwrite file
pub fn new<U: IntoUrl, H: ToHeaders, P: AsRef<Path> + ?Sized>(url: U, headers: H, path: Option<&P>, overwrite: Option<bool>) -> Result<Self, DownloaderError> {
let h = match headers.to_headers() {
Some(h) => { h }
None => { HashMap::new() }
};
let mut already_exists = false;
let pd_file = match path {
Some(p) => {
let p = p.as_ref();
match PdFile::open(p)? {
PdFileResult::TargetExisted => {
// #TODO
PdFile::new()
}
PdFileResult::Ok(p) => { p }
PdFileResult::ExistedOk(p) => {
already_exists = true;
p
}
}
}
None => { PdFile::new() }
};
let file = match path {
Some(p) => {
if already_exists {
Some(File::open(p)?)
} else {
Some(File::create(p)?)
}
}
None => { None }
};
Ok(Self {
client: Arc::new(WebClient::new()),
status: Arc::new(pd_file),
url: Arc::new(url.into_url()?),
headers: Arc::new(h),
file: RwLock::new(file),
})
}
}

8
src/downloader/error.rs Normal file
View File

@@ -0,0 +1,8 @@
use crate::downloader::pd_file::PdFileError;
#[derive(Debug, derive_more::From)]
pub enum DownloaderError {
ReqwestError(reqwest::Error),
PdFileError(PdFileError),
IoError(std::io::Error),
}

View File

@@ -1 +1,7 @@
/// A file downloader
pub mod downloader;
/// File downloader's error
pub mod error;
/// The pd file
pub mod pd_file;
pub use downloader::Downloader;

View File

@@ -9,3 +9,6 @@ pub mod file;
pub mod part_status;
/// Version of the pd file
pub mod version;
pub use enums::PdFileResult;
pub use error::PdFileError;
pub use file::PdFile;

View File

@@ -19,6 +19,7 @@ extern crate proc_macros;
extern crate tokio;
extern crate regex;
extern crate reqwest;
extern crate url;
extern crate urlparse;
#[cfg(feature = "utf16string")]
extern crate utf16string;

View File

@@ -1,4 +1,4 @@
use crate::downloader::pd_file::file::PdFile;
use crate::downloader::pd_file::PdFile;
use crate::ext::rw_lock::GetRwLock;
use crate::gettext;
use crate::opthelper::OptHelper;