Skip to content

Commit

Permalink
refactor: 优化了导入和导出数据的代码 (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Oct 14, 2024
1 parent deadef4 commit 90ce95f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
53 changes: 29 additions & 24 deletions src-tauri/src/plugins/backup/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Runtime>(
app_handle: AppHandle<R>,
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<bool> {
let file = File::open(path)?;
let decoder = GzDecoder::new(file);
pub async fn import_data(src_path: String, dst_dir: PathBuf) -> Result<bool, String> {
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)
Expand All @@ -73,7 +78,7 @@ pub async fn move_data(from: PathBuf, to: PathBuf) -> Result<PathBuf, String> {
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();

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/plugins/fs-extra/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn metadata(path: PathBuf) -> Result<Metadata, String> {
#[command]
pub async fn open_path<R: Runtime>(
app_handle: AppHandle<R>,
path: &str,
path: String,
finder: bool,
) -> Result<(), String> {
if finder {
Expand Down
17 changes: 2 additions & 15 deletions src-tauri/src/plugins/window/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,16 @@ 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<R: Runtime>(window: WebviewWindow<R>) {
window.show().unwrap();
window.unminimize().unwrap();
window.set_focus().unwrap();
}

// 显示窗口(linux)
#[cfg(target_os = "linux")]
#[command]
pub async fn show_window<R: Runtime>(window: WebviewWindow<R>) {
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<R: Runtime>(window: WebviewWindow<R>) {
window.hide().unwrap();
}
Expand Down
11 changes: 7 additions & 4 deletions src/plugins/backup.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -23,27 +24,29 @@ 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()}`,
});
};

/**
* 导入数据
*/
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,
});
};

Expand Down

0 comments on commit 90ce95f

Please sign in to comment.