From bd8adb9f0193f1482ca866b06cf133ff68fd9a8e Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 29 Jul 2025 13:46:43 +0100 Subject: =?UTF-8?q?=F0=9F=94=A7refactor:=20remove=20types.rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/downloader.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/downloader.rs (limited to 'src/downloader.rs') diff --git a/src/downloader.rs b/src/downloader.rs new file mode 100644 index 0000000..c40b0b4 --- /dev/null +++ b/src/downloader.rs @@ -0,0 +1,49 @@ +use futures_util::StreamExt; +use std::path::PathBuf; +use tokio::{fs, io::AsyncWriteExt}; + +use crate::{appimages_dir, make_progress_bar}; + +#[derive(Debug, Default)] +pub struct Downloader {} + +impl Downloader { + pub fn new() -> Self { + Self {} + } + pub async fn download_with_progress( + &self, + url: &str, + path: &PathBuf, + ) -> Result<(), Box> { + fs::create_dir_all(&appimages_dir()).await?; + + let resp = reqwest::get(&url.to_string()).await?; + 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?; + + // Stream download with progress updates + let mut stream = resp.bytes_stream(); + while let Some(chunk) = stream.next().await { + let chunk = chunk?; + let len = chunk.len() as u64; + out.write_all(&chunk).await?; + bar.inc(len); + } + + bar.finish_with_message("Download complete!"); + + // Make executable + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mut perms = fs::metadata(&path).await?.permissions(); + perms.set_mode(0o755); + fs::set_permissions(&path, perms).await?; + } + + Ok(()) + } +} -- cgit v1.2.3 From cd49bc9362c0fef2534948d368c8c52460558a77 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 29 Jul 2025 16:26:28 +0100 Subject: =?UTF-8?q?=F0=9F=94=A7refactor:=20move=20the=20path=20preparing?= =?UTF-8?q?=20logic=20to=20Downloader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/downloader.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/downloader.rs') diff --git a/src/downloader.rs b/src/downloader.rs index c40b0b4..2196e25 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -11,6 +11,15 @@ impl Downloader { pub fn new() -> Self { Self {} } + pub fn prepare_path(&self, url: &str, executable: &str) -> 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) + } pub async fn download_with_progress( &self, url: &str, -- cgit v1.2.3