mirror of
https://github.com/lanyeeee/bilibili-video-downloader.git
synced 2026-09-05 23:57:23 +08:00
feat: 后端支持保存配置文件
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
use parking_lot::RwLock;
|
||||
use tauri::AppHandle;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::{
|
||||
config::Config,
|
||||
errors::{CommandError, CommandResult},
|
||||
extensions::AppHandleExt,
|
||||
logger,
|
||||
};
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
@@ -14,3 +20,38 @@ pub fn greet(name: &str) -> String {
|
||||
pub fn get_config(config: tauri::State<RwLock<Config>>) -> Config {
|
||||
config.read().clone()
|
||||
}
|
||||
|
||||
#[tauri::command(async)]
|
||||
#[specta::specta]
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
pub fn save_config(app: AppHandle, config: Config) -> CommandResult<()> {
|
||||
let config_state = app.get_config();
|
||||
|
||||
let enable_file_logger = config.enable_file_logger;
|
||||
let enable_file_logger_changed = config_state
|
||||
.read()
|
||||
.enable_file_logger
|
||||
.ne(&enable_file_logger);
|
||||
|
||||
{
|
||||
// 包裹在大括号中,以便自动释放写锁
|
||||
let mut config_state = config_state.write();
|
||||
*config_state = config;
|
||||
config_state
|
||||
.save(&app)
|
||||
.map_err(|err| CommandError::from("保存配置失败", err))?;
|
||||
tracing::debug!("保存配置成功");
|
||||
}
|
||||
|
||||
if enable_file_logger_changed {
|
||||
if enable_file_logger {
|
||||
logger::reload_file_logger()
|
||||
.map_err(|err| CommandError::from("重新加载文件日志失败", err))?;
|
||||
} else {
|
||||
logger::disable_file_logger()
|
||||
.map_err(|err| CommandError::from("禁用文件日志失败", err))?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -22,7 +22,11 @@ fn generate_context() -> tauri::Context<Wry> {
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let builder = tauri_specta::Builder::<Wry>::new()
|
||||
.commands(tauri_specta::collect_commands![greet, get_config])
|
||||
.commands(tauri_specta::collect_commands![
|
||||
greet,
|
||||
get_config,
|
||||
save_config,
|
||||
])
|
||||
.events(tauri_specta::collect_events![LogEvent]);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
|
||||
@@ -10,6 +10,14 @@ async greet(name: string) : Promise<string> {
|
||||
},
|
||||
async getConfig() : Promise<Config> {
|
||||
return await TAURI_INVOKE("get_config");
|
||||
},
|
||||
async saveConfig(config: Config) : Promise<Result<null, CommandError>> {
|
||||
try {
|
||||
return { status: "ok", data: await TAURI_INVOKE("save_config", { config }) };
|
||||
} catch (e) {
|
||||
if(e instanceof Error) throw e;
|
||||
else return { status: "error", error: e as any };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +36,7 @@ logEvent: "log-event"
|
||||
|
||||
/** user-defined types **/
|
||||
|
||||
export type CommandError = { err_title: string; err_message: string }
|
||||
export type Config = { downloadDir: string; enableFileLogger: boolean }
|
||||
export type JsonValue = null | boolean | number | string | JsonValue[] | { [key in string]: JsonValue }
|
||||
export type LogEvent = { timestamp: string; level: LogLevel; fields: { [key in string]: JsonValue }; target: string; filename: string; line_number: number }
|
||||
|
||||
Reference in New Issue
Block a user