Skip to content

Commit

Permalink
feat(core): add BaseDirectory::Temp and $TEMP variable (#3763)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Mar 24, 2022
1 parent 6054e98 commit 266156a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changes/temp-base-directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"api": patch
"tauri": patch
---

Added `Temp` to the `BaseDirectory` enum.
5 changes: 5 additions & 0 deletions .changes/temp-scope-variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Added `$TEMP` to the allowed variables to the filesystem and asset protocol scopes.
4 changes: 2 additions & 2 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ macro_rules! check_feature {
/// Each pattern can start with a variable that resolves to a system base directory.
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(untagged)]
Expand Down Expand Up @@ -1071,7 +1071,7 @@ pub struct ShellAllowedCommand {
/// It can start with a variable that resolves to a system base directory.
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.
#[serde(rename = "cmd")]
pub command: PathBuf,

Expand Down
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion core/tauri/src/api/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

//! Types and functions related to file system path operations.

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

use crate::{Config, Env, PackageInfo};

Expand Down Expand Up @@ -61,6 +64,9 @@ pub enum BaseDirectory {
/// Resolves to `BaseDirectory::Home/Library/Logs/{bundle_identifier}` on macOS
/// and `BaseDirectory::Config/{bundle_identifier}/logs` on linux and windows.
Log,
/// A temporary directory.
/// Resolves to [`temp_dir`].
Temp,
}

impl BaseDirectory {
Expand All @@ -86,6 +92,7 @@ impl BaseDirectory {
Self::Resource => "$RESOURCE",
Self::App => "$APP",
Self::Log => "$LOG",
Self::Temp => "$TEMP",
}
}

Expand All @@ -111,6 +118,7 @@ impl BaseDirectory {
"$RESOURCE" => Self::Resource,
"$APP" => Self::App,
"$LOG" => Self::Log,
"$TEMP" => Self::Temp,
_ => return None,
};
Some(res)
Expand Down Expand Up @@ -162,6 +170,7 @@ pub fn parse<P: AsRef<Path>>(
}
p.push(component);
}
println!("res {:?}", p);

Ok(p)
}
Expand Down Expand Up @@ -236,6 +245,7 @@ pub fn resolve_path<P: AsRef<Path>>(
BaseDirectory::Resource => resource_dir(package_info, env),
BaseDirectory::App => app_dir(config),
BaseDirectory::Log => log_dir(config),
BaseDirectory::Temp => Some(temp_dir()),
};
if let Some(mut base_dir_path_value) = base_dir_path {
// use the same path resolution mechanism as the bundler's resource injection algorithm
Expand Down
6 changes: 4 additions & 2 deletions tooling/api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
* [[path.localDataDir | `$LOCALDATA`]], [[path.desktopDir | `$DESKTOP`]], [[path.documentDir | `$DOCUMENT`]],
* [[path.downloadDir | `$DOWNLOAD`]], [[path.executableDir | `$EXE`]], [[path.fontDir | `$FONT`]], [[path.homeDir | `$HOME`]],
* [[path.pictureDir | `$PICTURE`]], [[path.publicDir | `$PUBLIC`]], [[path.runtimeDir | `$RUNTIME`]],
* [[path.templateDir | `$TEMPLATE`]], [[path.videoDir | `$VIDEO`]], [[path.resourceDir | `$RESOURCE`]], [[path.appDir | `$APP`]].
* [[path.templateDir | `$TEMPLATE`]], [[path.videoDir | `$VIDEO`]], [[path.resourceDir | `$RESOURCE`]], [[path.appDir | `$APP`]],
* [[path.logDir | `$LOG`]], [[os.tempdir | `$TEMP`]].
*
* Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
*
Expand Down Expand Up @@ -89,7 +90,8 @@ export enum BaseDirectory {
Video,
Resource,
App,
Log
Log,
Temp
}

interface FsOptions {
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@
"additionalProperties": false
},
"FsAllowlistScope": {
"description": "Filesystem scope definition. It is a list of glob patterns that restrict the API access from the webview.\n\nEach pattern can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.",
"description": "Filesystem scope definition. It is a list of glob patterns that restrict the API access from the webview.\n\nEach pattern can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.",
"anyOf": [
{
"description": "A list of paths that are allowed by this scope.",
Expand Down Expand Up @@ -1429,7 +1429,7 @@
]
},
"cmd": {
"description": "The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`.",
"description": "The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.",
"type": "string"
},
"name": {
Expand Down

0 comments on commit 266156a

Please sign in to comment.