feat: add convert_file_src function to path module#14816
Closed
LouisLau-art wants to merge 1 commit into
Closed
Conversation
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
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 - 技术实现细节 - 贡献流程 - 技术心得
Contributor
Package Changes Through fb2d3fcThere 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 VersionsThe following package releases are the planned based on the context of changes in this pull request.
Add another change file through the GitHub UI by following this link. Read about change files or the docs at github.com/jbolda/covector |
Contributor
|
Hey, thanks for contributing It seems like someone has been working on this already at #14786 |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new public method
PathResolver::convert_file_srcthat 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
dunceto canonicalize paths (normalizes paths likeC:\on Windows)percent-encodingto URL-encode the pathdunce,percent-encoding) are already in Cargo.tomlPlatform-specific behavior
http://asset.localhost/<path>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