update download + apply flow

This commit is contained in:
2026-05-12 01:59:39 +03:00
parent 953263028b
commit 61fbe68251
4 changed files with 233 additions and 16 deletions
+73 -1
View File
@@ -1,4 +1,7 @@
use anyhow::Result;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
const GITEA_LATEST: &str =
@@ -10,6 +13,7 @@ pub struct UpdateInfo {
pub current_version: String,
pub release_url: String,
pub body: String,
pub download_url: Option<String>,
}
#[derive(Debug, Deserialize)]
@@ -17,6 +21,14 @@ struct GiteaRelease {
tag_name: String,
body: Option<String>,
html_url: String,
#[serde(default)]
assets: Vec<GiteaAsset>,
}
#[derive(Debug, Deserialize)]
struct GiteaAsset {
name: String,
browser_download_url: String,
}
pub async fn check_for_update() -> Result<Option<UpdateInfo>> {
@@ -33,17 +45,77 @@ pub async fn check_for_update() -> Result<Option<UpdateInfo>> {
let latest = release.tag_name.trim_start_matches('v').to_string();
let current = env!("CARGO_PKG_VERSION").to_string();
if is_newer(&latest, &current) {
let download_url = release
.assets
.iter()
.find(|a| a.name.eq_ignore_ascii_case("ByteSize.exe"))
.or_else(|| release.assets.iter().find(|a| a.name.to_lowercase().ends_with(".exe")))
.map(|a| a.browser_download_url.clone());
Ok(Some(UpdateInfo {
version: latest,
current_version: current,
release_url: release.html_url,
body: release.body.unwrap_or_default(),
download_url,
}))
} else {
Ok(None)
}
}
pub async fn download_to<F>(url: &str, dest: &Path, mut on_progress: F) -> Result<()>
where
F: FnMut(u64, Option<u64>) + Send,
{
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent).context("creating update parent dir")?;
}
let client = reqwest::Client::builder()
.user_agent(concat!("ByteSize/", env!("CARGO_PKG_VERSION")))
.build()?;
let resp = client.get(url).send().await?.error_for_status()?;
let total = resp.content_length();
let mut downloaded = 0u64;
let mut stream = resp.bytes_stream();
let mut file = std::fs::File::create(dest).context("creating update file")?;
use std::io::Write;
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
file.write_all(&chunk)?;
downloaded += chunk.len() as u64;
on_progress(downloaded, total);
}
file.flush()?;
Ok(())
}
pub fn pending_update_path(app_dir: &Path) -> PathBuf {
app_dir.join("ByteSize.new.exe")
}
pub fn has_pending_update(app_dir: &Path) -> bool {
pending_update_path(app_dir).is_file()
}
pub fn write_apply_script(app_dir: &Path) -> Result<PathBuf> {
let bat_path = app_dir.join("bytesize-apply-update.bat");
let exe_name = std::env::current_exe()
.ok()
.and_then(|p| p.file_name().map(|s| s.to_string_lossy().into_owned()))
.unwrap_or_else(|| "ByteSize.exe".to_string());
let body = format!(
"@echo off\r\n\
timeout /t 1 /nobreak >nul\r\n\
del \"%~dp0{exe}\"\r\n\
move /Y \"%~dp0ByteSize.new.exe\" \"%~dp0{exe}\" >nul\r\n\
start \"\" \"%~dp0{exe}\"\r\n\
del \"%~f0\"\r\n",
exe = exe_name
);
std::fs::write(&bat_path, body).context("writing apply script")?;
Ok(bat_path)
}
fn is_newer(latest: &str, current: &str) -> bool {
let mut l: Vec<u32> = latest.split('.').filter_map(|s| s.parse().ok()).collect();
let mut c: Vec<u32> = current.split('.').filter_map(|s| s.parse().ok()).collect();