From a572fb3dc756b3b2e3339f9f504fde68ddb6a943 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 8 Feb 2020 12:30:54 -0300 Subject: [PATCH] =?UTF-8?q?feat(tauri&tauri.js)=20allow=20inliner/loadAsse?= =?UTF-8?q?t=20to=20be=20used=20with=20pub=E2=80=A6=20(#354)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(tauri&tauri.js) allow inliner/loadAsset to be used with publicPath * fix minor error Co-authored-by: Tensor-Programming --- tauri/src/endpoints.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/tauri/src/endpoints.rs b/tauri/src/endpoints.rs index 43c76e2dd..f04131ee4 100644 --- a/tauri/src/endpoints.rs +++ b/tauri/src/endpoints.rs @@ -1,6 +1,8 @@ mod cmd; use web_view::WebView; +#[cfg(not(any(feature = "dev-server", feature = "embedded-server")))] +use std::path::{PathBuf}; #[allow(unused_variables)] pub(crate) fn handle(webview: &mut WebView<'_, T>, arg: &str) -> crate::Result { @@ -201,14 +203,33 @@ fn load_asset( crate::execute_promise( webview, move || { - let read_asset = crate::assets::ASSETS.get(&format!( - "{}{}{}", - env!("TAURI_DIST_DIR"), - if asset.starts_with("/") { "" } else { "/" }, - asset - )); - if read_asset.is_err() { - return Err(format!("Asset '{}' not found", asset).into()); + let mut path = PathBuf::from( + if asset.starts_with("/") { + asset.replacen("/", "", 1) + } else { + asset.clone() + } + ); + let mut read_asset; + loop { + read_asset = crate::assets::ASSETS.get(&format!( + "{}/{}", + env!("TAURI_DIST_DIR"), + path.to_string_lossy() + )); + if read_asset.is_err() { + match path.iter().next() { + Some(component) => { + let first_component = component.to_str().expect("failed to read path component"); + path = PathBuf::from(path.to_string_lossy().replacen(format!("{}/", first_component).as_str(), "", 1)); + } + None => { + return Err(format!("Asset '{}' not found", asset).into()); + } + } + } else { + break; + } } if asset_type == "image" {