Browse Source

feat: canoncialize assets for macOS

Jonathan Kelley 3 năm trước cách đây
mục cha
commit
24743e44e9
2 tập tin đã thay đổi với 44 bổ sung2 xóa
  1. 5 0
      packages/desktop/Cargo.toml
  2. 39 2
      packages/desktop/src/protocol.rs

+ 5 - 0
packages/desktop/Cargo.toml

@@ -29,6 +29,11 @@ dioxus-html = { path = "../html", features = ["serialize"], version = "^0.1.6" }
 webbrowser = "0.5.5"
 mime_guess = "2.0.3"
 dioxus-interpreter-js = { path = "../interpreter", version = "^0.0.0" }
+dunce = "1.0.2"
+
+
+[target.'cfg(target_os = "macos")'.dependencies]
+core-foundation = "0.9.3"
 
 
 [features]

+ 39 - 2
packages/desktop/src/protocol.rs

@@ -1,4 +1,4 @@
-use std::path::Path;
+use std::path::{Path, PathBuf};
 use wry::{
     http::{status::StatusCode, Request, Response, ResponseBuilder},
     Result,
@@ -21,8 +21,13 @@ 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 cur_path = Path::new(".").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) {
             return ResponseBuilder::new()
@@ -45,3 +50,35 @@ pub(super) fn desktop_handler(request: &Request) -> Result<Response> {
         ResponseBuilder::new().mimetype(&meta).body(data)
     }
 }
+
+#[allow(unreachable_code)]
+fn get_asset_root() -> Option<PathBuf> {
+    /*
+    We're matching exactly how cargo-bundle works.
+
+    - [x] macOS
+    - [ ] Windows
+    - [ ] Linux (rpm)
+    - [ ] Linux (deb)
+    - [ ] iOS
+    - [ ] Android
+
+    */
+
+    if std::env::var_os("CARGO").is_some() {
+        return None;
+    }
+
+    // TODO: support for other platforms
+    #[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()?;
+        return Some(canonical_resources_root);
+    }
+
+    None
+}