Skip to content

Commit 0f72957

Browse files
SRutilelucasfernog
authored andcommitted
fix(fs): support any UTF-8 path in writeFile (tauri-apps#1640)
* In the `writeFile` function, when `options.baseDir` is not set, convert `path` to URL to avoid errors caused by Chinese characters. * fmt * use TextEncoder * use percent encoding * add change file * fmt --------- Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 12d035c commit 0f72957

File tree

6 files changed

+13
-5
lines changed

6 files changed

+13
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fs": patch
3+
---
4+
5+
Support any UTF-8 character in the writeFile API.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/fs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ glob = "0.3"
3131
notify = { version = "6", optional = true, features = ["serde"] }
3232
notify-debouncer-full = { version = "0.3", optional = true }
3333
dunce = { workspace = true }
34+
percent-encoding = "2"
3435

3536
[features]
3637
watch = ["notify", "notify-debouncer-full"]

plugins/fs/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/fs/guest-js/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ async function writeFile(
10151015

10161016
await invoke('plugin:fs|write_file', data, {
10171017
headers: {
1018-
path: path instanceof URL ? path.toString() : path,
1018+
path: encodeURIComponent(path instanceof URL ? path.toString() : path),
10191019
options: JSON.stringify(options)
10201020
}
10211021
})

plugins/fs/src/commands.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,11 @@ pub async fn write_file<R: Runtime>(
855855
.get("path")
856856
.ok_or_else(|| anyhow::anyhow!("missing file path").into())
857857
.and_then(|p| {
858-
p.to_str()
859-
.map_err(|e| anyhow::anyhow!("invalid path: {e}").into())
858+
percent_encoding::percent_decode(p.as_ref())
859+
.decode_utf8()
860+
.map_err(|_| anyhow::anyhow!("path is not a valid UTF-8").into())
860861
})
861-
.and_then(|p| SafeFilePath::from_str(p).map_err(CommandError::from))?;
862+
.and_then(|p| SafeFilePath::from_str(&p).map_err(CommandError::from))?;
862863
let options = request
863864
.headers()
864865
.get("options")

0 commit comments

Comments
 (0)