Skip to content

Commit

Permalink
Added retry for getting content-length
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanuk12 committed May 28, 2023
1 parent 25922a6 commit e6c259a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ indicatif = "0.17.3"
futures-util = "0.3.28"
tar = "0.4.38"
flate2 = "1.0.26"
async-recursion = "1.0.4"
14 changes: 11 additions & 3 deletions src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use reqwest::{
};
use futures_util::StreamExt;
use clap::ValueEnum;
use async_recursion::async_recursion;

use crate::types::GitHubReleases;

Expand Down Expand Up @@ -58,18 +59,25 @@ impl ServerTrait for Servers {
}

// Downloads a file
#[async_recursion]
pub async fn download_file(client: &Client, url: &str) -> Result<Vec<u8>, String> {
// Initialise the request
let response = client
.get(url)
.send()
.await
.expect(&format!("Failed to GET from '{}'", &url));
let total_size = response
.content_length()
.expect(&format!("Failed to get content length from '{}'", &url));
let total_size_r = response
.content_length();

// Check we got the size
if total_size_r.is_none() {
println!("failed to get content-length of {}, retrying...", url);
return download_file(client, url).await;
}

// Indicatif setup
let total_size = total_size_r.unwrap();
let pb = ProgressBar::new(total_size);
pb.set_style(
ProgressStyle::default_bar()
Expand Down

0 comments on commit e6c259a

Please sign in to comment.