From ad94ec1a9603c21df23df3acf2a5c08f9ec26957 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 28 May 2022 13:24:16 +0000 Subject: [PATCH] Update --- src/downloader/downloader.rs | 27 ++++++++++++++++++++++----- src/downloader/enums.rs | 10 ++++++++++ src/downloader/mod.rs | 2 ++ src/webclient.rs | 1 + 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/downloader/enums.rs diff --git a/src/downloader/downloader.rs b/src/downloader/downloader.rs index 049bb94..59db16a 100644 --- a/src/downloader/downloader.rs +++ b/src/downloader/downloader.rs @@ -1,6 +1,8 @@ use super::pd_file::PdFile; use super::pd_file::PdFileResult; +use super::enums::DownloaderResult; use super::error::DownloaderError; +use crate::utils::ask_need_overwrite; use crate::webclient::WebClient; use crate::webclient::ToHeaders; use reqwest::IntoUrl; @@ -11,6 +13,7 @@ use std::sync::Arc; use std::sync::RwLock; use url::Url; +#[derive(Debug)] /// A file downloader pub struct Downloader { /// The webclient @@ -31,7 +34,7 @@ impl Downloader { /// * `header` - HTTP headers /// * `path` - The path to store downloaded file. /// * `overwrite` - Whether to overwrite file - pub fn new + ?Sized>(url: U, headers: H, path: Option<&P>, overwrite: Option) -> Result { + pub fn new + ?Sized>(url: U, headers: H, path: Option<&P>, overwrite: Option) -> Result { let h = match headers.to_headers() { Some(h) => { h } None => { HashMap::new() } @@ -42,8 +45,22 @@ impl Downloader { let p = p.as_ref(); match PdFile::open(p)? { PdFileResult::TargetExisted => { - // #TODO - PdFile::new() + match overwrite { + Some(overwrite) => { + if !overwrite { + return Ok(DownloaderResult::Canceled); + } else { + PdFile::new() + } + } + None => { + if !ask_need_overwrite(p.to_str().unwrap()) { + return Ok(DownloaderResult::Canceled); + } else { + PdFile::new() + } + } + } } PdFileResult::Ok(p) => { p } PdFileResult::ExistedOk(p) => { @@ -64,12 +81,12 @@ impl Downloader { } None => { None } }; - Ok(Self { + Ok(DownloaderResult::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), - }) + })) } } diff --git a/src/downloader/enums.rs b/src/downloader/enums.rs new file mode 100644 index 0000000..6ace131 --- /dev/null +++ b/src/downloader/enums.rs @@ -0,0 +1,10 @@ +use super::downloader::Downloader; + +#[derive(Debug)] +/// The result when try create a new [Downloader] interface +pub enum DownloaderResult { + /// Created successfully + Ok(Downloader), + /// The target file already downloaded and overwrite is disabled. + Canceled, +} diff --git a/src/downloader/mod.rs b/src/downloader/mod.rs index 37749fd..14a7f30 100644 --- a/src/downloader/mod.rs +++ b/src/downloader/mod.rs @@ -1,5 +1,7 @@ /// A file downloader pub mod downloader; +/// The enums of the downloader +pub mod enums; /// File downloader's error pub mod error; /// The pd file diff --git a/src/webclient.rs b/src/webclient.rs index 771588f..14a6af1 100644 --- a/src/webclient.rs +++ b/src/webclient.rs @@ -83,6 +83,7 @@ pub fn gen_cookie_header(c: &WebClient, url: U) -> String { s } +#[derive(Debug)] /// A Web Client pub struct WebClient { /// Basic Web Client