Browse Source

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

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Reupen Shah 2 years ago
parent
commit
85e77fb797

+ 5 - 0
.changes/txt-mime-type.md

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

+ 1 - 0
.github/workflows/test-core.yml

@@ -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 }}

+ 3 - 3
core/tauri-codegen/src/context.rs

@@ -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();

+ 2 - 2
core/tauri-macros/src/command_module.rs

@@ -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! {

+ 11 - 5
core/tauri-utils/src/mime_type.rs

@@ -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"));

+ 4 - 5
core/tauri/src/event.rs

@@ -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();