Skip to content

Conversation

@LouisLau-art
Copy link

Summary

Adds a new public method PathResolver::convert_file_src that converts a file path to a Tauri asset URL. This function is useful when you need to reference local files in your frontend (e.g., in markdown editors where you're parsing markdown to HTML in Rust).

Implementation

  • Uses dunce to canonicalize paths (normalizes paths like C:\ on Windows)
  • Uses percent-encoding to URL-encode the path
  • All dependencies (dunce, percent-encoding) are already in Cargo.toml

Platform-specific behavior

  • Windows and Android: Returns URLs in format http://asset.localhost/<path>
  • Other platforms: Returns URLs in format asset://localhost/<path>

Example usage

```rust
use tauri::Manager;
use std::path::Path;

tauri::Builder::default()
.setup(|app| {
let path = Path::new("/path/to/image.png");
let asset_url = app.path().convert_file_src(path)?;
println!("Asset URL: {}", asset_url);
Ok(())
});
```

Motivation

Previously, users had to manually call the JavaScript version of convertFileSrc() from Rust, which was hacky. This provides a native Rust way to convert file paths to Tauri asset URLs.

Closes #12022

Adds a new public method \`PathResolver::convert_file_src\` that converts a file path to a Tauri asset URL. This is useful when you need to reference local files in your frontend (e.g., in markdown editors where you're parsing markdown to HTML in Rust).

The function:
- On Windows/Android: Returns URLs in format \`http://asset.localhost/<path>\`
- On other platforms: Returns URLs in format \`asset://localhost/<path>\`

Implementation details:
- Uses \`dunce\` to canonicalize paths (normalizes paths like \`C:\` on Windows)
- Uses \`percent-encoding\` to URL-encode the path
- All dependencies (\`dunce\`, \`percent-encoding\`) are already in Cargo.toml

Fixes tauri-apps#12022
@LouisLau-art LouisLau-art requested a review from a team as a code owner January 23, 2026 17:45
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Jan 23, 2026
LouisLau-art pushed a commit to LouisLau-art/tech-blog that referenced this pull request Jan 23, 2026
添加了一篇关于为 Tauri 项目贡献 convert_file_src 函数的博文,详细记录了:
- PR 链接:tauri-apps/tauri#14816
- 技术实现细节
- 贡献流程
- 技术心得
@github-actions
Copy link
Contributor

Package Changes Through fb2d3fc

There are 9 changes which include tauri with minor, @tauri-apps/cli with minor, tauri-cli with minor, tauri-utils with patch, tauri-build with patch, tauri-macos-sign with patch, tauri-bundler with minor, tauri-runtime-wry with minor, tauri-runtime with minor

Planned Package Versions

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

package current next
tauri-utils 2.8.1 2.8.2
tauri-macos-sign 2.3.2 2.3.3
tauri-bundler 2.7.5 2.8.0
tauri-runtime 2.9.2 2.10.0
tauri-runtime-wry 2.9.3 2.10.0
tauri-codegen 2.5.2 2.5.3
tauri-macros 2.5.2 2.5.3
tauri-plugin 2.5.2 2.5.3
tauri-build 2.5.3 2.5.4
tauri 2.9.5 2.10.0
@tauri-apps/cli 2.9.6 2.10.0
tauri-cli 2.9.6 2.10.0

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


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

@Legend-Master
Copy link
Contributor

Hey, thanks for contributing

It seems like someone has been working on this already at #14786

@LouisLau-art
Copy link
Author

Thanks for the heads-up! I see #14786 covers this. I'll close this PR to avoid duplication. Happy to help review or contribute there if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📬Proposal

Development

Successfully merging this pull request may close these issues.

[feat] Rust equivalent convertFileSrc()

2 participants