blob: be35bd80ba2905f8a14671a878c4c0cc6c395f66 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
use futures_util::StreamExt;
use std::path::PathBuf;
use tokio::{fs, io::AsyncWriteExt};
use crate::{Result, appimages_dir, make_progress_bar};
#[derive(Debug, Default)]
pub struct Downloader {}
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, path: &PathBuf) -> Result<()> {
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(())
}
}
|