Skip to content

Commit 4652c44

Browse files
feat: add tempDir to path plugin (#7144)
Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
1 parent f2d68cf commit 4652c44

File tree

8 files changed

+45
-12
lines changed

8 files changed

+45
-12
lines changed

.changes/tempdir-api.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'patch'
3+
---
4+
5+
Add `tempDir` function to `path` module

.changes/tempdir-core.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch'
3+
---
4+
5+
Add `temp_dir` method to `PathResolver`

core/tauri/scripts/bundle.global.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/path/android.rs

+5
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,9 @@ impl<R: Runtime> PathResolver<R> {
112112
.call_resolve("getConfigDir")
113113
.map(|dir| dir.join("logs"))
114114
}
115+
116+
/// A temporary directory. Resolves to [`std::env::temp_dir`].
117+
pub fn temp_dir(&self) -> Result<PathBuf> {
118+
Ok(std::env::temp_dir())
119+
}
115120
}

core/tauri/src/path/desktop.rs

+5
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,9 @@ impl<R: Runtime> PathResolver<R> {
254254

255255
path
256256
}
257+
258+
/// A temporary directory. Resolves to [`std::env::temp_dir`].
259+
pub fn temp_dir(&self) -> Result<PathBuf> {
260+
Ok(std::env::temp_dir())
261+
}
257262
}

core/tauri/src/path/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::{
6-
env::temp_dir,
7-
path::{Component, Display, Path, PathBuf},
8-
};
5+
use std::path::{Component, Display, Path, PathBuf};
96

107
use crate::{
118
plugin::{Builder, TauriPlugin},
@@ -26,9 +23,9 @@ mod android;
2623
mod desktop;
2724

2825
#[cfg(target_os = "android")]
29-
pub(crate) use android::PathResolver;
26+
pub use android::PathResolver;
3027
#[cfg(not(target_os = "android"))]
31-
pub(crate) use desktop::PathResolver;
28+
pub use desktop::PathResolver;
3229

3330
/// A wrapper for [`PathBuf`] that prevents path traversal.
3431
#[derive(Clone, Debug)]
@@ -100,8 +97,7 @@ pub enum BaseDirectory {
10097
Video,
10198
/// The Resource directory.
10299
Resource,
103-
/// A temporary directory.
104-
/// Resolves to [`temp_dir`].
100+
/// A temporary directory. Resolves to [`std::env::temp_dir`].
105101
Temp,
106102
/// The default app config directory.
107103
/// Resolves to [`BaseDirectory::Config`]`/{bundle_identifier}`.
@@ -293,7 +289,7 @@ fn resolve_path<R: Runtime>(
293289
BaseDirectory::Public => resolver.public_dir(),
294290
BaseDirectory::Video => resolver.video_dir(),
295291
BaseDirectory::Resource => resolver.resource_dir(),
296-
BaseDirectory::Temp => Ok(temp_dir()),
292+
BaseDirectory::Temp => resolver.temp_dir(),
297293
BaseDirectory::AppConfig => resolver.app_config_dir(),
298294
BaseDirectory::AppData => resolver.app_data_dir(),
299295
BaseDirectory::AppLocalData => resolver.app_local_data_dir(),

tooling/api/docs/js-api.json

+1-1
Large diffs are not rendered by default.

tooling/api/src/path.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ async function appLogDir(): Promise<string> {
524524
})
525525
}
526526

527+
/**
528+
* Returns a temporary directory.
529+
* @example
530+
* ```typescript
531+
* import { tempDir } from '@tauri-apps/api/path';
532+
* const temp = await tempDir();
533+
* ```
534+
*
535+
* @since 2.0.0
536+
*/
537+
async function tempDir(path: string): Promise<string> {
538+
return invoke('plugin:path|resolve_directory', {
539+
directory: BaseDirectory.Temp
540+
})
541+
}
542+
527543
/**
528544
* Provides the platform-specific path segment separator:
529545
* - `\` on Windows
@@ -683,5 +699,6 @@ export {
683699
dirname,
684700
extname,
685701
basename,
686-
isAbsolute
702+
isAbsolute,
703+
tempDir
687704
}

0 commit comments

Comments
 (0)