diff --git a/Cargo.lock b/Cargo.lock index c61512a..214736e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,7 +330,7 @@ dependencies = [ [[package]] name = "mangafetchi" -version = "0.1.0" +version = "0.1.1" dependencies = [ "quick-xml", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index a3b55e1..ce20ee5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mangafetchi" -version = "0.1.0" +version = "0.1.1" authors = ["blank X "] edition = "2018" @@ -14,4 +14,4 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" reqwest = "0.11" quick-xml = "0.20" -tokio = { version = "1.0", features = ["rt-multi-thread", "sync"] } +tokio = { version = "1.0", features = ["rt-multi-thread", "sync", "time"] } diff --git a/src/commands/download.rs b/src/commands/download.rs index b6cf9ac..4b34b0b 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -7,10 +7,12 @@ use std::process::exit; use std::path::{Path, PathBuf}; use tokio::sync::Mutex; use tokio::task::JoinHandle; +use tokio::time::{sleep, Duration}; extern crate tokio; extern crate reqwest; const DOWNLOAD_WORKERS: usize = 5; +const NON_IMAGE_WAIT_TIME: u64 = 5000; pub async fn run(mut args: env::Args) { let manga_id = match args.next() { @@ -90,7 +92,12 @@ async fn summon_handles(client: reqwest::Client, mutex: Arc Vec { pages } -pub async fn download_file(client: reqwest::Client, url: &str, file_name: &PathBuf, referer: &str) -> Result<(), reqwest::Error> { - let bytes = client.get(url) +pub async fn download_file(client: reqwest::Client, url: &str, file_name: &PathBuf, referer: &str) -> Result { + let resp = client.get(url) .header("Referer", referer) .send() - .await? - .bytes() .await?; - if !file_name.parent().unwrap().is_dir() { - create_dir(file_name.parent().unwrap()).unwrap(); + match resp.headers().get("Content-Type") { + Some(header_value) => { + if header_value.to_str().unwrap().starts_with("image/") { + let bytes = resp.bytes().await?; + if !file_name.parent().unwrap().is_dir() { + create_dir(file_name.parent().unwrap()).unwrap(); + } + let mut file = File::create(&file_name).unwrap(); + file.write_all(&bytes).unwrap(); + return Ok(true); + } + return Ok(false); + }, + None => Ok(false) } - let mut file = File::create(&file_name).unwrap(); - file.write_all(&bytes).unwrap(); - Ok(()) }