update banner via gitea releases
This commit is contained in:
@@ -2,6 +2,7 @@ mod bootstrap;
|
||||
mod capture;
|
||||
mod config;
|
||||
mod encode;
|
||||
mod updates;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
@@ -459,6 +460,16 @@ fn reveal_in_explorer(path: String) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn open_url(url: String) -> Result<(), String> {
|
||||
use std::process::Command;
|
||||
Command::new("cmd")
|
||||
.args(["/C", "start", "", &url])
|
||||
.spawn()
|
||||
.map_err(|e| format!("opening url: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn export(state: State<'_, AppState>, request: ExportRequest) -> Result<ExportResult, String> {
|
||||
let cfg = state.config.lock().expect("config mutex").clone();
|
||||
@@ -548,6 +559,27 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.manage(state)
|
||||
.setup(|app| {
|
||||
let check_on_launch = {
|
||||
let st = app.state::<AppState>();
|
||||
let cfg = st.config.lock().expect("config mutex");
|
||||
cfg.updates.check_on_launch
|
||||
};
|
||||
if check_on_launch {
|
||||
let handle = app.handle().clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
match updates::check_for_update().await {
|
||||
Ok(Some(info)) => {
|
||||
tracing::info!(version = info.version, "update available");
|
||||
let _ = handle.emit("update-available", &info);
|
||||
}
|
||||
Ok(None) => tracing::debug!("no update available"),
|
||||
Err(e) => tracing::warn!("update check failed: {e:#}"),
|
||||
}
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_status,
|
||||
get_config,
|
||||
@@ -560,6 +592,7 @@ pub fn run() {
|
||||
export,
|
||||
extract_thumbnails,
|
||||
reveal_in_explorer,
|
||||
open_url,
|
||||
show_overlay,
|
||||
hide_overlay,
|
||||
get_overlay_rect,
|
||||
|
||||
@@ -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, ¤t) {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user