Skip to main content

qualia_client_core/
update_installer.rs

1//! Download platform installers and launch them (Tauri updater parity for Flutter desktop).
2
3use std::path::PathBuf;
4
5pub async fn download_and_install_update(download_url: String) -> Result<(), String> {
6    if download_url.is_empty() {
7        return Err("Empty download URL".into());
8    }
9
10    let response = reqwest::get(&download_url)
11        .await
12        .map_err(|e| format!("Download failed: {e}"))?;
13    if !response.status().is_success() {
14        return Err(format!("Download HTTP {}", response.status()));
15    }
16
17    let bytes = response
18        .bytes()
19        .await
20        .map_err(|e| format!("Read body: {e}"))?;
21
22    let file_name = download_url
23        .rsplit('/')
24        .next()
25        .filter(|s| !s.is_empty())
26        .unwrap_or("qualia_update.exe");
27
28    let path: PathBuf = std::env::temp_dir().join(file_name);
29    std::fs::write(&path, &bytes).map_err(|e| format!("Write installer: {e}"))?;
30
31    launch_installer(&path)
32}
33
34fn launch_installer(path: &PathBuf) -> Result<(), String> {
35    #[cfg(windows)]
36    {
37        std::process::Command::new("cmd")
38            .args(["/C", "start", "", &path.to_string_lossy()])
39            .spawn()
40            .map_err(|e| format!("Launch installer: {e}"))?;
41        Ok(())
42    }
43
44    #[cfg(target_os = "macos")]
45    {
46        std::process::Command::new("open")
47            .arg(path)
48            .spawn()
49            .map_err(|e| format!("Launch installer: {e}"))?;
50        Ok(())
51    }
52
53    #[cfg(all(unix, not(target_os = "macos")))]
54    {
55        use std::os::unix::fs::PermissionsExt;
56        let mut perms = std::fs::metadata(path)
57            .map_err(|e| e.to_string())?
58            .permissions();
59        perms.set_mode(0o755);
60        std::fs::set_permissions(path, perms).map_err(|e| e.to_string())?;
61        std::process::Command::new(path)
62            .spawn()
63            .map_err(|e| format!("Launch installer: {e}"))?;
64        Ok(())
65    }
66
67    #[cfg(not(any(windows, target_os = "macos", unix)))]
68    {
69        let _ = path;
70        Err("Auto-install not supported on this platform".into())
71    }
72}