@@ -46,6 +46,11 @@ impl std::fmt::Display for MimeType {
4646impl MimeType {
4747 /// parse a URI suffix to convert text/plain mimeType to their actual web compatible mimeType.
4848 pub fn parse_from_uri ( uri : & str ) -> MimeType {
49+ Self :: parse_from_uri_with_fallback ( uri, Self :: Html )
50+ }
51+
52+ /// parse a URI suffix to convert text/plain mimeType to their actual web compatible mimeType with specified fallback for unknown file extensions.
53+ pub fn parse_from_uri_with_fallback ( uri : & str , fallback : MimeType ) -> MimeType {
4954 let suffix = uri. split ( '.' ) . last ( ) ;
5055 match suffix {
5156 Some ( "bin" ) => Self :: OctetStream ,
@@ -61,15 +66,19 @@ impl MimeType {
6166 Some ( "svg" ) => Self :: Svg ,
6267 Some ( "mp4" ) => Self :: Mp4 ,
6368 // Assume HTML when a TLD is found for eg. `wry:://tauri.app` | `wry://hello.com`
64- Some ( _) => Self :: Html ,
65- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
69+ Some ( _) => fallback,
6670 // using octet stream according to this:
71+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
6772 None => Self :: OctetStream ,
6873 }
6974 }
7075
7176 /// infer mimetype from content (or) URI if needed.
7277 pub fn parse ( content : & [ u8 ] , uri : & str ) -> String {
78+ Self :: parse_with_fallback ( content, uri, Self :: Html )
79+ }
80+ /// infer mimetype from content (or) URI if needed with specified fallback for unknown file extensions.
81+ pub fn parse_with_fallback ( content : & [ u8 ] , uri : & str , fallback : MimeType ) -> String {
7382 let mime = if uri. ends_with ( ".svg" ) {
7483 // when reading svg, we can't use `infer`
7584 None
@@ -78,8 +87,10 @@ impl MimeType {
7887 } ;
7988
8089 match mime {
81- Some ( mime) if mime == MIMETYPE_PLAIN => Self :: parse_from_uri ( uri) . to_string ( ) ,
82- None => Self :: parse_from_uri ( uri) . to_string ( ) ,
90+ Some ( mime) if mime == MIMETYPE_PLAIN => {
91+ Self :: parse_from_uri_with_fallback ( uri, fallback) . to_string ( )
92+ }
93+ None => Self :: parse_from_uri_with_fallback ( uri, fallback) . to_string ( ) ,
8394 Some ( mime) => mime. to_string ( ) ,
8495 }
8596 }
0 commit comments