feat: add convert_file_src function to path module #14816
Closed
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