浏览代码

fix: protocol works on in both cargo and bundle

This commit enables assets to be served locally through cargo run
or through a bundle strategy. We use cargo-bundle's bundle strategy.
This PR checks only for macOS targets.
Jonathan Kelley 3 年之前
父节点
当前提交
c12bcd8150
共有 1 个文件被更改,包括 11 次插入15 次删除
  1. 11 15
      packages/desktop/src/protocol.rs

+ 11 - 15
packages/desktop/src/protocol.rs

@@ -21,30 +21,25 @@ pub(super) fn desktop_handler(request: &Request) -> Result<Response> {
             .mimetype("text/javascript")
             .body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec())
     } else {
-        // the path of the asset specified without any relative paths
-        let path_buf = Path::new(trimmed).canonicalize()?;
+        let asset_root = get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf());
+        let asset = asset_root.join(trimmed).canonicalize()?;
 
-        // the current path of the bundle
-        let cur_path = get_asset_root()
-            .unwrap_or_else(|| Path::new(".").to_path_buf())
-            .canonicalize()?;
-
-        if !path_buf.starts_with(cur_path) {
+        if !asset.starts_with(asset_root) {
             return ResponseBuilder::new()
                 .status(StatusCode::FORBIDDEN)
                 .body(String::from("Forbidden").into_bytes());
         }
 
-        if !path_buf.exists() {
+        if !asset.exists() {
             return ResponseBuilder::new()
                 .status(StatusCode::NOT_FOUND)
                 .body(String::from("Not Found").into_bytes());
         }
 
-        let mime = mime_guess::from_path(&path_buf).first_or_octet_stream();
+        let mime = mime_guess::from_path(&asset).first_or_octet_stream();
 
         // do not let path searching to go two layers beyond the caller level
-        let data = std::fs::read(path_buf)?;
+        let data = std::fs::read(asset)?;
         let meta = format!("{}", mime);
 
         ResponseBuilder::new().mimetype(&meta).body(data)
@@ -73,10 +68,11 @@ fn get_asset_root() -> Option<PathBuf> {
     #[cfg(target_os = "macos")]
     {
         let bundle = core_foundation::bundle::CFBundle::main_bundle();
-        let bundle_path = bundle.path()?;
-        let resources_path = bundle.resources_path()?;
-        let absolute_resources_root = bundle_path.join(resources_path);
-        let canonical_resources_root = dunce::canonicalize(absolute_resources_root).ok()?;
+        let bundle_path = dbg!(bundle.path()?);
+        let resources_path = dbg!(bundle.resources_path()?);
+        let absolute_resources_root = dbg!(bundle_path.join(resources_path));
+        let canonical_resources_root = dbg!(dunce::canonicalize(absolute_resources_root).ok()?);
+
         return Some(canonical_resources_root);
     }