diff --git a/src-tauri/src/plugins/backup/src/commands.rs b/src-tauri/src/plugins/backup/src/commands.rs index a5049f2ab8..caeb4d8ad0 100644 --- a/src-tauri/src/plugins/backup/src/commands.rs +++ b/src-tauri/src/plugins/backup/src/commands.rs @@ -9,57 +9,62 @@ use std::{ path::PathBuf, }; use tar::Archive; -use tauri::{command, path::BaseDirectory, AppHandle, Manager, Runtime}; +use tauri::{command, AppHandle, Runtime}; use tauri_plugin_eco_fs_extra::{get_file_name, open_path}; // 导出数据 #[command] pub async fn export_data( app_handle: AppHandle, - src_dir: PathBuf, - file_name: String, -) -> tauri::Result<()> { - let dst_path = app_handle - .path() - .resolve(file_name, BaseDirectory::Download)?; - let dst_file = File::create(dst_path.clone())?; - + src_dir: String, + dst_path: String, +) -> Result<(), String> { + let dst_file = File::create(dst_path.clone()).map_err(|err| err.to_string())?; let enc = GzEncoder::new(dst_file, Compression::default()); let mut tar = tar::Builder::new(enc); - for entry in read_dir(&src_dir)? { - let entry = entry?; + for entry in read_dir(&src_dir).map_err(|err| err.to_string())? { + let entry = entry.map_err(|err| err.to_string())?; let path = entry.path(); - let name = path.strip_prefix(&src_dir).unwrap(); + let name = path.strip_prefix(&src_dir).map_err(|err| err.to_string())?; if path.is_dir() { - tar.append_dir_all(name, path.clone())?; + tar.append_dir_all(name, path.clone()) + .map_err(|err| err.to_string())?; } else { - tar.append_file(name, &mut File::open(path.clone())?)?; + tar.append_file( + name, + &mut File::open(path.clone()).map_err(|err| err.to_string())?, + ) + .map_err(|err| err.to_string())?; } } - tar.finish()?; + tar.finish().map_err(|err| err.to_string())?; - let _ = open_path(app_handle, dst_path.to_str().unwrap(), true).await; + open_path(app_handle, dst_path, true) + .await + .map_err(|err| err.to_string())?; Ok(()) } // 导入数据 #[command] -pub async fn import_data(dst_dir: PathBuf, path: String) -> tauri::Result { - let file = File::open(path)?; - let decoder = GzDecoder::new(file); +pub async fn import_data(src_path: String, dst_dir: PathBuf) -> Result { + let src_file = File::open(src_path).map_err(|err| err.to_string())?; + let decoder = GzDecoder::new(src_file); let mut archive = Archive::new(decoder); - for entry in archive.entries()? { - let mut entry = entry?; - let path = entry.path()?; + for entry in archive.entries().map_err(|err| err.to_string())? { + let mut entry = entry.map_err(|err| err.to_string())?; + let path = entry.path().map_err(|err| err.to_string())?; #[cfg(target_os = "windows")] let path = std::path::Path::new(&path.to_string_lossy().replace("\\", "/")).to_path_buf(); - entry.unpack(dst_dir.join(path))?; + entry + .unpack(dst_dir.join(path)) + .map_err(|err| err.to_string())?; } Ok(true) @@ -73,7 +78,7 @@ pub async fn move_data(from: PathBuf, to: PathBuf) -> Result { let mut config = HashSet::new(); config.insert(DirEntryAttr::Path); - let ls_result = ls(&from, &config).unwrap(); + let ls_result = ls(&from, &config).map_err(|err| err.to_string())?; let mut from_items = Vec::new(); diff --git a/src-tauri/src/plugins/fs-extra/src/commands.rs b/src-tauri/src/plugins/fs-extra/src/commands.rs index 1a482457d8..d62a6b8374 100644 --- a/src-tauri/src/plugins/fs-extra/src/commands.rs +++ b/src-tauri/src/plugins/fs-extra/src/commands.rs @@ -43,7 +43,7 @@ pub async fn metadata(path: PathBuf) -> Result { #[command] pub async fn open_path( app_handle: AppHandle, - path: &str, + path: String, finder: bool, ) -> Result<(), String> { if finder { diff --git a/src-tauri/src/plugins/window/src/commands.rs b/src-tauri/src/plugins/window/src/commands.rs index f780843515..8622104c13 100644 --- a/src-tauri/src/plugins/window/src/commands.rs +++ b/src-tauri/src/plugins/window/src/commands.rs @@ -7,8 +7,7 @@ pub static PREFERENCE_WINDOW_LABEL: &str = "preference"; // 主窗口的title pub static MAIN_WINDOW_TITLE: &str = "EcoPaste"; -// 显示窗口(非linux) -#[cfg(not(target_os = "linux"))] +// 显示窗口 #[command] pub async fn show_window(window: WebviewWindow) { window.show().unwrap(); @@ -16,20 +15,8 @@ pub async fn show_window(window: WebviewWindow) { window.set_focus().unwrap(); } -// 显示窗口(linux) -#[cfg(target_os = "linux")] -#[command] -pub async fn show_window(window: WebviewWindow) { - let position = window.outer_position().unwrap(); - let physical_position = tauri::PhysicalPosition::new(position.x, position.y); - - window.hide().unwrap(); - window.set_position(physical_position).unwrap(); - window.show().unwrap(); -} - -#[command] // 隐藏窗口 +#[command] pub async fn hide_window(window: WebviewWindow) { window.hide().unwrap(); } diff --git a/src/plugins/backup.ts b/src/plugins/backup.ts index b71377541f..06c0373d7f 100644 --- a/src/plugins/backup.ts +++ b/src/plugins/backup.ts @@ -1,5 +1,6 @@ import { invoke } from "@tauri-apps/api/core"; import { emit } from "@tauri-apps/api/event"; +import { downloadDir } from "@tauri-apps/api/path"; import { open } from "@tauri-apps/plugin-dialog"; import { omit } from "lodash-es"; @@ -23,9 +24,11 @@ export const exportData = async () => { await writeFile(getBackupStorePath(), JSON.stringify(content)); + const dstPath = joinPath(await downloadDir(), `${formatDate()}.${extname()}`); + return invoke(BACKUP_PLUGIN.EXPORT_DATA, { + dstPath, srcDir: getSaveDataDir(), - fileName: `${formatDate()}.${extname()}`, }); }; @@ -33,17 +36,17 @@ export const exportData = async () => { * 导入数据 */ export const importData = async () => { - const path = await open({ + const srcPath = await open({ filters: [{ name: "", extensions: [extname()] }], }); - if (!path) return; + if (!srcPath) return; await emit(LISTEN_KEY.CLOSE_DATABASE); return invoke(BACKUP_PLUGIN.IMPORT_DATA, { + srcPath, dstDir: getSaveDataDir(), - path, }); };