diff --git a/Cargo.lock b/Cargo.lock index c35a1ae..e3c98a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -935,12 +935,12 @@ dependencies = [ [[package]] name = "ruget" -version = "0.2.2" +version = "0.3.0" dependencies = [ "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)", - "seahorse 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "seahorse 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -982,7 +982,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "seahorse" -version = "0.3.1" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1522,7 +1522,7 @@ dependencies = [ "checksum schannel 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "f2f6abf258d99c3c1c5c2131d99d064e94b7b3dd5f416483057f308fea253339" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" -"checksum seahorse 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "190dc2d49e3b603bf0640faad858ef5989b1f426833e9fa2652230e5de72aa6e" +"checksum seahorse 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "24374e880c4bc0ac6688f921b0ad7857d31c4d147d6e274014d05fd67d2e3845" "checksum security-framework 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eee63d0f4a9ec776eeb30e220f0bc1e092c3ad744b2a379e3993070364d3adc2" "checksum security-framework-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9636f8989cbf61385ae4824b98c1aaa54c994d7d8b41f11c601ed799f0549a56" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" diff --git a/Cargo.toml b/Cargo.toml index 78ed6ce..52ae673 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ruget" -version = "0.2.2" +version = "0.3.0" authors = ["ksk001100 "] edition = "2018" repository = "https://github.com/ksk001100/ruget" @@ -13,4 +13,4 @@ description = "Alternative to wget written in Rust" reqwest = "0.9" rayon = "1.1" num_cpus = "1.0" -seahorse = "0.3.1" +seahorse = "0.6.1" diff --git a/README.md b/README.md index eaaa736..b40b321 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ $ brew install ruget ```bash $ ruget https://sample-videos.com/img/Sample-png-image-30mb.png +$ ruget https://sample-videos.com/img/Sample-png-image-30mb.png --output sample.png +$ ruget https://sample-videos.com/img/Sample-png-image-30mb.png -o sample.png ``` ![screen shot2](images/screen_shot2.png) diff --git a/src/lib/download_manager.rs b/src/lib/download_manager.rs index d5b3578..0b03f9f 100644 --- a/src/lib/download_manager.rs +++ b/src/lib/download_manager.rs @@ -8,12 +8,12 @@ pub struct DownloadManager { } impl DownloadManager { - pub fn new(url: String) -> Self { + pub fn new(url: String, output_path: Option) -> Self { let downloader: Box = { if is_accept_ranges(&url) { - Box::new(ParallelDownloader::new(url)) + Box::new(ParallelDownloader::new(url, output_path)) } else { - Box::new(SingleDownloader::new(url)) + Box::new(SingleDownloader::new(url, output_path)) } }; Self { downloader } diff --git a/src/lib/downloader/parallel.rs b/src/lib/downloader/parallel.rs index a164dab..e3f4fba 100644 --- a/src/lib/downloader/parallel.rs +++ b/src/lib/downloader/parallel.rs @@ -19,13 +19,18 @@ const TMP_DIR: &str = "ruget_tmp_dir"; pub struct ParallelDownloader { pub url: String, + pub output_path: Option, pub client: Client, } impl ParallelDownloader { - pub fn new(url: String) -> Self { + pub fn new(url: String, output_path: Option) -> Self { let client = Client::new(); - Self { url, client } + Self { + url, + output_path, + client, + } } pub fn create_args(&self) -> Vec<(usize, String)> { @@ -51,10 +56,15 @@ impl ParallelDownloader { } pub fn get_filename(&self) -> &str { - let url_parse: Vec<&str> = self.url.split('/').collect(); - match url_parse.last() { - Some(name) => name, - None => panic!("cannot get file name..."), + match &self.output_path { + Some(output_path) => &output_path, + None => { + let url_parse: Vec<&str> = self.url.split('/').collect(); + match url_parse.last() { + Some(name) => name, + None => panic!("cannot get file name..."), + } + } } } @@ -107,9 +117,6 @@ impl Download for ParallelDownloader { } thread_args.into_par_iter().for_each(|arg| { - let tmp = format!("{}/{}.tmp", TMP_DIR, arg.0); - let mut file = File::create(tmp).unwrap(); - loop { let res = self .client @@ -120,6 +127,8 @@ impl Download for ParallelDownloader { match res { Ok(mut res) => { if res.status().is_success() { + let tmp = format!("{}/{}.tmp", TMP_DIR, arg.0); + let mut file = File::create(tmp).unwrap(); match res.copy_to(&mut file) { Ok(_) => break, Err(_) => continue, diff --git a/src/lib/downloader/single.rs b/src/lib/downloader/single.rs index 169faa5..38217f3 100644 --- a/src/lib/downloader/single.rs +++ b/src/lib/downloader/single.rs @@ -6,18 +6,24 @@ use crate::lib::utils::Download; pub struct SingleDownloader { pub url: String, + pub output_path: Option, } impl SingleDownloader { - pub fn new(url: String) -> Self { - Self { url } + pub fn new(url: String, output_path: Option) -> Self { + Self { url, output_path } } pub fn get_filename(&self) -> &str { - let url_parse: Vec<&str> = self.url.split('/').collect(); - match url_parse.last() { - Some(name) => name, - None => panic!("cannot get file name..."), + match &self.output_path { + Some(output_path) => &output_path, + None => { + let url_parse: Vec<&str> = self.url.split('/').collect(); + match url_parse.last() { + Some(name) => name, + None => panic!("cannot get file name..."), + } + } } } } diff --git a/src/main.rs b/src/main.rs index 1efb91d..7e6e504 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,11 @@ mod lib; use std::{env, process::exit}; use rayon::ThreadPoolBuilder; -use seahorse::{Action, SingleApp, color}; +use seahorse::{color, Action, App, Flag, FlagType}; use lib::download_manager::DownloadManager; -const DISPLAY_NAME: &'static str = " +const NAME: &'static str = " _ | | _ __ _ _ __ _ ___| |_ @@ -24,25 +24,36 @@ fn main() { .unwrap(); let args: Vec = env::args().collect(); - let action: Action = |v: Vec| { - let url = match v.len() { - 1 => &v[0], + let action: Action = |c| { + let url = match c.args.len() { + 1 => &c.args[0], _ => { eprintln!("Please specify a URL..."); exit(1); } }; - let download_manager = DownloadManager::new(url.to_owned()); + let output = &c.string_flag("output"); + + let download_manager = DownloadManager::new(url.to_owned(), output.to_owned()); download_manager.downloader.download(); }; - let app = SingleApp::new() - .name("ruget") - .display_name(color::red(DISPLAY_NAME)) + let app = App::new() + .name(color::red(NAME)) .usage("ruget [url]") + .author(env!("CARGO_PKG_AUTHORS")) + .description(env!("CARGO_PKG_DESCRIPTION")) .version(env!("CARGO_PKG_VERSION")) - .action(action); + .action(action) + .flag( + Flag::new( + "output", + "[--output, -o]: ruget [url] --output [file name]", + FlagType::String, + ) + .alias("o"), + ); app.run(args); -} \ No newline at end of file +}