feat: add tempDir to path plugin (#7144)

Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2023-06-06 20:51:29 +03:00 committed by GitHub
parent f2d68cf7d4
commit 4652c446b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 12 deletions

5
.changes/tempdir-api.md Normal file
View File

@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'patch'
---
Add `tempDir` function to `path` module

5
.changes/tempdir-core.md Normal file
View File

@ -0,0 +1,5 @@
---
'tauri': 'patch'
---
Add `temp_dir` method to `PathResolver`

File diff suppressed because one or more lines are too long

View File

@ -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())
}
}

View File

@ -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())
}
}

View File

@ -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},
@ -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)]
@ -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}`.
@ -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(),

File diff suppressed because one or more lines are too long

View File

@ -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
@ -683,5 +699,6 @@ export {
dirname,
extname,
basename,
isAbsolute
isAbsolute,
tempDir
}