diff options
| author | Naz <ndpm13@ch-naseem.com> | 2025-08-05 14:10:43 +0100 |
|---|---|---|
| committer | Naz <ndpm13@ch-naseem.com> | 2025-08-05 14:10:43 +0100 |
| commit | 137ef7705cda9b2e6f51e86c6a100b089d9060ff (patch) | |
| tree | 9e91fa2df1d6c165c1d70a0681461bac18f31cb1 | |
| parent | 896282dc598f54b1e8b9169a74a344ba23f580ea (diff) | |
🐛fix: download to a temporary path that will be removed on failure or renamed on success
| -rw-r--r-- | src/downloader.rs | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/downloader.rs b/src/downloader.rs index e40d5d1..f402c4e 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -60,6 +60,8 @@ impl Downloader { pub async fn download_with_progress(&self, url: &str, path: &PathBuf) -> Result<()> { fs::create_dir_all(&appimages_dir()?).await?; + let temp_path = PathBuf::from(format!("{}.part", path.display())); + let resp = reqwest::get(&url.to_string()) .await .map_err(|source| Error::Download { @@ -72,15 +74,21 @@ impl Downloader { let total_size = resp.content_length().unwrap_or(0); let bar = make_progress_bar(total_size)?; - let mut out = tokio::fs::File::create(&path).await?; + let mut out = tokio::fs::File::create(&temp_path).await?; // Stream download with progress updates let mut stream = resp.bytes_stream(); while let Some(chunk) = stream.next().await { - let chunk = chunk.map_err(|source| Error::Download { - url: url.to_string(), - source, - })?; + let chunk = match chunk { + Ok(chunk) => chunk, + Err(source) => { + fs::remove_file(temp_path).await?; + return Err(Error::Download { + url: url.to_string(), + source, + }); + } + }; let len = chunk.len() as u64; out.write_all(&chunk).await?; bar.inc(len); @@ -88,6 +96,8 @@ impl Downloader { bar.finish_with_message("Download complete!"); + fs::rename(temp_path, path).await?; + // Make executable #[cfg(unix)] { |
