mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-21 11:26:15 +02:00
feat(fs): add the size method to get the size of a file or directory (#2095)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
fs: minor
|
||||
---
|
||||
|
||||
Add the `size` method to get the size of a file or directory.
|
||||
File diff suppressed because one or more lines are too long
@@ -102,6 +102,7 @@ const COMMANDS: &[(&str, &[&str])] = &[
|
||||
("exists", &[]),
|
||||
("watch", &[]),
|
||||
("unwatch", &[]),
|
||||
("size", &[]),
|
||||
];
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -1322,6 +1322,31 @@ async function watchImmediate(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of a file or directory. For files, the `stat` functions can be used as well.
|
||||
*
|
||||
* If `path` is a directory, this function will recursively iterate over every file and every directory inside of `path` and therefore will be very time consuming if used on larger directories.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { size, BaseDirectory } from '@tauri-apps/plugin-fs';
|
||||
* // Get the size of the `$APPDATA/tauri` directory.
|
||||
* const dirSize = await size('tauri', { baseDir: BaseDirectory.AppData });
|
||||
* console.log(dirSize); // 1024
|
||||
* ```
|
||||
*
|
||||
* @since 2.1.0
|
||||
*/
|
||||
async function size(path: string | URL): Promise<number> {
|
||||
if (path instanceof URL && path.protocol !== 'file:') {
|
||||
throw new TypeError('Must be a file URL.')
|
||||
}
|
||||
|
||||
return await invoke('plugin:fs|size', {
|
||||
path: path instanceof URL ? path.toString() : path
|
||||
})
|
||||
}
|
||||
|
||||
export type {
|
||||
CreateOptions,
|
||||
OpenOptions,
|
||||
@@ -1369,5 +1394,6 @@ export {
|
||||
writeTextFile,
|
||||
exists,
|
||||
watch,
|
||||
watchImmediate
|
||||
watchImmediate,
|
||||
size
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# Automatically generated - DO NOT EDIT!
|
||||
|
||||
"$schema" = "../../schemas/schema.json"
|
||||
|
||||
[[permission]]
|
||||
identifier = "allow-size"
|
||||
description = "Enables the size command without any pre-configured scope."
|
||||
commands.allow = ["size"]
|
||||
|
||||
[[permission]]
|
||||
identifier = "deny-size"
|
||||
description = "Denies the size command without any pre-configured scope."
|
||||
commands.deny = ["size"]
|
||||
@@ -3410,6 +3410,32 @@ Denies the seek command without any pre-configured scope.
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:allow-size`
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Enables the size command without any pre-configured scope.
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:deny-size`
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Denies the size command without any pre-configured scope.
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:allow-stat`
|
||||
|
||||
</td>
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
[[permission]]
|
||||
identifier = "read-meta"
|
||||
description = "This enables all index or metadata related commands without any pre-configured accessible paths."
|
||||
commands.allow = ["read_dir", "stat", "lstat", "fstat", "exists"]
|
||||
commands.allow = ["read_dir", "stat", "lstat", "fstat", "exists", "size"]
|
||||
|
||||
@@ -1589,6 +1589,16 @@
|
||||
"type": "string",
|
||||
"const": "deny-seek"
|
||||
},
|
||||
{
|
||||
"description": "Enables the size command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "allow-size"
|
||||
},
|
||||
{
|
||||
"description": "Denies the size command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
"const": "deny-size"
|
||||
},
|
||||
{
|
||||
"description": "Enables the stat command without any pre-configured scope.",
|
||||
"type": "string",
|
||||
|
||||
@@ -897,6 +897,55 @@ pub fn exists<R: Runtime>(
|
||||
Ok(resolved_path.exists())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn size<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
global_scope: GlobalScope<Entry>,
|
||||
command_scope: CommandScope<Entry>,
|
||||
path: SafeFilePath,
|
||||
options: Option<BaseOptions>,
|
||||
) -> CommandResult<u64> {
|
||||
let resolved_path = resolve_path(
|
||||
&webview,
|
||||
&global_scope,
|
||||
&command_scope,
|
||||
path,
|
||||
options.as_ref().and_then(|o| o.base_dir),
|
||||
)?;
|
||||
|
||||
let metadata = resolved_path.metadata()?;
|
||||
|
||||
if metadata.is_file() {
|
||||
Ok(metadata.len())
|
||||
} else {
|
||||
let size = get_dir_size(&resolved_path).map_err(|e| {
|
||||
format!(
|
||||
"failed to get size at path: {} with error: {e}",
|
||||
resolved_path.display()
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_dir_size(path: &PathBuf) -> CommandResult<u64> {
|
||||
let mut size = 0;
|
||||
|
||||
for entry in std::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)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
pub fn resolve_file<R: Runtime>(
|
||||
webview: &Webview<R>,
|
||||
|
||||
@@ -417,6 +417,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
|
||||
commands::write_file,
|
||||
commands::write_text_file,
|
||||
commands::exists,
|
||||
commands::size,
|
||||
#[cfg(feature = "watch")]
|
||||
watcher::watch,
|
||||
#[cfg(feature = "watch")]
|
||||
|
||||
Reference in New Issue
Block a user