Skip to content

Commit

Permalink
fix file size
Browse files Browse the repository at this point in the history
  • Loading branch information
mankong committed Nov 4, 2024
1 parent e2517f0 commit e2a0b16
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ unstable = []
anyhow = "1"
chrono = "0.4"
indicatif = "0.17"
log = "0.4.20"
reqwest = {version = "0.12", default-features = false, features = [
"rustls-tls",
"json",
]}
tokio = {version = "1", features = ["fs", "macros", "rt-multi-thread"]}
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ use siwi_download::{
download::{Download, DownloadOptions},
error::AnyResult,
};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> AnyResult<()> {
let url = "https://nodejs.org/dist/v22.11.0/node-v22.11.0.pkg";
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let url = "https://npmmirror.com/mirrors/node/v20.18.0/node-v20.18.0.pkg";
let mut storage_path = std::env::current_dir()?;
storage_path.push("storage");
let storage_path = storage_path.to_str().unwrap();
Expand All @@ -66,7 +73,7 @@ async fn main() -> AnyResult<()> {
headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")?);
options
.set_headers(headers)
.set_file_name("node-v22.11.0.pkg")
.set_file_name("node-v20.18.0.pkg")
.set_show_progress(true);

let download = Download::new(storage_path);
Expand All @@ -77,6 +84,7 @@ async fn main() -> AnyResult<()> {
Ok(())
}


```

- Write a CLI tool
Expand Down
11 changes: 9 additions & 2 deletions examples/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ use siwi_download::{
download::{Download, DownloadOptions},
error::AnyResult,
};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> AnyResult<()> {
let url = "https://nodejs.org/dist/v22.11.0/node-v22.11.0.pkg";
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let url = "https://npmmirror.com/mirrors/node/v20.18.0/node-v20.18.0.pkg";
let mut storage_path = std::env::current_dir()?;
storage_path.push("storage");
let storage_path = storage_path.to_str().unwrap();
Expand All @@ -14,7 +21,7 @@ async fn main() -> AnyResult<()> {
headers.insert(USER_AGENT, HeaderValue::from_str("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")?);
options
.set_headers(headers)
.set_file_name("node-v22.11.0.pkg")
.set_file_name("node-v20.18.0.pkg")
.set_show_progress(true);

let download = Download::new(storage_path);
Expand Down
36 changes: 22 additions & 14 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ use crate::{
utils::{create_dir_all, date, get_file_name_from_url, get_file_size, is_dir},
};
use chrono::{DateTime, Utc};
use indicatif::{ProgressBar, ProgressStyle};
use log::info;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use reqwest::header::CONTENT_LENGTH;

use reqwest::header::{HeaderMap, HeaderValue, RANGE};
use std::fmt::Write;
use std::{borrow::Cow, path::Path};
use tokio::{
fs,
io::AsyncWriteExt,
time::{sleep, Duration},
};
use tracing::{error, info};

#[derive(Debug)]
pub struct DownloadOptions<'a> {
pub maybe_file_name: Option<Cow<'a, str>>,
Expand Down Expand Up @@ -186,8 +190,8 @@ impl<'a> Download<'a> {
pub async fn auto_create_storage_path(&self) -> AnyResult<()> {
if !is_dir(self.storage_path.as_ref())? {
match create_dir_all(self.storage_path.as_ref()).await {
Ok(()) => println!("create storage_path {}", &self.storage_path),
Err(e) => eprint!("create storage_path err {:?}", e),
Ok(()) => info!("create storage_path {}", &self.storage_path),
Err(e) => error!("create storage_path err {:?}", e),
}
}
Ok(())
Expand Down Expand Up @@ -242,7 +246,7 @@ impl<'a> Download<'a> {
.default_headers(headers)
.build()?,
};
// head 获取文件大小
// head get file size
let try_times_limit: u16 = 5;
let mut this_time: u16 = 0;

Expand Down Expand Up @@ -275,20 +279,24 @@ impl<'a> Download<'a> {
}
}

let content_length = match resp.content_length() {
Some(len) => len,
None => 0,
};
let mut content_length = 0;
if let Some(hv) = resp.headers().get(CONTENT_LENGTH) {
if let Ok(l) = hv.to_str() {
info!("Content-Length: {}", l);
if let Ok(l) = l.parse::<u64>() {
content_length = l;
}
}
}

let total = file_size + content_length;
report.set_file_size(total);
let pb = ProgressBar::new(total);
if options.show_progress {
pb.set_style(
ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")?
.progress_chars("#>-"),
);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
}

let mut resp = client.get(url.as_ref()).send().await?;
Expand Down

0 comments on commit e2a0b16

Please sign in to comment.