/ linuxy / src-tauri / src / downloader.rs
Swadhin Biswas Ci/improve release notes (#41) · 8/28/2026
04d550f
65 lines · 2.0 KB
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
55
56
57
58
59
60
61
62
63
64
65
use futures_util::StreamExt;
use tauri::Emitter;
use tokio::io::AsyncWriteExt;

use crate::installer;

#[tauri::command]
pub async fn download_and_install(
    app_handle: tauri::AppHandle,
    url: String,
    name: String,
) -> Result<String, String> {
    if !(url.starts_with("https://") || url.starts_with("http://")) {
        return Err("Only http:// and https:// downloads are allowed.".into());
    }

    let _ = app_handle.emit("install-progress", format!("Downloading {}...", name));

    let response = reqwest::get(&url).await.map_err(|e| e.to_string())?;
    let status = response.status();
    if !status.is_success() {
        return Err(format!("Download failed with HTTP status {}.", status));
    }

    let total_size = response.content_length().unwrap_or(0);
    let temp_path = std::env::temp_dir().join(format!("{}.AppImage", uuid::Uuid::new_v4()));
    let mut file = tokio::fs::File::create(&temp_path)
        .await
        .map_err(|e| e.to_string())?;
    let mut downloaded: u64 = 0;
    let mut stream = response.bytes_stream();

    while let Some(item) = stream.next().await {
        let chunk = item.map_err(|e| e.to_string())?;
        if chunk.is_empty() {
            continue;
        }
        file.write_all(&chunk).await.map_err(|e| e.to_string())?;
        downloaded += chunk.len() as u64;

        if total_size > 0 {
            let percentage = (downloaded as f64 / total_size as f64) * 100.0;
            let _ = app_handle.emit(
                "install-progress",
                format!("Downloading: {:.1}%", percentage),
            );
        }
    }

    file.flush().await.map_err(|e| e.to_string())?;

    if downloaded == 0 {
        let _ = std::fs::remove_file(&temp_path);
        return Err("Download completed with no data.".into());
    }

    let _ = app_handle.emit("install-progress", "Download complete. Installing...");

    let result =
        installer::install_appimage_internal(temp_path.to_string_lossy().to_string()).await;

    let _ = std::fs::remove_file(&temp_path);
    result
}