????
This commit is contained in:
parent
c30b835167
commit
6095dc48fa
@ -7,10 +7,7 @@ authors = ["Smart SangGe <liangjunyong06@gmail.com>"]
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
<<<<<<< HEAD
|
|
||||||
clap = "2.33.2"
|
clap = "2.33.2"
|
||||||
indicatif = "0.15"
|
indicatif = "0.15"
|
||||||
console = "0.14"
|
console = "0.14"
|
||||||
=======
|
reqwest = { version = "0.11", features = ["blocking"] }
|
||||||
clap = "2.26.0"
|
|
||||||
>>>>>>> parent of 4ca1b62 (feat: finish all func)
|
|
||||||
|
74
src/main.rs
74
src/main.rs
@ -1,19 +1,19 @@
|
|||||||
extern crate clap;
|
extern crate clap;
|
||||||
<<<<<<< HEAD
|
|
||||||
extern crate indicatif;
|
extern crate indicatif;
|
||||||
extern crate console;
|
extern crate console;
|
||||||
|
|
||||||
use clap::{Arg, App};
|
use clap::{Arg, App};
|
||||||
use indicatif::{ProgressBar, ProgressStyle, HumanBytes};
|
use indicatif::{ProgressBar, ProgressStyle, HumanBytes};
|
||||||
use console::style;
|
use console::style;
|
||||||
|
use std::io::Read;
|
||||||
|
use reqwest::header::{CONTENT_LENGTH, CONTENT_TYPE};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
use reqwest::blocking::Client;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
=======
|
|
||||||
|
|
||||||
use clap::{Arg, App};
|
|
||||||
>>>>>>> parent of 4ca1b62 (feat: finish all func)
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = App::new("Rget")
|
let matches = App::new("Rget")
|
||||||
@ -26,8 +26,12 @@ fn main() {
|
|||||||
.index(1)
|
.index(1)
|
||||||
.help("url to download"))
|
.help("url to download"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
let url = matches.value_of("URL").unwrap();
|
let url = matches.value_of("URL").unwrap();
|
||||||
println!("{}", url);
|
|
||||||
|
if let Err(e) = download(url, false) {
|
||||||
|
eprintln!("Download error: {}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_progress_bar(quiet_mode: bool, msg: &str, length: Option<u64>) -> ProgressBar {
|
fn create_progress_bar(quiet_mode: bool, msg: &str, length: Option<u64>) -> ProgressBar {
|
||||||
@ -53,41 +57,54 @@ fn create_progress_bar(quiet_mode: bool, msg: &str, length: Option<u64>) -> Prog
|
|||||||
bar
|
bar
|
||||||
}
|
}
|
||||||
|
|
||||||
fn download(target: &str, quiet_mode: bool) -> Result<(), Box<::std::error::Error>> {
|
fn download(target: &str, quiet_mode: bool) -> Result<(), Box<dyn::std::error::Error>> {
|
||||||
|
|
||||||
// parse url
|
// parse url
|
||||||
let url = parse_url(target)?;
|
let url = parse_url(target)?;
|
||||||
let client = Client::new().unwrap();
|
let client = reqwest::blocking::Client::new();
|
||||||
let mut resp = client.get(url)?
|
let mut resp = client.get(url).send()?;
|
||||||
.send()
|
|
||||||
.unwrap();
|
|
||||||
print(format!("HTTP request sent... {}",
|
|
||||||
style(format!("{}", resp.status())).green()),
|
if !quiet_mode {
|
||||||
quiet_mode);
|
println!("HTTP request sent... {}", style(resp.status()).green());
|
||||||
|
}
|
||||||
|
|
||||||
if resp.status().is_success() {
|
if resp.status().is_success() {
|
||||||
|
|
||||||
let headers = resp.headers().clone();
|
let headers = resp.headers();
|
||||||
let ct_len = headers.get::<ContentLength>().map(|ct_len| **ct_len);
|
|
||||||
|
|
||||||
let ct_type = headers.get::<ContentType>().unwrap();
|
let ct_len = resp.headers().get(CONTENT_LENGTH)
|
||||||
|
.and_then(|ct_len| ct_len.to_str().ok())
|
||||||
|
.and_then(|ct_len_str| ct_len_str.parse::<u64>().ok());
|
||||||
|
|
||||||
|
let ct_type = resp.headers().get(CONTENT_TYPE)
|
||||||
|
.and_then(|ct_type| ct_type.to_str().ok());
|
||||||
|
|
||||||
match ct_len {
|
match ct_len {
|
||||||
Some(len) => {
|
Some(len) => {
|
||||||
print(format!("Length: {} ({})",
|
if !quiet_mode {
|
||||||
|
println!("Length: {} ({})",
|
||||||
style(len).green(),
|
style(len).green(),
|
||||||
style(format!("{}", HumanBytes(len))).red()),
|
style(HumanBytes(len)).red());
|
||||||
quiet_mode);
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
print(format!("Length: {}", style("unknown").red()), quiet_mode);
|
if !quiet_mode {
|
||||||
|
println!("Length: {}", style("unknown").red());
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
print(format!("Type: {}", style(ct_type).green()), quiet_mode);
|
// Print content type
|
||||||
|
custom_print(format!("Type: {}", style(ct_type).green()), quiet_mode);
|
||||||
|
|
||||||
|
// Get the filename from the URL
|
||||||
let fname = target.split("/").last().unwrap();
|
let fname = target.split("/").last().unwrap();
|
||||||
|
|
||||||
print(format!("Saving to: {}", style(fname).green()), quiet_mode);
|
// Print saving message
|
||||||
|
custom_print(format!("Saving to: {}", style(fname).green()), quiet_mode);
|
||||||
|
|
||||||
|
|
||||||
let chunk_size = match ct_len {
|
let chunk_size = match ct_len {
|
||||||
Some(x) => x as usize / 99,
|
Some(x) => x as usize / 99,
|
||||||
@ -121,3 +138,16 @@ fn download(target: &str, quiet_mode: bool) -> Result<(), Box<::std::error::Erro
|
|||||||
Ok(())
|
Ok(())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn save_to_file(data: &[u8], filename: &str) -> io::Result<()> {
|
||||||
|
let mut file = File::create(filename)?;
|
||||||
|
file.write_all(data)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn custom_print(message: String, quiet_mode: bool) {
|
||||||
|
if !quiet_mode {
|
||||||
|
println!("{}", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user