fix: correct MIME type of .txt files (ref: #6762) (#7111)

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Reupen Shah
2023-06-05 13:16:05 +01:00
committed by GitHub
parent 3327dd641d
commit 85e77fb797
6 changed files with 26 additions and 15 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri-utils": patch
---
Correctly determine MIME type of `.txt` files.

View File

@@ -93,6 +93,7 @@ jobs:
cargo update -p time --precise 0.3.15
cargo update -p ignore --precise 0.4.18
cargo update -p raw-window-handle --precise 0.5.0
cargo update -p cargo_toml:0.15.3 --precise 0.15.2
- name: test
run: cargo test --target ${{ matrix.platform.target }} ${{ matrix.features.args }}

View File

@@ -176,10 +176,10 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
.tauri
.security
.dev_csp
.clone()
.or_else(|| config.tauri.security.csp.clone())
.as_ref()
.or(config.tauri.security.csp.as_ref())
} else {
config.tauri.security.csp.clone()
config.tauri.security.csp.as_ref()
};
if csp.is_some() {
options = options.with_csp();

View File

@@ -272,12 +272,12 @@ pub fn command_handler(attributes: HandlerAttributes, function: ItemFn) -> Token
pub fn command_test(attributes: HandlerTestAttributes, function: ItemFn) -> TokenStream2 {
let allowlist = attributes.allowlist;
let error_message = attributes.error_message.as_str();
let signature = function.sig.clone();
let signature = &function.sig;
let enum_variant_name = function.sig.ident.to_string().to_lower_camel_case();
let response = match attributes.allowlist_check_kind {
AllowlistCheckKind::Runtime => {
let test_name = function.sig.ident.clone();
let test_name = &signature.ident;
quote!(super::Cmd::#test_name(crate::test::mock_invoke_context()))
}
AllowlistCheckKind::Serde => quote! {

View File

@@ -18,10 +18,11 @@ pub enum MimeType {
Js,
Json,
Jsonld,
Mp4,
OctetStream,
Rtf,
Svg,
Mp4,
Txt,
}
impl std::fmt::Display for MimeType {
@@ -34,10 +35,11 @@ impl std::fmt::Display for MimeType {
MimeType::Js => "text/javascript",
MimeType::Json => "application/json",
MimeType::Jsonld => "application/ld+json",
MimeType::Mp4 => "video/mp4",
MimeType::OctetStream => "application/octet-stream",
MimeType::Rtf => "application/rtf",
MimeType::Svg => "image/svg+xml",
MimeType::Mp4 => "video/mp4",
MimeType::Txt => MIMETYPE_PLAIN,
};
write!(f, "{mime}")
}
@@ -62,9 +64,10 @@ impl MimeType {
Some("json") => Self::Json,
Some("jsonld") => Self::Jsonld,
Some("mjs") => Self::Js,
Some("mp4") => Self::Mp4,
Some("rtf") => Self::Rtf,
Some("svg") => Self::Svg,
Some("mp4") => Self::Mp4,
Some("txt") => Self::Txt,
// Assume HTML when a TLD is found for eg. `wry:://tauri.app` | `wry://hello.com`
Some(_) => fallback,
// using octet stream according to this:
@@ -133,14 +136,17 @@ mod tests {
let mjs: String = MimeType::parse_from_uri("https://example.com/bundled.mjs").to_string();
assert_eq!(mjs, String::from("text/javascript"));
let mp4: String = MimeType::parse_from_uri("https://example.com/video.mp4").to_string();
assert_eq!(mp4, String::from("video/mp4"));
let rtf: String = MimeType::parse_from_uri("https://example.com/document.rtf").to_string();
assert_eq!(rtf, String::from("application/rtf"));
let svg: String = MimeType::parse_from_uri("https://example.com/picture.svg").to_string();
assert_eq!(svg, String::from("image/svg+xml"));
let mp4: String = MimeType::parse_from_uri("https://example.com/video.mp4").to_string();
assert_eq!(mp4, String::from("video/mp4"));
let txt: String = MimeType::parse_from_uri("https://example.com/file.txt").to_string();
assert_eq!(txt, String::from("text/plain"));
let custom_scheme = MimeType::parse_from_uri("wry://tauri.app").to_string();
assert_eq!(custom_scheme, String::from("text/html"));

View File

@@ -282,14 +282,13 @@ mod test {
// check to see if on_event properly grabs the stored function from listen.
#[test]
fn check_on_event(e in "[a-z]+", d in "[a-z]+") {
fn check_on_event(key in "[a-z]+", d in "[a-z]+") {
let listeners: Listeners = Default::default();
// clone e as the key
let key = e.clone();
// call listen with e and the event_fn dummy func
listeners.listen(e.clone(), None, event_fn);
listeners.listen(key.clone(), None, event_fn);
// call on event with e and d.
listeners.trigger(&e, None, Some(d));
listeners.trigger(&key, None, Some(d));
// lock the mutex
let l = listeners.inner.handlers.lock().unwrap();