From bb51a41d67ebf989e8aedf10c4b1a7f9514d1bdf Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Wed, 17 Apr 2024 15:49:32 +0200 Subject: [PATCH] feat: Remove UNC prefix in paths returned to the frontend (#1168) * feat: Remove UNC prefix in paths returned to the frontend * that one doesn't count * map instead of mut * revert accidental ipc::response change * move dunce to workspace root --- .changes/remove-unc-path-prefix.md | 7 +++++++ Cargo.lock | 2 ++ Cargo.toml | 1 + plugins/dialog/Cargo.toml | 1 + plugins/dialog/src/commands.rs | 26 +++++++++++++++++++++----- plugins/store/Cargo.toml | 1 + plugins/store/src/store.rs | 3 ++- 7 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 .changes/remove-unc-path-prefix.md diff --git a/.changes/remove-unc-path-prefix.md b/.changes/remove-unc-path-prefix.md new file mode 100644 index 00000000..2c569f4e --- /dev/null +++ b/.changes/remove-unc-path-prefix.md @@ -0,0 +1,7 @@ +--- +dialog: patch +fs: patch +store: patch +--- + +**Breaking Change:** All apis that return paths to the frontend will now remove the `\\?\` UNC prefix on Windows. diff --git a/Cargo.lock b/Cargo.lock index 80412450..be9f4aec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6227,6 +6227,7 @@ dependencies = [ name = "tauri-plugin-dialog" version = "2.0.0-beta.4" dependencies = [ + "dunce", "log", "raw-window-handle 0.6.0", "rfd", @@ -6464,6 +6465,7 @@ dependencies = [ name = "tauri-plugin-store" version = "2.0.0-beta.4" dependencies = [ + "dunce", "log", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 56d6a375..e4bd40a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ serde_json = "1" thiserror = "1" url = "2" schemars = "0.8" +dunce = "1" [workspace.package] edition = "2021" diff --git a/plugins/dialog/Cargo.toml b/plugins/dialog/Cargo.toml index 165b493e..4db6b077 100644 --- a/plugins/dialog/Cargo.toml +++ b/plugins/dialog/Cargo.toml @@ -23,6 +23,7 @@ serde_json = { workspace = true } tauri = { workspace = true } log = { workspace = true } thiserror = { workspace = true } +dunce = { workspace = true } tauri-plugin-fs = { path = "../fs", version = "2.0.0-beta.4" } [target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies] diff --git a/plugins/dialog/src/commands.rs b/plugins/dialog/src/commands.rs index f0dfb092..0d4a0de9 100644 --- a/plugins/dialog/src/commands.rs +++ b/plugins/dialog/src/commands.rs @@ -129,7 +129,12 @@ pub(crate) async fn open( } } } - OpenResponse::Folders(folders) + OpenResponse::Folders(folders.map(|folders| { + folders + .iter() + .map(|p| dunce::simplified(p).to_path_buf()) + .collect() + })) } else { let folder = dialog_builder.blocking_pick_folder(); if let Some(path) = &folder { @@ -137,7 +142,7 @@ pub(crate) async fn open( s.allow_directory(path, options.recursive); } } - OpenResponse::Folder(folder) + OpenResponse::Folder(folder.map(|p| dunce::simplified(&p).to_path_buf())) } } #[cfg(mobile)] @@ -154,7 +159,15 @@ pub(crate) async fn open( .allow_file(&file.path)?; } } - OpenResponse::Files(files) + OpenResponse::Files(files.map(|files| { + files + .into_iter() + .map(|mut f| { + f.path = dunce::simplified(&f.path).to_path_buf(); + f + }) + .collect() + })) } else { let file = dialog_builder.blocking_pick_file(); if let Some(file) = &file { @@ -165,7 +178,10 @@ pub(crate) async fn open( .state::() .allow_file(&file.path)?; } - OpenResponse::File(file) + OpenResponse::File(file.map(|mut f| { + f.path = dunce::simplified(&f.path).to_path_buf(); + f + })) }; Ok(res) } @@ -208,7 +224,7 @@ pub(crate) async fn save( window.state::().allow_file(p)?; } - Ok(path) + Ok(path.map(|p| dunce::simplified(&p).to_path_buf())) } } diff --git a/plugins/store/Cargo.toml b/plugins/store/Cargo.toml index 30b6c49d..04eabb52 100644 --- a/plugins/store/Cargo.toml +++ b/plugins/store/Cargo.toml @@ -22,3 +22,4 @@ serde_json = { workspace = true } tauri = { workspace = true } log = { workspace = true } thiserror = { workspace = true } +dunce = { workspace = true } \ No newline at end of file diff --git a/plugins/store/src/store.rs b/plugins/store/src/store.rs index 9437ada1..efd10984 100644 --- a/plugins/store/src/store.rs +++ b/plugins/store/src/store.rs @@ -58,7 +58,8 @@ impl StoreBuilder { /// ``` pub fn new>(path: P) -> Self { Self { - path: path.as_ref().to_path_buf(), + // Since Store.path is only exposed to the user in emit calls we may as well simplify it here already. + path: dunce::simplified(path.as_ref()).to_path_buf(), defaults: None, cache: Default::default(), serialize: default_serialize,