Skip to content

Commit a0ecd81

Browse files
authored
fix(core): percent decode file drop payloads, closes #4034 (#4035)
1 parent 715cbde commit a0ecd81

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

.changes/file-drop-percent-decode.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime-wry": patch
3+
"tauri": patch
4+
---
5+
6+
The file drop event payloads are now percent-decoded.

core/tauri-runtime-wry/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ webview2-com = "0.13.0"
2828

2929
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
3030
gtk = { version = "0.15", features = [ "v3_20" ] }
31+
percent-encoding = "2.1"
3132

3233
[features]
3334
dox = [ "wry/dox" ]

core/tauri-runtime-wry/src/lib.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -927,11 +927,42 @@ impl WindowBuilder for WindowBuilderWrapper {
927927

928928
pub struct FileDropEventWrapper(WryFileDropEvent);
929929

930+
// on Linux, the paths are percent-encoded
931+
#[cfg(any(
932+
target_os = "linux",
933+
target_os = "dragonfly",
934+
target_os = "freebsd",
935+
target_os = "netbsd",
936+
target_os = "openbsd"
937+
))]
938+
fn decode_path(path: PathBuf) -> PathBuf {
939+
percent_encoding::percent_decode(path.display().to_string().as_bytes())
940+
.decode_utf8_lossy()
941+
.into_owned()
942+
.into()
943+
}
944+
945+
// on Windows and macOS, we do not need to decode the path
946+
#[cfg(not(any(
947+
target_os = "linux",
948+
target_os = "dragonfly",
949+
target_os = "freebsd",
950+
target_os = "netbsd",
951+
target_os = "openbsd"
952+
)))]
953+
fn decode_path(path: PathBuf) -> PathBuf {
954+
path
955+
}
956+
930957
impl From<FileDropEventWrapper> for FileDropEvent {
931958
fn from(event: FileDropEventWrapper) -> Self {
932959
match event.0 {
933-
WryFileDropEvent::Hovered(paths) => FileDropEvent::Hovered(paths),
934-
WryFileDropEvent::Dropped(paths) => FileDropEvent::Dropped(paths),
960+
WryFileDropEvent::Hovered(paths) => {
961+
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect())
962+
}
963+
WryFileDropEvent::Dropped(paths) => {
964+
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect())
965+
}
935966
// default to cancelled
936967
// FIXME(maybe): Add `FileDropEvent::Unknown` event?
937968
_ => FileDropEvent::Cancelled,

examples/api/dist/assets/index.js

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/dist/assets/vendor.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/Cargo.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src/App.svelte

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hotkeys from "hotkeys-js";
55
import { open } from "@tauri-apps/api/shell";
66
import { invoke } from "@tauri-apps/api/tauri";
7+
import { appWindow } from "@tauri-apps/api/window";
78
89
import Welcome from "./components/Welcome.svelte";
910
import Cli from "./components/Cli.svelte";
@@ -28,6 +29,10 @@
2829
});
2930
});
3031
32+
appWindow.listen('tauri://file-drop', function (event) {
33+
onMessage(`File drop: ${event.payload}`);
34+
});
35+
3136
const views = [
3237
{
3338
label: "Welcome",

0 commit comments

Comments
 (0)