config and ffmpeg bootstrap
This commit is contained in:
+91
-1
@@ -3,10 +3,100 @@ mod capture;
|
||||
mod config;
|
||||
mod encode;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::Serialize;
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
|
||||
use crate::bootstrap::FfmpegStatus;
|
||||
use crate::config::Config;
|
||||
|
||||
pub struct AppState {
|
||||
pub app_dir: PathBuf,
|
||||
pub config: Mutex<Config>,
|
||||
}
|
||||
|
||||
fn exe_dir() -> Result<PathBuf> {
|
||||
let exe = std::env::current_exe()?;
|
||||
Ok(exe
|
||||
.parent()
|
||||
.ok_or_else(|| anyhow::anyhow!("exe has no parent directory"))?
|
||||
.to_path_buf())
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct AppStatus {
|
||||
ffmpeg: FfmpegStatus,
|
||||
needs_first_run: bool,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_status(state: State<'_, AppState>) -> AppStatus {
|
||||
let cfg = state.config.lock().expect("config mutex");
|
||||
AppStatus {
|
||||
ffmpeg: bootstrap::check(&state.app_dir),
|
||||
needs_first_run: cfg.needs_first_run(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_config(state: State<'_, AppState>) -> Config {
|
||||
state.config.lock().expect("config mutex").clone()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn save_config(state: State<'_, AppState>, config: Config) -> Result<(), String> {
|
||||
let path = config::config_path(&state.app_dir);
|
||||
config.save(&path).map_err(|e| format!("{e:#}"))?;
|
||||
*state.config.lock().expect("config mutex") = config;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
struct DownloadProgress {
|
||||
downloaded: u64,
|
||||
total: Option<u64>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn download_ffmpeg(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> {
|
||||
let app_dir = state.app_dir.clone();
|
||||
let app_handle = app.clone();
|
||||
bootstrap::download(&app_dir, move |downloaded, total| {
|
||||
let _ = app_handle.emit(
|
||||
"ffmpeg-progress",
|
||||
DownloadProgress { downloaded, total },
|
||||
);
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("{e:#}"))
|
||||
}
|
||||
|
||||
pub fn run() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let app_dir = exe_dir().expect("resolve exe directory");
|
||||
let cfg_path = config::config_path(&app_dir);
|
||||
let cfg = Config::load_or_create(&cfg_path).expect("load or create config.toml");
|
||||
|
||||
tracing::info!(?app_dir, "starting ByteSize");
|
||||
|
||||
let state = AppState {
|
||||
app_dir,
|
||||
config: Mutex::new(cfg),
|
||||
};
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.invoke_handler(tauri::generate_handler![])
|
||||
.manage(state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_status,
|
||||
get_config,
|
||||
save_config,
|
||||
download_ffmpeg,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running ByteSize")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user