فهرست منبع

feat(core): validate dialog `default_path` (it must exist) (#1599)

Lucas Fernandes Nogueira 4 سال پیش
والد
کامیت
cfa74ebf68
3فایلهای تغییر یافته به همراه16 افزوده شده و 0 حذف شده
  1. 5 0
      .changes/dialog-default-path-validation.md
  2. 6 0
      core/tauri/src/endpoints/dialog.rs
  3. 5 0
      core/tauri/src/error.rs

+ 5 - 0
.changes/dialog-default-path-validation.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Validate dialog option `defaultPath` - it must exists.

+ 6 - 0
core/tauri/src/endpoints/dialog.rs

@@ -144,6 +144,9 @@ fn set_default_path(
 pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
   let mut dialog_builder = FileDialogBuilder::new();
   if let Some(default_path) = options.default_path {
+    if !default_path.exists() {
+      return Err(crate::Error::DialogDefaultPathNotExists(default_path));
+    }
     dialog_builder = set_default_path(dialog_builder, default_path);
   }
   for filter in options.filters {
@@ -165,6 +168,9 @@ pub fn open(options: OpenDialogOptions) -> crate::Result<InvokeResponse> {
 pub fn save(options: SaveDialogOptions) -> crate::Result<InvokeResponse> {
   let mut dialog_builder = FileDialogBuilder::new();
   if let Some(default_path) = options.default_path {
+    if !default_path.exists() {
+      return Err(crate::Error::DialogDefaultPathNotExists(default_path));
+    }
     dialog_builder = set_default_path(dialog_builder, default_path);
   }
   for filter in options.filters {

+ 5 - 0
core/tauri/src/error.rs

@@ -2,6 +2,8 @@
 // SPDX-License-Identifier: Apache-2.0
 // SPDX-License-Identifier: MIT
 
+use std::path::PathBuf;
+
 /// Runtime errors that can happen inside a Tauri application.
 #[derive(Debug, thiserror::Error)]
 pub enum Error {
@@ -60,6 +62,9 @@ pub enum Error {
   /// Error initializing plugin.
   #[error("failed to initialize plugin `{0}`: {1}")]
   PluginInitialization(String, String),
+  /// `default_path` provided to dialog API doesn't exist.
+  #[error("failed to setup dialog: provided default path `{0}` doesn't exist")]
+  DialogDefaultPathNotExists(PathBuf),
 }
 
 impl From<serde_json::Error> for Error {