feat: asset custom protocol to access local file system (#2104)

This commit is contained in:
Ngo Iok Ui (Wu Yu Wei) 2021-06-28 21:56:20 +08:00 committed by GitHub
parent 628a53eb61
commit ee60e42422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"api": patch
---
Add asset custom protocol to access local file system.

View File

@ -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"

View File

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