ketcindyinstaller/worker/
downloader.rs1use std::path::PathBuf;
2use anyhow::Result;
3use std::io::Write;
4
5pub(super) async fn download_file<F>(
6 source: url::Url,
7 destination: PathBuf,
8 mut progress_callback: F
9) -> Result<PathBuf>
10where
11 F: FnMut(f32) + Send,
12{
13 let mut response = reqwest::get(source).await?.error_for_status()?;
15 let total_size = response.content_length();
16 let mut downloaded_size = 0f32;
17
18 let mut file = std::fs::File::create(&destination)?;
20
21 while let Some(chunk) = response.chunk().await? {
23 file.write_all(&chunk)?;
24 downloaded_size += chunk.len() as f32;
25
26 if let Some(total_size) = total_size {
28 progress_callback(downloaded_size / total_size as f32);
29 }
30 }
31
32 Ok(destination)
33}