feat: use infer instead of outdated mime-guess, bump wry (#548)

* feat: use infer instead of outdated mime

* chore: remove unused file

* fix: remove unused file
This commit is contained in:
Jon Kelley 2022-09-24 23:48:05 -07:00 committed by GitHub
parent bf9901a6cd
commit 0c48cb3601
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 9 deletions

View file

@ -21,7 +21,7 @@ serde = "1.0.136"
serde_json = "1.0.79"
thiserror = "1.0.30"
log = "0.4.14"
wry = { version = "0.19.0" }
wry = { version = "0.20.2" }
futures-channel = "0.3.21"
tokio = { version = "1.16.1", features = [
"sync",
@ -30,7 +30,7 @@ tokio = { version = "1.16.1", features = [
"time",
], optional = true, default-features = false }
webbrowser = "0.7.1"
mime_guess = "2.0.3"
infer = "0.9.0"
dunce = "1.0.2"
interprocess = { version = "1.1.1", optional = true }

View file

@ -68,13 +68,9 @@ pub(super) fn desktop_handler(
.body(String::from("Not Found").into_bytes());
}
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(asset)?;
let meta = format!("{}", mime);
ResponseBuilder::new().mimetype(&meta).body(data)
ResponseBuilder::new()
.mimetype(get_mime_from_path(trimmed)?)
.body(std::fs::read(asset)?)
}
}
@ -110,3 +106,42 @@ fn get_asset_root() -> Option<PathBuf> {
None
}
/// Get the mime type from a path-like string
fn get_mime_from_path(trimmed: &str) -> Result<&str> {
if trimmed.ends_with(".svg") {
return Ok("image/svg+xml");
}
let res = match infer::get_from_path(&trimmed)?.map(|f| f.mime_type()) {
Some(t) if t == "text/plain" => get_mime_by_ext(trimmed),
Some(f) => f,
None => get_mime_by_ext(trimmed),
};
Ok(res)
}
/// Get the mime type from a URI using its extension
fn get_mime_by_ext(trimmed: &str) -> &str {
let suffix = trimmed.split('.').last();
match suffix {
Some("bin") => "application/octet-stream",
Some("css") => "text/css",
Some("csv") => "text/csv",
Some("html") => "text/html",
Some("ico") => "image/vnd.microsoft.icon",
Some("js") => "text/javascript",
Some("json") => "application/json",
Some("jsonld") => "application/ld+json",
Some("mjs") => "text/javascript",
Some("rtf") => "application/rtf",
Some("svg") => "image/svg+xml",
Some("mp4") => "video/mp4",
// Assume HTML when a TLD is found for eg. `dioxus:://dioxuslabs.app` | `dioxus://hello.com`
Some(_) => "text/html",
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// using octet stream according to this:
None => "application/octet-stream",
}
}