|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use log::{error, info}; |
| 4 | +use sha2::{Digest, Sha256}; |
| 5 | + |
| 6 | +pub struct ReleaseInfo { |
| 7 | + pub version: String, |
| 8 | + pub zip_url: String, |
| 9 | + pub sha256: String, |
| 10 | +} |
| 11 | + |
| 12 | +pub fn get_latest_release() -> Option<ReleaseInfo> { |
| 13 | + let req = minreq::Request::new( |
| 14 | + minreq::Method::Get, |
| 15 | + "https://api.github.com/repos/RustCastLabs/rustcast/releases/latest", |
| 16 | + ) |
| 17 | + .with_header("User-Agent", "rustcast-update-checker") |
| 18 | + .with_header("Accept", "application/vnd.github+json") |
| 19 | + .with_header("X-GitHub-Api-Version", "2022-11-28"); |
| 20 | + |
| 21 | + let resp = req |
| 22 | + .send() |
| 23 | + .and_then(|x| x.as_str().map(serde_json::Value::from_str)); |
| 24 | + |
| 25 | + if let Ok(Ok(val)) = resp { |
| 26 | + let version = val.get("name")?.as_str()?.to_string(); |
| 27 | + |
| 28 | + let assets = val.get("assets")?.as_array()?; |
| 29 | + |
| 30 | + let mut zip_url = None; |
| 31 | + let mut sha256 = None; |
| 32 | + |
| 33 | + for asset in assets { |
| 34 | + let name = asset.get("name")?.as_str()?; |
| 35 | + let url = asset.get("browser_download_url")?.as_str()?.to_string(); |
| 36 | + |
| 37 | + if name == "Rustcast-universal-macos.app.zip" { |
| 38 | + zip_url = Some(url); |
| 39 | + |
| 40 | + sha256 = asset |
| 41 | + .get("digest") |
| 42 | + .and_then(|d| d.as_str()) |
| 43 | + .and_then(|d| d.strip_prefix("sha256:")) |
| 44 | + .map(|d| d.to_string()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + Some(ReleaseInfo { |
| 49 | + version, |
| 50 | + zip_url: zip_url?, |
| 51 | + sha256: sha256?, |
| 52 | + }) |
| 53 | + } else { |
| 54 | + None |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +pub fn new_version_available() -> Option<ReleaseInfo> { |
| 59 | + info!("Checking for new version"); |
| 60 | + let info = get_latest_release()?; |
| 61 | + info!("Got latest info"); |
| 62 | + let current = option_env!("APP_VERSION").unwrap_or(""); |
| 63 | + |
| 64 | + if info.version != current { |
| 65 | + Some(info) |
| 66 | + } else { |
| 67 | + None |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +pub fn verify_sha256(file_path: &std::path::Path, expected_hex: &str) -> std::io::Result<bool> { |
| 72 | + let bytes = std::fs::read(file_path)?; |
| 73 | + let digest = Sha256::digest(&bytes); |
| 74 | + let actual_hex = hex::encode(digest); |
| 75 | + Ok(actual_hex == expected_hex) |
| 76 | +} |
| 77 | + |
| 78 | +pub fn download_latest_app() -> Result<std::path::PathBuf, ()> { |
| 79 | + let info = get_latest_release().ok_or_else(|| { |
| 80 | + error!("Could not get latest release info"); |
| 81 | + })?; |
| 82 | + |
| 83 | + info!("got latest release"); |
| 84 | + |
| 85 | + let tmp = tempfile::tempdir().map_err(|e| { |
| 86 | + error!("Could not create temporary directory: {e}"); |
| 87 | + })?; |
| 88 | + |
| 89 | + info!("created temp dir"); |
| 90 | + |
| 91 | + let zip_path = tmp.path().join("Rustcast-universal-macos.app.zip"); |
| 92 | + |
| 93 | + info!("zip path: {:?}", zip_path); |
| 94 | + let resp = minreq::get(&info.zip_url) |
| 95 | + .with_header("User-Agent", "rustcast-update-checker") |
| 96 | + .send() |
| 97 | + .map_err(|e| { |
| 98 | + error!("Could not download update: {e}"); |
| 99 | + })?; |
| 100 | + |
| 101 | + info!("downloaded zip"); |
| 102 | + |
| 103 | + std::fs::write(&zip_path, resp.as_bytes()).map_err(|e| { |
| 104 | + error!("Could not write zip to disk: {e}"); |
| 105 | + })?; |
| 106 | + |
| 107 | + info!("wrote zip to disk"); |
| 108 | + |
| 109 | + let ok = verify_sha256(&zip_path, &info.sha256).map_err(|e| { |
| 110 | + error!("Could not verify sha256: {e}"); |
| 111 | + })?; |
| 112 | + |
| 113 | + info!("verified sha256"); |
| 114 | + |
| 115 | + if !ok { |
| 116 | + error!("SHA256 mismatch — aborting update"); |
| 117 | + return Err(()); |
| 118 | + } |
| 119 | + |
| 120 | + let zip_file = std::fs::File::open(&zip_path).map_err(|e| { |
| 121 | + error!("Could not open zip: {e}"); |
| 122 | + })?; |
| 123 | + |
| 124 | + info!("opened zip"); |
| 125 | + |
| 126 | + let mut archive = zip::ZipArchive::new(zip_file).map_err(|e| { |
| 127 | + error!("Could not read zip archive: {e}"); |
| 128 | + })?; |
| 129 | + |
| 130 | + info!("read zip archive. contents:"); |
| 131 | + |
| 132 | + archive.extract(tmp.path()).map_err(|e| { |
| 133 | + error!("Could not extract zip: {e}"); |
| 134 | + })?; |
| 135 | + |
| 136 | + if let Ok(entries) = std::fs::read_dir(tmp.path()) { |
| 137 | + for entry in entries.flatten() { |
| 138 | + info!(" extracted entry: {:?}", entry.file_name()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + let extracted_app = tmp.path().join("target/release/macos/Rustcast.app"); |
| 143 | + |
| 144 | + info!("found extracted app at: {:?}", extracted_app); |
| 145 | + |
| 146 | + let dest = get_app_path().ok_or_else(|| { |
| 147 | + error!("Could not determine current app path"); |
| 148 | + })?; |
| 149 | + |
| 150 | + info!("Installing update over {:?}", dest); |
| 151 | + |
| 152 | + if dest.exists() { |
| 153 | + std::fs::remove_dir_all(&dest).map_err(|e| { |
| 154 | + error!("Could not remove existing app: {e}"); |
| 155 | + })?; |
| 156 | + } |
| 157 | + |
| 158 | + move_or_copy(&extracted_app, &dest).map_err(|e| { |
| 159 | + error!("Could not move app into place: {e}"); |
| 160 | + })?; |
| 161 | + |
| 162 | + info!("Successful update"); |
| 163 | + |
| 164 | + Ok(dest) |
| 165 | +} |
| 166 | + |
| 167 | +fn move_or_copy(src: &std::path::Path, dst: &std::path::Path) -> std::io::Result<()> { |
| 168 | + match std::fs::rename(src, dst) { |
| 169 | + Ok(()) => Ok(()), |
| 170 | + Err(_) => { |
| 171 | + copy_dir_recursive(src, dst)?; |
| 172 | + std::fs::remove_dir_all(src) |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +fn copy_dir_recursive(src: &std::path::Path, dst: &std::path::Path) -> std::io::Result<()> { |
| 178 | + std::fs::create_dir_all(dst)?; |
| 179 | + for entry in std::fs::read_dir(src)? { |
| 180 | + let entry = entry?; |
| 181 | + let dst_path = dst.join(entry.file_name()); |
| 182 | + if entry.file_type()?.is_dir() { |
| 183 | + copy_dir_recursive(&entry.path(), &dst_path)?; |
| 184 | + } else { |
| 185 | + std::fs::copy(entry.path(), dst_path)?; |
| 186 | + } |
| 187 | + } |
| 188 | + Ok(()) |
| 189 | +} |
| 190 | + |
| 191 | +pub fn relaunch_app() { |
| 192 | + let app_path = match get_app_path() { |
| 193 | + Some(p) => p, |
| 194 | + None => { |
| 195 | + error!("Could not determine current app path for relaunch"); |
| 196 | + return; |
| 197 | + } |
| 198 | + }; |
| 199 | + |
| 200 | + match std::process::Command::new("open").arg(&app_path).spawn() { |
| 201 | + Ok(_) => { |
| 202 | + info!("Relaunching app at {:?}", app_path); |
| 203 | + std::thread::sleep(std::time::Duration::from_millis(500)); |
| 204 | + std::process::exit(0); |
| 205 | + } |
| 206 | + Err(e) => { |
| 207 | + error!("Could not relaunch app: {e}"); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +pub fn get_app_path() -> Option<std::path::PathBuf> { |
| 213 | + let exe = std::env::current_exe().ok()?; |
| 214 | + |
| 215 | + let mut path = exe.as_path(); |
| 216 | + loop { |
| 217 | + if path.extension().and_then(|e| e.to_str()) == Some("app") { |
| 218 | + return Some(path.to_path_buf()); |
| 219 | + } |
| 220 | + path = path.parent()?; |
| 221 | + } |
| 222 | +} |
0 commit comments