diff --git a/.husky/pre-commit b/.husky/pre-commit index b3e891bf..cb2c84d5 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -pnpm lint-staged || bun lint-staged +pnpm lint-staged diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 69d9d9bf..406b4c9b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,14 +1,11 @@ -use std::fs::File; +use std::env; use std::io::Read; -use std::io::Write; use base64::engine::general_purpose; use base64::Engine; -use std::env; -use std::fs::read; // 引入 read 函数用于读取文件 +#[cfg(debug_assertions)] use tauri::Manager; // 引入 base64 编码函数 -use tauri::Runtime; // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command #[tauri::command] @@ -28,9 +25,7 @@ fn open_file_by_path(path: String) -> String { /// 保存字符串到指定路径 #[tauri::command] fn save_file_by_path(path: String, content: String) -> Result { - let mut file = std::fs::File::create(&path).map_err(|e| e.to_string())?; - file.write_all(content.as_bytes()) - .map_err(|e| e.to_string())?; + std::fs::write(path, content).map_err(|e| e.to_string())?; Ok(true) } @@ -49,47 +44,40 @@ fn check_json_exist(path: String) -> bool { #[tauri::command] fn convert_image_to_base64(image_path: String) -> Result { - match read(&image_path) { - Ok(image_data) => { - // let base64_str = Engine::encode(&image_data); - let base64_str = general_purpose::STANDARD.encode(&image_data); - Ok(base64_str) - } - Err(e) => Err(format!("无法读取文件: {}, {}", e, image_path)), - } + Ok(general_purpose::STANDARD + .encode(&std::fs::read(image_path).map_err(|e| format!("无法读取文件: {}", e))?)) } /// 将base64编码字符串保存为图片文件 #[tauri::command] fn save_base64_to_image(base64_str: &str, file_name: &str) -> Result<(), String> { - // 进行解码 - match general_purpose::STANDARD.decode(base64_str) { - Ok(image_data) => { - // 创建文件并写入数据 - let mut file = File::create(file_name).map_err(|e| format!("无法创建文件: {}", e))?; - file.write_all(&image_data) - .map_err(|e| format!("无法写入文件: {}", e))?; - Ok(()) - } - Err(e) => Err(format!("解码失败: {}", e)), - } + std::fs::write( + file_name, + &general_purpose::STANDARD + .decode(base64_str) + .map_err(|e| format!("解码失败: {}", e))?, + ) + .map_err(|e| e.to_string())?; + Ok(()) } /// 读取 MP3 文件并返回其 Base64 编码字符串 #[tauri::command] fn read_mp3_file(path: String) -> Result { - let mut file = File::open(&path).map_err(|e| format!("无法打开文件: {}", e))?; - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) - .map_err(|e| format!("读取文件时出错: {}", e))?; - // 将文件内容编码为 Base64 - let base64_str = general_purpose::STANDARD.encode(&buffer); - Ok(base64_str) + Ok(general_purpose::STANDARD + .encode(&std::fs::read(path).map_err(|e| format!("读取文件时出错: {}", e))?)) } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + // 在 Linux 上禁用 DMA-BUF 渲染器 + // 否则无法在 Linux 上运行 + // 相同的bug: https://github.com/tauri-apps/tauri/issues/10702 + // 解决方案来源: https://github.com/clash-verge-rev/clash-verge-rev/blob/ae5b2cfb79423c7e76a281725209b812774367fa/src-tauri/src/lib.rs#L27-L28 + #[cfg(target_os = "linux")] + std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1"); + tauri::Builder::default() .plugin(tauri_plugin_store::Builder::new().build()) .setup(|app| { diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index c80f9baf..c043face 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -3,9 +3,9 @@ "version": "0.0.0-dev", "identifier": "liren.project-graph", "build": { - "beforeDevCommand": "pnpm dev", + "beforeDevCommand": "pnpm dev || bun run dev", "devUrl": "http://localhost:1420", - "beforeBuildCommand": "pnpm build", + "beforeBuildCommand": "pnpm build || bun run build", "frontendDist": "../dist" }, "app": { @@ -19,6 +19,7 @@ "dragDropEnabled": false } ], + "macOSPrivateApi": true, "security": { "csp": null, "capabilities": [] diff --git a/src/core/RecentFileManager.tsx b/src/core/RecentFileManager.tsx index 615ff1e2..b02daa5f 100644 --- a/src/core/RecentFileManager.tsx +++ b/src/core/RecentFileManager.tsx @@ -99,7 +99,7 @@ export namespace RecentFileManager { for (const file of recentFiles) { try { // const isExists = await exists(file.path); // 检查文件是否存在 - const isExists = await invoke("check_json_exist", { + const isExists = await invoke("check_json_exist", { path: file.path, }); if (isExists) { diff --git a/src/core/stage/StageSaveManager.tsx b/src/core/stage/StageSaveManager.tsx index 5b52bc00..c72757f7 100644 --- a/src/core/stage/StageSaveManager.tsx +++ b/src/core/stage/StageSaveManager.tsx @@ -67,7 +67,7 @@ export namespace StageSaveManager { */ export async function backupHandle(path: string, data: Serialized.File) { const backupFolderPath = PathString.dirPath(path); - const isExists = await invoke("check_json_exist", { + const isExists = await invoke("check_json_exist", { path: backupFolderPath, }); if (!isExists) { diff --git a/src/main.tsx b/src/main.tsx index ad0d9fb1..514c7eba 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -163,7 +163,7 @@ async function loadStartFile() { if (path === "") { return; } - const isExists = await invoke("check_json_exist", { + const isExists = await invoke("check_json_exist", { path, }); if (isExists) { diff --git a/src/pages/_toolbar.tsx b/src/pages/_toolbar.tsx index dd08d683..3a29d808 100644 --- a/src/pages/_toolbar.tsx +++ b/src/pages/_toolbar.tsx @@ -501,7 +501,7 @@ async function openBrowserOrFile() { // 是网址 myOpen(nodeText); } else { - const isExists = await invoke("check_json_exist", { + const isExists = await invoke("check_json_exist", { path: nodeText, }); if (isExists) {