Initial Commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AppConfig {
|
||||
pub update_url: String,
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
update_url: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn config_path(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
let dir = app
|
||||
.path()
|
||||
.app_config_dir()
|
||||
.map_err(|e| format!("Failed to get config dir: {}", e))?;
|
||||
std::fs::create_dir_all(&dir)
|
||||
.map_err(|e| format!("Failed to create config dir: {}", e))?;
|
||||
Ok(dir.join("config.json"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_config(app: AppHandle) -> Result<AppConfig, String> {
|
||||
let path = config_path(&app)?;
|
||||
if !path.exists() {
|
||||
return Ok(AppConfig::default());
|
||||
}
|
||||
let data = std::fs::read_to_string(&path)
|
||||
.map_err(|e| format!("Failed to read config: {}", e))?;
|
||||
serde_json::from_str(&data)
|
||||
.map_err(|e| format!("Failed to parse config: {}", e))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn save_config(app: AppHandle, config: AppConfig) -> Result<(), String> {
|
||||
let path = config_path(&app)?;
|
||||
let data = serde_json::to_string_pretty(&config)
|
||||
.map_err(|e| format!("Failed to serialize config: {}", e))?;
|
||||
std::fs::write(&path, data)
|
||||
.map_err(|e| format!("Failed to write config: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UpdateInfo {
|
||||
pub version: String,
|
||||
pub url: String,
|
||||
pub notes: Option<String>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn check_for_update(app: AppHandle) -> Result<Option<UpdateInfo>, String> {
|
||||
let config = get_config(app.clone()).await?;
|
||||
|
||||
if config.update_url.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let response = reqwest::get(&config.update_url)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to check for updates: {}", e))?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(format!("Update server returned {}", response.status()));
|
||||
}
|
||||
|
||||
let body: serde_json::Value = response
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| format!("Invalid update response: {}", e))?;
|
||||
|
||||
// Parse Gitea/GitHub release format
|
||||
let tag = body["tag_name"]
|
||||
.as_str()
|
||||
.ok_or_else(|| "No tag_name in response".to_string())?;
|
||||
|
||||
let html_url = body["html_url"]
|
||||
.as_str()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let notes = body["body"]
|
||||
.as_str()
|
||||
.map(|s| s.to_string())
|
||||
.filter(|s| !s.is_empty());
|
||||
|
||||
// Strip leading 'v' from tag if present (v0.2.0 -> 0.2.0)
|
||||
let remote_version = tag.strip_prefix('v').unwrap_or(tag);
|
||||
let current = app.package_info().version.to_string();
|
||||
|
||||
if remote_version != current {
|
||||
Ok(Some(UpdateInfo {
|
||||
version: remote_version.to_string(),
|
||||
url: html_url,
|
||||
notes,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user