update banner via gitea releases

This commit is contained in:
2026-05-12 00:20:49 +03:00
parent 5a916d2ad2
commit 297f307588
5 changed files with 209 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
const GITEA_LATEST: &str =
"https://git.lashman.live/api/v1/repos/lashman/bytesize/releases/latest";
#[derive(Debug, Clone, Serialize)]
pub struct UpdateInfo {
pub version: String,
pub current_version: String,
pub release_url: String,
pub body: String,
}
#[derive(Debug, Deserialize)]
struct GiteaRelease {
tag_name: String,
body: Option<String>,
html_url: String,
}
pub async fn check_for_update() -> Result<Option<UpdateInfo>> {
let client = reqwest::Client::builder()
.user_agent(concat!("ByteSize/", env!("CARGO_PKG_VERSION")))
.build()?;
let resp = client.get(GITEA_LATEST).send().await?;
if !resp.status().is_success() {
tracing::debug!("update check returned status {}", resp.status());
return Ok(None);
}
let body = resp.text().await?;
let release: GiteaRelease = serde_json::from_str(&body)?;
let latest = release.tag_name.trim_start_matches('v').to_string();
let current = env!("CARGO_PKG_VERSION").to_string();
if is_newer(&latest, &current) {
Ok(Some(UpdateInfo {
version: latest,
current_version: current,
release_url: release.html_url,
body: release.body.unwrap_or_default(),
}))
} else {
Ok(None)
}
}
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();
let max = l.len().max(c.len());
l.resize(max, 0);
c.resize(max, 0);
l > c
}