Skip to content

Commit

Permalink
Merge pull request #9 from lsk569937453/dev
Browse files Browse the repository at this point in the history
Update the downloading file.
  • Loading branch information
lsk569937453 authored May 11, 2024
2 parents f37bea8 + f8b092b commit 077c04b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
tokio = { version = "1.32.0", features = ["full"] }
clap = { version = "4.4.1", features = ["derive"] }
clap = { version = "4.4.1", features = ["derive", "string"] }

hyper = { version = "1.3.1", features = ["full","tracing"] }
hyper = { version = "1.3.1", features = ["full", "tracing"] }
hyper-util = { version = "0.1", features = ["full"] }
http-body-util = { version = "0.1" }

futures = "0.3.29"
serde = {version="1.0.190",features = ["derive"]}
serde = { version = "1.0.190", features = ["derive"] }
serde_json = "1.0.108"

openssl = { version = "0.10", features = ["vendored"] }
indicatif = "0.17.7"
http = "0.2"
chrono = "0.4.31"
tracing= "0.1.4"
tracing-subscriber= "0.3.18"
tracing = "0.1.4"
tracing-subscriber = "0.3.18"
tracing-appender = "0.2.3"
bytes = "1"
rustls = { version = "0.22.1" ,features = [ "logging" ]}
rustls = { version = "0.22.1", features = ["logging"] }
pki-types = { package = "rustls-pki-types", version = "1" }
tokio-rustls = "0.25.0"
rustls-pemfile = "2"
webpki-roots = "0.26.0"

futures-util = { version = "0.3.1", default-features = false }
tokio-util = { version = "0.7.7", features = ["full", "time"] }
33 changes: 19 additions & 14 deletions src/http/handler.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use bytes::buf;
use bytes::Bytes;
use futures::StreamExt;
use futures_util::TryStreamExt;
use http_body_util::BodyExt;
use http_body_util::BodyStream;
use hyper::body::Incoming;
use hyper::http::header::CONTENT_LENGTH;
use hyper::Response;

use http_body_util::StreamBody;
use hyper::body::Frame;
use hyper::{body::Buf, Request};
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use std::convert::Infallible;
use std::fs::OpenOptions;
use std::io::Write as WriteStd;
use std::{cmp::min, fmt::Write};
use tokio::io::AsyncReadExt;
use tokio::io::BufReader;
use tokio::io::{self, AsyncWriteExt};
pub async fn handle_response(
file_path_option: Option<String>,
res: Response<Incoming>,
Expand All @@ -22,37 +30,34 @@ pub async fn handle_response(
.to_str()?
.parse::<u64>()?;

let mut body = res.collect().await?.aggregate();

if let Some(file_path) = file_path_option {
let incoming = res.into_body();
let mut body_streaming = BodyStream::new(incoming);
let mut downloaded = 0;
let total_size = content_length;
let mut file = OpenOptions::new()
.create_new(true)
.append(true)
.write(true)
.create(true)
.open(file_path)?;
let pb = ProgressBar::new(total_size);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")
?
.progress_chars("#>-"));

loop {
if body.remaining() == 0 {
break;
while let Some(Ok(t)) = body_streaming.next().await {
if let Ok(bytes) = t.into_data() {
let new = min(downloaded + bytes.len() as u64, total_size);
downloaded = new;
pb.set_position(new);
file.write_all(&bytes)?;
}
let bytes = body.chunk();
let new = min(downloaded + bytes.len() as u64, total_size);
downloaded = new;
pb.set_position(new);
file.write_all(&bytes)?;
}

pb.finish_with_message("downloaded");
} else {
if content_length > 1024 * 1024 * 100 {
return Err(anyhow!("The content_length is large than 100MB!"));
}

let mut body = res.collect().await?.aggregate();
let dst = body.copy_to_bytes(content_length as usize);
let response_string = String::from_utf8_lossy(&dst);
println!("{}", response_string);
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;
extern crate anyhow;
#[macro_use]
extern crate tracing;
use clap::builder::Str;
use clap::Parser;
use hyper::header::{HeaderName, HeaderValue};
use hyper::Request;
Expand Down Expand Up @@ -168,7 +169,7 @@ struct Cli {
certificate_path_option: Option<String>,

/// The downloading file path .
#[arg(short = 'o', long)]
#[arg(global = true, short = 'O', long, default_missing_value = "none")]
file_path_option: Option<String>,

/// Skip certificate validation.
Expand Down Expand Up @@ -342,6 +343,7 @@ async fn do_request(cli: Cli) -> Result<(), anyhow::Error> {
println!("< {}: {}", key, value.to_str()?);
}
}
println!("handle response");
handle_response(cli.file_path_option, res).await?;
return Ok(());
}
Expand Down

0 comments on commit 077c04b

Please sign in to comment.