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