Skip to content

Commit

Permalink
feat: add tempDir to path plugin (#7144)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Jun 6, 2023
1 parent f2d68cf commit 4652c44
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/tempdir-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'patch'
---

Add `tempDir` function to `path` module
5 changes: 5 additions & 0 deletions .changes/tempdir-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': 'patch'
---

Add `temp_dir` method to `PathResolver`
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

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

5 changes: 5 additions & 0 deletions core/tauri/src/path/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ impl<R: Runtime> PathResolver<R> {
.call_resolve("getConfigDir")
.map(|dir| dir.join("logs"))
}

/// A temporary directory. Resolves to [`std::env::temp_dir`].
pub fn temp_dir(&self) -> Result<PathBuf> {
Ok(std::env::temp_dir())
}
}
5 changes: 5 additions & 0 deletions core/tauri/src/path/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,9 @@ impl<R: Runtime> PathResolver<R> {

path
}

/// A temporary directory. Resolves to [`std::env::temp_dir`].
pub fn temp_dir(&self) -> Result<PathBuf> {
Ok(std::env::temp_dir())
}
}
14 changes: 5 additions & 9 deletions core/tauri/src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{
env::temp_dir,
path::{Component, Display, Path, PathBuf},
};
use std::path::{Component, Display, Path, PathBuf};

use crate::{
plugin::{Builder, TauriPlugin},
Expand All @@ -26,9 +23,9 @@ mod android;
mod desktop;

#[cfg(target_os = "android")]
pub(crate) use android::PathResolver;
pub use android::PathResolver;
#[cfg(not(target_os = "android"))]
pub(crate) use desktop::PathResolver;
pub use desktop::PathResolver;

/// A wrapper for [`PathBuf`] that prevents path traversal.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -100,8 +97,7 @@ pub enum BaseDirectory {
Video,
/// The Resource directory.
Resource,
/// A temporary directory.
/// Resolves to [`temp_dir`].
/// A temporary directory. Resolves to [`std::env::temp_dir`].
Temp,
/// The default app config directory.
/// Resolves to [`BaseDirectory::Config`]`/{bundle_identifier}`.
Expand Down Expand Up @@ -293,7 +289,7 @@ fn resolve_path<R: Runtime>(
BaseDirectory::Public => resolver.public_dir(),
BaseDirectory::Video => resolver.video_dir(),
BaseDirectory::Resource => resolver.resource_dir(),
BaseDirectory::Temp => Ok(temp_dir()),
BaseDirectory::Temp => resolver.temp_dir(),
BaseDirectory::AppConfig => resolver.app_config_dir(),
BaseDirectory::AppData => resolver.app_data_dir(),
BaseDirectory::AppLocalData => resolver.app_local_data_dir(),
Expand Down
2 changes: 1 addition & 1 deletion tooling/api/docs/js-api.json

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion tooling/api/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,22 @@ async function appLogDir(): Promise<string> {
})
}

/**
* Returns a temporary directory.
* @example
* ```typescript
* import { tempDir } from '@tauri-apps/api/path';
* const temp = await tempDir();
* ```
*
* @since 2.0.0
*/
async function tempDir(path: string): Promise<string> {
return invoke('plugin:path|resolve_directory', {
directory: BaseDirectory.Temp
})
}

/**
* Provides the platform-specific path segment separator:
* - `\` on Windows
Expand Down Expand Up @@ -683,5 +699,6 @@ export {
dirname,
extname,
basename,
isAbsolute
isAbsolute,
tempDir
}

0 comments on commit 4652c44

Please sign in to comment.