diff options
Diffstat (limited to 'src/downloader.rs')
| -rw-r--r-- | src/downloader.rs | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/downloader.rs b/src/downloader.rs index 2196e25..0141424 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -2,7 +2,7 @@ use futures_util::StreamExt; use std::path::PathBuf; use tokio::{fs, io::AsyncWriteExt}; -use crate::{appimages_dir, make_progress_bar}; +use crate::{Error, Result, appimages_dir, make_progress_bar}; #[derive(Debug, Default)] pub struct Downloader {} @@ -11,32 +11,36 @@ impl Downloader { pub fn new() -> Self { Self {} } - pub fn prepare_path(&self, url: &str, executable: &str) -> PathBuf { + pub fn prepare_path(&self, url: &str, executable: &str) -> Result<PathBuf> { // Try to extract filename from URL or use default let filename = match url.split('/').next_back() { Some(name) => name.to_string(), None => format!("{executable}.AppImage"), }; - appimages_dir().join(filename) + Ok(appimages_dir()?.join(filename)) } - pub async fn download_with_progress( - &self, - url: &str, - path: &PathBuf, - ) -> Result<(), Box<dyn std::error::Error>> { - fs::create_dir_all(&appimages_dir()).await?; - - let resp = reqwest::get(&url.to_string()).await?; + pub async fn download_with_progress(&self, url: &str, path: &PathBuf) -> Result<()> { + fs::create_dir_all(&appimages_dir()?).await?; + + let resp = reqwest::get(&url.to_string()) + .await + .map_err(|source| Error::Download { + url: url.to_string(), + source, + })?; let total_size = resp.content_length().unwrap_or(0); - let bar = make_progress_bar(total_size); + let bar = make_progress_bar(total_size)?; let mut out = tokio::fs::File::create(&path).await?; // Stream download with progress updates let mut stream = resp.bytes_stream(); while let Some(chunk) = stream.next().await { - let chunk = chunk?; + let chunk = chunk.map_err(|source| Error::Download { + url: url.to_string(), + source, + })?; let len = chunk.len() as u64; out.write_all(&chunk).await?; bar.inc(len); |
