Skip to content

Instantly share code, notes, and snippets.

@giuliano-macedo
Last active July 13, 2025 13:56
Show Gist options
  • Save giuliano-macedo/4d11d6b3bb003dba3a1b53f43d81b30d to your computer and use it in GitHub Desktop.
Save giuliano-macedo/4d11d6b3bb003dba3a1b53f43d81b30d to your computer and use it in GitHub Desktop.

Revisions

  1. giuliano-macedo revised this gist Feb 10, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion download_file.rs
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ pub async fn download_file(client: &Client, url: &str, path: &str) -> Result<(),

    while let Some(item) = stream.next().await {
    let chunk = item.or(Err(format!("Error while downloading file")))?;
    file.write(&chunk)
    file.write_all(&chunk)
    .or(Err(format!("Error while writing to file")))?;
    let new = min(downloaded + (chunk.len() as u64), total_size);
    downloaded = new;
  2. giuliano-macedo revised this gist Apr 21, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions download_file.rs
    Original file line number Diff line number Diff line change
    @@ -6,11 +6,11 @@ use std::cmp::min;
    use std::fs::File;
    use std::io::Write;

    use reqwest::Client;
    use indicatif::{ProgressBar, ProgressStyle};

    use futures_util::StreamExt;

    pub async fn download_file(client: &reqwest::Client, url: &str, path: &str) -> Result<(), String> {
    pub async fn download_file(client: &Client, url: &str, path: &str) -> Result<(), String> {
    // Reqwest setup
    let res = client
    .get(url)
  3. giuliano-macedo revised this gist Apr 21, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions download_file.rs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // you need this in your cargo.toml
    // reqwest = { version = "0.11.3", features = ["stream"] }
    // futures-util = "0.3.14"
    // indicatif = "0.15.0"
    use std::cmp::min;
    use std::fs::File;
    use std::io::Write;
  4. giuliano-macedo revised this gist Apr 21, 2021. No changes.
  5. giuliano-macedo created this gist Apr 21, 2021.
    43 changes: 43 additions & 0 deletions download_file.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    use std::cmp::min;
    use std::fs::File;
    use std::io::Write;

    use indicatif::{ProgressBar, ProgressStyle};

    use futures_util::StreamExt;

    pub async fn download_file(client: &reqwest::Client, url: &str, path: &str) -> Result<(), String> {
    // Reqwest setup
    let res = client
    .get(url)
    .send()
    .await
    .or(Err(format!("Failed to GET from '{}'", &url)))?;
    let total_size = res
    .content_length()
    .ok_or(format!("Failed to get content length from '{}'", &url))?;

    // Indicatif setup
    let pb = ProgressBar::new(total_size);
    pb.set_style(ProgressStyle::default_bar()
    .template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")
    .progress_chars("#>-"));
    pb.set_message(&format!("Downloading {}", url));

    // download chunks
    let mut file = File::create(path).or(Err(format!("Failed to create file '{}'", path)))?;
    let mut downloaded: u64 = 0;
    let mut stream = res.bytes_stream();

    while let Some(item) = stream.next().await {
    let chunk = item.or(Err(format!("Error while downloading file")))?;
    file.write(&chunk)
    .or(Err(format!("Error while writing to file")))?;
    let new = min(downloaded + (chunk.len() as u64), total_size);
    downloaded = new;
    pb.set_position(new);
    }

    pb.finish_with_message(&format!("Downloaded {} to {}", url, path));
    return Ok(());
    }