From 24743e44e92def5384b2a8e1f9b5bcac7bfc00ca Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 23 Feb 2022 13:53:45 -0500 Subject: [PATCH] feat: canoncialize assets for macOS --- packages/desktop/Cargo.toml | 5 ++++ packages/desktop/src/protocol.rs | 41 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index 6fd277ba4..f90d00000 100644 --- a/packages/desktop/Cargo.toml +++ b/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] diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index c8f007c1d..6cac600f2 100644 --- a/packages/desktop/src/protocol.rs +++ b/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 { .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 { ResponseBuilder::new().mimetype(&meta).body(data) } } + +#[allow(unreachable_code)] +fn get_asset_root() -> Option { + /* + 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 +}