Skip to content

Commit

Permalink
fix(fs): support any UTF-8 path in writeFile (#1640)
Browse files Browse the repository at this point in the history
* 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 <lucas@tauri.studio>
  • Loading branch information
SRutile and lucasfernog authored Sep 11, 2024
1 parent 3715f3c commit 9291e4d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/fs-write-file-utf8-chars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fs": patch
---

Support any UTF-8 character in the writeFile API.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ glob = "0.3"
notify = { version = "6", optional = true, features = ["serde"] }
notify-debouncer-full = { version = "0.3", optional = true }
dunce = { workspace = true }
percent-encoding = "2"

[features]
watch = ["notify", "notify-debouncer-full"]
2 changes: 1 addition & 1 deletion plugins/fs/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ async function writeFile(

await invoke('plugin:fs|write_file', data, {
headers: {
path: path instanceof URL ? path.toString() : path,
path: encodeURIComponent(path instanceof URL ? path.toString() : path),
options: JSON.stringify(options)
}
})
Expand Down
7 changes: 4 additions & 3 deletions plugins/fs/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,11 @@ pub async fn write_file<R: Runtime>(
.get("path")
.ok_or_else(|| anyhow::anyhow!("missing file path").into())
.and_then(|p| {
p.to_str()
.map_err(|e| anyhow::anyhow!("invalid path: {e}").into())
percent_encoding::percent_decode(p.as_ref())
.decode_utf8()
.map_err(|_| anyhow::anyhow!("path is not a valid UTF-8").into())
})
.and_then(|p| SafeFilePath::from_str(p).map_err(CommandError::from))?;
.and_then(|p| SafeFilePath::from_str(&p).map_err(CommandError::from))?;
let options = request
.headers()
.get("options")
Expand Down

0 comments on commit 9291e4d

Please sign in to comment.