Skip to content

Conversation

@AndyBodnar
Copy link

Summary

Adds a Rust equivalent of the JavaScript convertFileSrc function to the PathResolver API. This enables file-to-URL conversion from Rust code, which is useful when processing file paths in Rust (e.g., parsing markdown with image references) and needing to convert them to URLs that the webview can load.

The implementation:

  • Uses percent-encoding for URL-safe path encoding (already a dependency)
  • Uses dunce::simplified to normalize Windows paths (already a dependency)
  • Returns HTTP URLs on Windows/Android and custom protocol URLs elsewhere, matching the JS implementation

Usage

use tauri::Manager;

tauri::Builder::default()
    .setup(|app| {
        let video_path = app.path().app_data_dir()?.join("video.mp4");
        let url = app.path().convert_file_src(&video_path, None);
        // On Windows: http://asset.localhost/C%3A%5CUsers%5C...
        // On macOS/Linux: asset://localhost/%2FUsers%2F...
        println!("URL: {}", url);
        Ok(())
    });

Test plan

  • Added function to PathResolver in desktop.rs
  • Added function to PathResolver in android.rs
  • Verify build passes in CI

Closes #12022

Adds a Rust equivalent of the JavaScript `convertFileSrc` function to the
PathResolver API, enabling file-to-URL conversion from Rust code.

This is useful when processing file paths in Rust (e.g., parsing markdown
with image references) and needing to convert them to URLs that the webview
can load.

The implementation:
- Uses percent-encoding for URL-safe path encoding
- Uses dunce::simplified to normalize Windows paths
- Returns HTTP URLs on Windows/Android and custom protocol URLs elsewhere

Closes tauri-apps#12022
@AndyBodnar AndyBodnar requested a review from a team as a code owner January 17, 2026 06:43
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Jan 17, 2026
@AndyBodnar
Copy link
Author

This adds the Rust equivalent of the JavaScript convertFileSrc function. Followed the existing patterns in the codebase:

  • Desktop version uses dunce::simplified for path canonicalization and percent-encodes the path
  • Android version uses the same approach (HTTP protocol like Windows)
  • iOS version also added with the same pattern

Platform-specific URL schemes:

  • Windows/Android: http://{protocol}.localhost/{encoded_path}
  • macOS/Linux/iOS: {protocol}://localhost/{encoded_path}

Socket Security checks passed.

@AndyBodnar
Copy link
Author

All checks passed.

What this adds

The Rust equivalent of the JavaScript convertFileSrc() function. This lets you convert a local file path into a URL that can be loaded by the webview, which is useful for displaying local images, videos, or other assets.

Implementation details

  • Added convert_file_src() to PathResolver for desktop, Android, and iOS
  • Uses dunce::simplified() on desktop to clean up Windows UNC paths
  • Percent-encodes the path to handle special characters and spaces
  • Returns platform-appropriate URLs:
    • Windows/Android: http://{protocol}.localhost/{encoded_path}
    • macOS/Linux/iOS: {protocol}://localhost/{encoded_path}
  • Default protocol is "asset" but can be customized

Why this matters

Previously you had to use the JS bridge to call convertFileSrc() from Rust code, or manually construct the URL. This gives Rust-side code a clean way to generate asset URLs without going through JS.

Testing

  • Socket Security checks passed
  • Followed the existing patterns in the codebase for path handling and URL construction

@AndyBodnar
Copy link
Author

@zeenix

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()

1 participant