Skip to content

Commit 3c62dbc

Browse files
FabianLarsamrbashirlucasfernog
authored
feat(api): Add exists function to the fs module. (#5060)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com> Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
1 parent 255ebcb commit 3c62dbc

File tree

10 files changed

+116
-40
lines changed

10 files changed

+116
-40
lines changed

.changes/feat-exists-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"api": minor
3+
"tauri": minor
4+
---
5+
6+
Add `exists` function to the fs module.

core/tauri-utils/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,9 @@ pub struct FsAllowlistConfig {
12041204
/// Rename file from local filesystem.
12051205
#[serde(default, alias = "rename-file")]
12061206
pub rename_file: bool,
1207+
/// Check if path exists on the local filesystem.
1208+
#[serde(default)]
1209+
pub exists: bool,
12071210
}
12081211

12091212
impl Allowlist for FsAllowlistConfig {
@@ -1219,6 +1222,7 @@ impl Allowlist for FsAllowlistConfig {
12191222
remove_dir: true,
12201223
remove_file: true,
12211224
rename_file: true,
1225+
exists: true,
12221226
};
12231227
let mut features = allowlist.to_features();
12241228
features.push("fs-all");
@@ -1238,6 +1242,7 @@ impl Allowlist for FsAllowlistConfig {
12381242
check_feature!(self, features, remove_dir, "fs-remove-dir");
12391243
check_feature!(self, features, remove_file, "fs-remove-file");
12401244
check_feature!(self, features, rename_file, "fs-rename-file");
1245+
check_feature!(self, features, exists, "fs-exists");
12411246
features
12421247
}
12431248
}

core/tauri/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ dialog-save = [ "dialog" ]
195195
fs-all = [
196196
"fs-copy-file",
197197
"fs-create-dir",
198+
"fs-exists",
198199
"fs-read-file",
199200
"fs-read-dir",
200201
"fs-remove-dir",
@@ -204,6 +205,7 @@ fs-all = [
204205
]
205206
fs-copy-file = [ ]
206207
fs-create-dir = [ ]
208+
fs-exists = [ ]
207209
fs-read-file = [ ]
208210
fs-read-dir = [ ]
209211
fs-remove-dir = [ ]

core/tauri/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ fn main() {
5959
"remove-dir",
6060
"remove-file",
6161
"rename-file",
62+
"exists",
6263
],
6364
api_all,
6465
);

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/endpoints/file_system.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ pub(crate) enum Cmd {
114114
new_path: SafePathBuf,
115115
options: Option<FileOperationOptions>,
116116
},
117+
/// The exists API.
118+
#[cmd(fs_exists, "fs > exists")]
119+
Exists {
120+
path: SafePathBuf,
121+
options: Option<FileOperationOptions>,
122+
},
117123
}
118124

119125
impl Cmd {
@@ -339,6 +345,22 @@ impl Cmd {
339345
.with_context(|| format!("old: {}, new: {}", old.display(), new.display()))
340346
.map_err(Into::into)
341347
}
348+
349+
#[module_command_handler(fs_exists)]
350+
fn exists<R: Runtime>(
351+
context: InvokeContext<R>,
352+
path: SafePathBuf,
353+
options: Option<FileOperationOptions>,
354+
) -> super::Result<bool> {
355+
let resolved_path = resolve_path(
356+
&context.config,
357+
&context.package_info,
358+
&context.window,
359+
path,
360+
options.and_then(|o| o.dir),
361+
)?;
362+
Ok(resolved_path.as_ref().exists())
363+
}
342364
}
343365

344366
#[allow(dead_code)]
@@ -474,4 +496,11 @@ mod tests {
474496
);
475497
crate::test_utils::assert_not_allowlist_error(res);
476498
}
499+
500+
#[tauri_macros::module_command_test(fs_exists, "fs > exists")]
501+
#[quickcheck_macros::quickcheck]
502+
fn exists(path: SafePathBuf, options: Option<FileOperationOptions>) {
503+
let res = super::Cmd::exists(crate::test::mock_invoke_context(), path, options);
504+
crate::test_utils::assert_not_allowlist_error(res);
505+
}
477506
}

core/tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
//! - **fs-all**: Enables all [Filesystem APIs](https://tauri.app/en/docs/api/js/modules/fs).
7070
//! - **fs-copy-file**: Enables the [`copyFile` API](https://tauri.app/en/docs/api/js/modules/fs#copyfile).
7171
//! - **fs-create-dir**: Enables the [`createDir` API](https://tauri.app/en/docs/api/js/modules/fs#createdir).
72+
//! - **fs-exists**: Enables the [`exists` API](https://tauri.app/en/docs/api/js/modules/fs#exists).
7273
//! - **fs-read-dir**: Enables the [`readDir` API](https://tauri.app/en/docs/api/js/modules/fs#readdir).
7374
//! - **fs-read-file**: Enables the [`readTextFile` API](https://tauri.app/en/docs/api/js/modules/fs#readtextfile) and the [`readBinaryFile` API](https://tauri.app/en/docs/api/js/modules/fs#readbinaryfile).
7475
//! - **fs-remove-dir**: Enables the [`removeDir` API](https://tauri.app/en/docs/api/js/modules/fs#removedir).

examples/api/dist/assets/index.js

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/fs.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
* "createDir": true,
2222
* "removeDir": true,
2323
* "removeFile": true,
24-
* "renameFile": true
24+
* "renameFile": true,
25+
* "exists": true
2526
* }
2627
* }
2728
* }
@@ -550,6 +551,28 @@ async function renameFile(
550551
})
551552
}
552553

554+
/**
555+
* Check if a path exists.
556+
* @example
557+
* ```typescript
558+
* import { exists, BaseDirectory } from '@tauri-apps/api/fs';
559+
* // Check if the `$APPDIR/avatar.png` file exists
560+
* await exists('avatar.png', { dir: BaseDirectory.App });
561+
* ```
562+
*
563+
* @since 1.1.0
564+
*/
565+
async function exists(path: string, options: FsOptions = {}): Promise<void> {
566+
return invokeTauriCommand({
567+
__tauriModule: 'Fs',
568+
message: {
569+
cmd: 'exists',
570+
path,
571+
options
572+
}
573+
})
574+
}
575+
553576
export type {
554577
FsOptions,
555578
FsDirOptions,
@@ -571,5 +594,6 @@ export {
571594
removeDir,
572595
copyFile,
573596
removeFile,
574-
renameFile
597+
renameFile,
598+
exists
575599
}

tooling/cli/schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"all": false,
4646
"copyFile": false,
4747
"createDir": false,
48+
"exists": false,
4849
"readDir": false,
4950
"readFile": false,
5051
"removeDir": false,
@@ -306,6 +307,7 @@
306307
"all": false,
307308
"copyFile": false,
308309
"createDir": false,
310+
"exists": false,
309311
"readDir": false,
310312
"readFile": false,
311313
"removeDir": false,
@@ -1498,6 +1500,7 @@
14981500
"all": false,
14991501
"copyFile": false,
15001502
"createDir": false,
1503+
"exists": false,
15011504
"readDir": false,
15021505
"readFile": false,
15031506
"removeDir": false,
@@ -1735,6 +1738,11 @@
17351738
"description": "Rename file from local filesystem.",
17361739
"default": false,
17371740
"type": "boolean"
1741+
},
1742+
"exists": {
1743+
"description": "Check if path exists on the local filesystem.",
1744+
"default": false,
1745+
"type": "boolean"
17381746
}
17391747
},
17401748
"additionalProperties": false

0 commit comments

Comments
 (0)