From 0afc09cfc003c63f6293903d792fcf9f3c107f04 Mon Sep 17 00:00:00 2001 From: Petr Gadorek Date: Wed, 26 Jun 2024 13:19:19 +0200 Subject: [PATCH] skipping downloads if file already present and sha checksum checks --- Cargo.lock | 3 ++- src/wizard/mod.rs | 35 +++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5d0c14..e8fc42f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1698,7 +1698,7 @@ dependencies = [ [[package]] name = "idf-im-lib" version = "0.1.0" -source = "git+https://github.com/espressif/idf-im-lib.git?branch=master#2598c4a2cb520599063ec962ff6856f7d77aa616" +source = "git+https://github.com/espressif/idf-im-lib.git?branch=master#8b5fbd2813f2aa851728067e92bb9e9503288c43" dependencies = [ "colored", "decompress", @@ -1710,6 +1710,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "sha2", "sys-info", "tokio", ] diff --git a/src/wizard/mod.rs b/src/wizard/mod.rs index abc7118..49e48df 100644 --- a/src/wizard/mod.rs +++ b/src/wizard/mod.rs @@ -194,22 +194,42 @@ async fn download_tools( let mut downloaded_tools: Vec = vec![]; for (tool_name, download_link) in download_links.iter() { println!("Downloading tool: {}", tool_name); - let progress_bar = ProgressBar::new(100); + let progress_bar = ProgressBar::new(download_link.size); progress_bar.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 update_progress = |amount_downloaded: u64, total_size: u64| { - let current_progress = ((amount_downloaded as f64) / (total_size as f64)) * 100.0; - progress_bar.set_position(current_progress as u64); + // let current_progress = ((amount_downloaded as f64) / (total_size as f64)) * 100.0; + progress_bar.set_position(amount_downloaded); }; - println!("Download link: {}", download_link); + println!("Download link: {}", download_link.url); println!("destination: {}", destination_path); - match idf_im_lib::download_file(download_link, destination_path, &update_progress).await { + let file_path = Path::new(&download_link.url); + let filename: &str = file_path.file_name().unwrap().to_str().unwrap(); + + let full_file_path = Path::new(&destination_path).join(Path::new(filename)); + match idf_im_lib::verify_file_checksum( + &download_link.sha256, + full_file_path.to_str().unwrap(), + ) { + Ok(true) => { + downloaded_tools.push(filename.to_string()); // add it to the list for extraction even if it's already downloaded + println!("The file is already downloaded and the checksum matches."); + progress_bar.finish(); + continue; + } + _ => { + println!("The checksum does not match or file was not avalible."); + // TODO: move to debug + } + } + + match idf_im_lib::download_file(&download_link.url, destination_path, &update_progress) + .await + { Ok(_) => { - let file_path = Path::new(download_link); - let filename: &str = file_path.file_name().unwrap().to_str().unwrap(); downloaded_tools.push(filename.to_string()); progress_bar.finish(); println!("Downloaded {}", tool_name); @@ -220,7 +240,6 @@ async fn download_tools( panic!(); } } - // TODO: check sha256 of downloaded files } downloaded_tools }