Browse Source

feat(core): add `BaseDirectory::Temp` and `$TEMP` variable (#3763)

Lucas Fernandes Nogueira 3 years ago
parent
commit
266156a0b0

+ 6 - 0
.changes/temp-base-directory.md

@@ -0,0 +1,6 @@
+---
+"api": patch
+"tauri": patch
+---
+
+Added `Temp` to the `BaseDirectory` enum.

+ 5 - 0
.changes/temp-scope-variable.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Added `$TEMP` to the allowed variables to the filesystem and asset protocol scopes.

+ 2 - 2
core/tauri-utils/src/config.rs

@@ -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)]
@@ -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,
 

File diff suppressed because it is too large
+ 0 - 0
core/tauri/scripts/bundle.js


+ 11 - 1
core/tauri/src/api/path.rs

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

+ 4 - 2
tooling/api/src/fs.ts

@@ -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.
  *
@@ -89,7 +90,8 @@ export enum BaseDirectory {
   Video,
   Resource,
   App,
-  Log
+  Log,
+  Temp
 }
 
 interface FsOptions {

+ 2 - 2
tooling/cli/schema.json

@@ -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.",
@@ -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": {

Some files were not shown because too many files changed in this diff