Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ksk001100 committed Feb 25, 2020
1 parent 7ab206e commit d32cc86
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 35 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ruget"
version = "0.2.2"
version = "0.3.0"
authors = ["ksk001100 <[email protected]>"]
edition = "2018"
repository = "https://github.com/ksk001100/ruget"
Expand All @@ -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"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/download_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub struct DownloadManager {
}

impl DownloadManager {
pub fn new(url: String) -> Self {
pub fn new(url: String, output_path: Option<String>) -> Self {
let downloader: Box<dyn Download> = {
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 }
Expand Down
27 changes: 18 additions & 9 deletions src/lib/downloader/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ const TMP_DIR: &str = "ruget_tmp_dir";

pub struct ParallelDownloader {
pub url: String,
pub output_path: Option<String>,
pub client: Client,
}

impl ParallelDownloader {
pub fn new(url: String) -> Self {
pub fn new(url: String, output_path: Option<String>) -> Self {
let client = Client::new();
Self { url, client }
Self {
url,
output_path,
client,
}
}

pub fn create_args(&self) -> Vec<(usize, String)> {
Expand All @@ -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..."),
}
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
18 changes: 12 additions & 6 deletions src/lib/downloader/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ use crate::lib::utils::Download;

pub struct SingleDownloader {
pub url: String,
pub output_path: Option<String>,
}

impl SingleDownloader {
pub fn new(url: String) -> Self {
Self { url }
pub fn new(url: String, output_path: Option<String>) -> 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..."),
}
}
}
}
}
Expand Down
33 changes: 22 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "
_
| |
_ __ _ _ __ _ ___| |_
Expand All @@ -24,25 +24,36 @@ fn main() {
.unwrap();

let args: Vec<String> = env::args().collect();
let action: Action = |v: Vec<String>| {
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);
}
}

0 comments on commit d32cc86

Please sign in to comment.