Skip to content

Commit

Permalink
feat: asset custom protocol to access local file system (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) committed Jun 28, 2021
1 parent 628a53e commit ee60e42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/asset-protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Add asset custom protocol to access local file system.
2 changes: 1 addition & 1 deletion core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ normal = [ "attohttpc" ]
[dependencies]
serde_json = { version = "1.0", features = [ "raw_value" ] }
serde = { version = "1.0", features = [ "derive" ] }
tokio = { version = "1.7", features = [ "rt", "rt-multi-thread", "sync" ] }
tokio = { version = "1.7", features = [ "rt", "rt-multi-thread", "sync", "fs" ] }
futures = "0.3"
uuid = { version = "0.8", features = [ "v4" ] }
thiserror = "1.0"
Expand Down
9 changes: 9 additions & 0 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ impl<P: Params> WindowManager<P> {
current_window_label = label.to_js_string()?,
));

webview_attributes.uri_scheme_protocols.insert(
"asset".into(),
Box::new(move |url| {
let path = url.replace("asset://", "");
let data = crate::async_runtime::block_on(async move { tokio::fs::read(path).await })?;
Ok(data)
}),
);

#[cfg(dev)]
{
webview_attributes = webview_attributes.initialization_script(&format!(
Expand Down

0 comments on commit ee60e42

Please sign in to comment.