Skip to content

Commit

Permalink
fix: 修复 Linux 导出数据报错的问题 (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Oct 22, 2024
1 parent d262dd4 commit 8c7e0af
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
39 changes: 19 additions & 20 deletions src-tauri/src/plugins/backup/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
};
use tar::Archive;
use tauri::{command, AppHandle, Runtime};
use tauri_plugin_eco_fs_extra::{get_file_name, open_path};
use tauri_plugin_eco_fs_extra::{metadata, open_path};

// 导出数据
#[command]
Expand All @@ -24,18 +24,21 @@ pub async fn export_data<R: Runtime>(
let mut tar = tar::Builder::new(enc);

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).map_err(|err| err.to_string())?;
if path.is_dir() {
tar.append_dir_all(name, path.clone())
let path = entry.map_err(|err| err.to_string())?.path();
let metadata = metadata(path.clone()).await?;

if metadata.is_file {
if metadata.extname == "db" || metadata.name == ".store-backup.json" {
let file = &mut File::open(path.clone()).map_err(|err| err.to_string())?;

tar.append_file(metadata.name.clone(), file)
.map_err(|err| err.to_string())?;
}
}

if metadata.is_dir && metadata.name == "images" {
tar.append_dir_all(metadata.name, path.clone())
.map_err(|err| err.to_string())?;
} else {
tar.append_file(
name,
&mut File::open(path.clone()).map_err(|err| err.to_string())?,
)
.map_err(|err| err.to_string())?;
}
}

Expand Down Expand Up @@ -86,21 +89,17 @@ pub async fn move_data(from: PathBuf, to: PathBuf) -> Result<PathBuf, String> {
if let Some(path) = item.get(&DirEntryAttr::Path) {
if let &DirEntryValue::String(ref path) = path {
let path = PathBuf::from(path);
let is_dir = path.is_dir();
let is_file = path.is_file();
let file_name = get_file_name(path.clone());
let metadata = metadata(path.clone()).await?;

// 忽略窗口状态插件生成的的文件
if is_file && file_name.starts_with(".window-state") {
if metadata.is_dir && metadata.name != "images" {
continue;
}

// 忽略日志插件生成的目录
if is_dir && file_name == "logs" {
if metadata.is_file && metadata.extname == "json" {
continue;
}

from_items.push(path);
from_items.push(path.clone());
}
}
}
Expand Down
26 changes: 18 additions & 8 deletions src-tauri/src/plugins/fs-extra/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,45 @@ use tauri_plugin_shell::ShellExt;
#[derive(Debug, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
size: u64,
is_dir: bool,
is_file: bool,
is_exist: bool,
file_name: String,
pub size: u64,
pub is_dir: bool,
pub is_file: bool,
pub is_exist: bool,
pub name: String,
pub extname: String,
}

// 获取文件(夹)名
pub fn get_file_name(path: PathBuf) -> String {
fn get_name(path: PathBuf) -> String {
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_default()
}

// 获取文件后缀名
fn get_extname(path: PathBuf) -> String {
path.extension()
.map(|ext| ext.to_string_lossy().to_string())
.unwrap_or_default()
}

// 获取路径的元信息
#[command]
pub async fn metadata(path: PathBuf) -> Result<Metadata, String> {
let size = get_size(&path).unwrap_or(0);
let is_dir = path.is_dir();
let is_file = path.is_file();
let is_exist = path.exists();
let file_name = get_file_name(path);
let name = get_name(path.clone());
let extname = get_extname(path);

Ok(Metadata {
size,
is_dir,
is_file,
is_exist,
file_name,
name,
extname,
})
}

Expand Down
12 changes: 6 additions & 6 deletions src/plugins/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ export const readFiles = async (): Promise<ClipboardPayload> => {

let count = 0;

const fileNames = [];
const names = [];

for await (const path of files) {
const { size, fileName } = await metadata(path);
const { size, name } = await metadata(path);

count += size;

fileNames.push(fileName);
names.push(name);
}

return {
count,
search: fileNames.join(" "),
search: names.join(" "),
value: JSON.stringify(files),
group: "files",
};
Expand Down Expand Up @@ -337,9 +337,9 @@ export const pasteClipboard = async (data?: ClipboardItem, plain = false) => {

if (plain) {
if (type === "files") {
const pasteValue = JSON.parse(value);
const pasteValue = JSON.parse(value).join("\n");

await writeText(pasteValue.join("\n"));
await writeText(pasteValue);
} else {
await writeText(data.search);
}
Expand Down
3 changes: 2 additions & 1 deletion src/types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export interface Metadata {
isDir: boolean;
isFile: boolean;
isExist: boolean;
fileName: string;
name: string;
extname: string;
}

export interface ReadImage {
Expand Down

0 comments on commit 8c7e0af

Please sign in to comment.