Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fs): support any UTF-8 path in writeFile #1640

Merged
merged 7 commits into from
Sep 11, 2024
Merged

Conversation

SRutile
Copy link
Contributor

@SRutile SRutile commented Aug 9, 2024

improper input parsing

In the writeFile function, when path is a string that contains Chinese characters or spaces, and options.baseDir is not provided, it can lead to errors. This PR introduces a fix: when path is a string and contains Chinese characters or spaces, it converts such paths to URL format, thus preventing these errors. As a result, paths obtained from save dialogs can be directly used in the writeFile function without requiring preliminary conversion to URL format.


在writeFile函数中,当path为字符串且包含中文字符或空格时,如果没有提供options.baseDir,会导致错误。此PR引入了一个修复方案:当path为字符串并且包含中文字符或空格时,将其转换为URL格式,以防止这些错误的发生。这样一来,通过保存对话框获取的路径可以被直接用于writeFile函数,而不需要预先转换为URL格式。

…rt `path` to URL to avoid errors caused by Chinese characters.
@SRutile SRutile requested a review from a team as a code owner August 9, 2024 06:38
Copy link
Contributor

github-actions bot commented Aug 9, 2024

Package Changes Through 841b5b1

There are 1 changes which include fs with prerelease

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
api-example 2.0.0-rc.5 2.0.0-rc.6
fs 2.0.0-rc.3 2.0.0-rc.4
dialog 2.0.0-rc.5 2.0.0-rc.6
http 2.0.0-rc.3 2.0.0-rc.4
persisted-scope 2.0.0-rc.3 2.0.0-rc.4

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@lucasfernog
Copy link
Member

is this a problem only for writeFile? i expected this to happen for any FS API call..
also which error do you have? i assume the path sent to the tauri side is incorrect right?

@SRutile
Copy link
Contributor Author

SRutile commented Aug 14, 2024

Only the writeFile function has this issue, as it uses request.body() to obtain the data. Normally, it should return Raw([]), but when there are Chinese characters or spaces, it returns Json(Object {"0": Number()}). To ensure it always returns data of the Raw type, I converted the path to URL format.

@@ -1009,6 +1009,10 @@ async function writeFile(
throw new TypeError("Must be a file URL.");
}

if (!options || !options.baseDir) {
path = path instanceof URL ? path : new URL(encodeURI(path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw if provided path is something like this ./file or just file, because new URL() expects a valid URL e.g. <scheme>://<domain>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware of this issue and appreciate you pointing it out. To resolve this, I decided to abandon using new URL() and instead used TextEncoder, adding decoding logic on the Rust side.

@lucasfernog lucasfernog changed the title fix(fs): Automatically convert non-ASCII and space-containing paths to URL format in writeFile fix(fs): support any UTF-8 path in writeFile Sep 11, 2024
@lucasfernog lucasfernog merged commit 9291e4d into tauri-apps:v2 Sep 11, 2024
37 checks passed
@xygengcn
Copy link

improper input parsing

In the writeFile function, when path is a string that contains Chinese characters or spaces, and options.baseDir is not provided, it can lead to errors. This PR introduces a fix: when path is a string and contains Chinese characters or spaces, it converts such paths to URL format, thus preventing these errors. As a result, paths obtained from save dialogs can be directly used in the writeFile function without requiring preliminary conversion to URL format.

在writeFile函数中,当path为字符串且包含中文字符或空格时,如果没有提供options.baseDir,会导致错误。此PR引入了一个修复方案:当path为字符串并且包含中文字符或空格时,将其转换为URL格式,以防止这些错误的发生。这样一来,通过保存对话框获取的路径可以被直接用于writeFile函数,而不需要预先转换为URL格式。

improper input parsing

In the writeFile function, when path is a string that contains Chinese characters or spaces, and options.baseDir is not provided, it can lead to errors. This PR introduces a fix: when path is a string and contains Chinese characters or spaces, it converts such paths to URL format, thus preventing these errors. As a result, paths obtained from save dialogs can be directly used in the writeFile function without requiring preliminary conversion to URL format.

在writeFile函数中,当path为字符串且包含中文字符或空格时,如果没有提供options.baseDir,会导致错误。此PR引入了一个修复方案:当path为字符串并且包含中文字符或空格时,将其转换为URL格式,以防止这些错误的发生。这样一来,通过保存对话框获取的路径可以被直接用于writeFile函数,而不需要预先转换为URL格式。

如果是mac的话,path没有file://协议开头,new URL会出错的,还要把path加上file

Sir-Thom pushed a commit to Sir-Thom/plugins-workspace that referenced this pull request Oct 22, 2024
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants