Skip to content

Commit

Permalink
feat: 自定义 fs-extra 插件
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Jun 14, 2024
1 parent 0a8c043 commit b85326b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 32 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"react-window": "^1.8.10",
"tauri-plugin-autostart-api": "github:tauri-apps/tauri-plugin-autostart#v1",
"tauri-plugin-clipboard-api": "^1.0.1",
"tauri-plugin-fs-extra-api": "github:tauri-apps/tauri-plugin-fs-extra#v1",
"tauri-plugin-sql-api": "github:tauri-apps/tauri-plugin-sql#v1",
"valtio": "^1.13.2",
"valtio-persist": "^1.0.2"
Expand Down
11 changes: 0 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ edition = "2021"
[build-dependencies]
tauri-build = { version = "1", features = [] }


[dependencies]
tauri = { version = "1", features = [ "system-tray", "macos-private-api", "api-all"] }
serde = { version = "1", features = ["derive"] }
Expand All @@ -20,8 +19,8 @@ tauri-plugin-theme = "0.2.0"
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-sql = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1",features = ["sqlite"] }
tauri-plugin-fs-extra = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
window-shadows = "0.2.2"
thiserror = "1.0.61"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
88 changes: 88 additions & 0 deletions src-tauri/src/fs_extra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use serde::{ser::Serializer, Serialize};
use std::{fs, path::Path};
use tauri::{
command, generate_handler,
plugin::{Builder, TauriPlugin},
Runtime,
};

#[derive(Debug, thiserror::Error)]
enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
}

impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}

type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, serde::Serialize)]
struct Metadata {
size: u64,
is_dir: bool,
is_file: bool,
is_exist: bool,
}

fn get_dir_size<P: AsRef<Path>>(path: P) -> Result<u64> {
let mut size = 0;

for entry in fs::read_dir(path)? {
let entry = entry?;
let metadata = entry.metadata()?;

if metadata.is_file() {
size += metadata.len();
} else if metadata.is_dir() {
size += get_dir_size(entry.path())?;
}
}

Ok(size)
}

#[command]
async fn metadata(path: &str) -> Result<Metadata> {
let replace_path = path.replace("file://", "");

let path = Path::new(&replace_path);

let is_exist = path.exists();

let mut size = 0;
let mut is_dir = false;
let mut is_file = false;

if is_exist {
let metadata = fs::metadata(&path)?;

is_dir = metadata.is_dir();
is_file = metadata.is_file();

size = if is_file {
metadata.len()
} else {
get_dir_size(&path)?
};
}

Ok(Metadata {
size,
is_dir,
is_file,
is_exist,
})
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("fs-extra")
.invoke_handler(generate_handler![metadata])
.build()
}
5 changes: 3 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use tauri::{generate_context, generate_handler, Builder, Manager, WindowEvent};
use tauri_plugin_theme::ThemePlugin;
mod fs_extra;
mod tray;
mod window;
use tauri_plugin_autostart::MacosLauncher;
Expand Down Expand Up @@ -43,8 +44,8 @@ fn main() {
))
// 数据库:https://github.com/tauri-apps/tauri-plugin-sql
.plugin(tauri_plugin_sql::Builder::default().build())
// fs拓展插件:https://github.com/tauri-apps/tauri-plugin-fs-extra
.plugin(tauri_plugin_fs_extra::init())
// 自定义的 fs_extra 插件
.plugin(fs_extra::init())
// 系统托盘:https://tauri.app/v1/guides/features/system-tray
.system_tray(tray::menu())
.on_system_tray_event(tray::handler)
Expand Down
17 changes: 17 additions & 0 deletions src/utils/fsExtra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { invoke } from "@tauri-apps/api";
import { camelCase, mapKeys } from "lodash-es";

interface Metadata {
size: number;
isDir: boolean;
isFile: boolean;
isExist: boolean;
}

export const metadata = async (path: string) => {
const result = await invoke<any>("plugin:fs-extra|metadata", {
path: decodeURI(path),
});

return mapKeys(result, (_, key) => camelCase(key)) as Metadata;
};

0 comments on commit b85326b

Please sign in to comment.