From ee60e424221559d3d725716b0003c5566ef2b5cd Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Mon, 28 Jun 2021 21:56:20 +0800 Subject: [PATCH] feat: asset custom protocol to access local file system (#2104) --- .changes/asset-protocol.md | 5 +++++ core/tauri/Cargo.toml | 2 +- core/tauri/src/manager.rs | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .changes/asset-protocol.md diff --git a/.changes/asset-protocol.md b/.changes/asset-protocol.md new file mode 100644 index 000000000..64d40a875 --- /dev/null +++ b/.changes/asset-protocol.md @@ -0,0 +1,5 @@ +--- +"api": patch +--- + +Add asset custom protocol to access local file system. diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index ec032932a..ad65d9a2d 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -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" diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index 6d506788b..08a48eb9d 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -343,6 +343,15 @@ impl WindowManager

{ 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!(