Skip to content

Conversation

@ddmoney420
Copy link

Summary

Adds a Rust equivalent of the JavaScript convertFileSrc() function, allowing backend code to convert device file paths to URLs that can be loaded by the webview.

Motivation

This is useful when processing file paths in Rust (e.g., markdown parsers, image processors) that need to generate URLs for the webview to load assets. Currently this requires hacky workarounds like calling JavaScript from Rust or manually implementing the URL transformation.

Implementation

The function in tauri::path::convert_file_src:

  • URL encodes the file path using percent_encoding
  • Uses platform-appropriate scheme (http:// on Windows/Android, custom protocol on other platforms)
  • Supports custom protocols via optional parameter
  • Matches the behavior of the JavaScript convertFileSrc() exactly

Usage

use tauri::path::convert_file_src;

// Default "asset" protocol
let url = convert_file_src("/path/to/video.mp4", None);
// Returns: "asset://localhost/%2Fpath%2Fto%2Fvideo.mp4" (on macOS/Linux)
// Returns: "http://asset.localhost/%2Fpath%2Fto%2Fvideo.mp4" (on Windows/Android)

// Custom protocol
let url = convert_file_src("/path/to/file", Some("custom"));

Test plan

  • Added unit tests for default protocol, custom protocol, and special character handling
  • Tests verify platform-specific URL schemes

Fixes #12022

🤖 Generated with Claude Code

Adds a Rust equivalent of the JavaScript convertFileSrc() function,
allowing backend code to convert device file paths to URLs that can
be loaded by the webview.

This is useful when processing file paths in Rust (e.g., markdown
parsers) that need to generate URLs for the webview to load assets.

The function:
- URL encodes the file path
- Uses platform-appropriate scheme (http:// on Windows/Android,
  custom protocol on other platforms)
- Supports custom protocols via optional parameter

Fixes tauri-apps#12022

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@ddmoney420 ddmoney420 requested a review from a team as a code owner January 29, 2026 20:27
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Jan 29, 2026
@FabianLars
Copy link
Member

Thanks for the PR but just like #14816 i'll close it in favor of #14786.
Funny that all 3 PRs made the same mistake of missing the https option though 😅

@FabianLars FabianLars closed this Jan 29, 2026
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