Skip to content

Commit 547db83

Browse files
derkanRené Werner
andauthored
Get mime fallback (#2340)
* added fallback to go's mime detection * added test for getting mime * added err check * added err check * removing import alias for builtin mime and aserting error for adding mime type. * removing import alias for builtin mime and aserting error for adding mime type. * added fallback to go's mime detection * added test for getting mime * added err check * added err check * removing import alias for builtin mime and aserting error for adding mime type. * removing import alias for builtin mime and aserting error for adding mime type. --------- Co-authored-by: René Werner <[email protected]>
1 parent 1f52799 commit 547db83

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

utils/http.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package utils
66

77
import (
8+
"mime"
89
"strings"
910
)
1011

@@ -15,16 +16,25 @@ func GetMIME(extension string) string {
1516
if len(extension) == 0 {
1617
return ""
1718
}
18-
var mime string
19+
var foundMime string
1920
if extension[0] == '.' {
20-
mime = mimeExtensions[extension[1:]]
21+
foundMime = mimeExtensions[extension[1:]]
2122
} else {
22-
mime = mimeExtensions[extension]
23+
foundMime = mimeExtensions[extension]
2324
}
24-
if len(mime) == 0 {
25-
return MIMEOctetStream
25+
26+
if len(foundMime) == 0 {
27+
if extension[0] != '.' {
28+
foundMime = mime.TypeByExtension("." + extension)
29+
} else {
30+
foundMime = mime.TypeByExtension(extension)
31+
}
32+
33+
if foundMime == "" {
34+
return MIMEOctetStream
35+
}
2636
}
27-
return mime
37+
return foundMime
2838
}
2939

3040
// ParseVendorSpecificContentType check if content type is vendor specific and

utils/http_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func Test_GetMIME(t *testing.T) {
2323

2424
res = GetMIME("unknown")
2525
AssertEqual(t, MIMEOctetStream, res)
26+
27+
err := mime.AddExtensionType(".mjs", "application/javascript")
28+
if err == nil {
29+
res = GetMIME(".mjs")
30+
AssertEqual(t, "application/javascript", res)
31+
}
32+
AssertEqual(t, nil, err)
33+
2634
// empty case
2735
res = GetMIME("")
2836
AssertEqual(t, "", res)

0 commit comments

Comments
 (0)