mirror of https://github.com/gofiber/fiber.git
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 <rene.werner@verivox.com>pull/2367/head^2
parent
1f52799686
commit
547db83cdd
|
@ -5,6 +5,7 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -15,16 +16,25 @@ func GetMIME(extension string) string {
|
|||
if len(extension) == 0 {
|
||||
return ""
|
||||
}
|
||||
var mime string
|
||||
var foundMime string
|
||||
if extension[0] == '.' {
|
||||
mime = mimeExtensions[extension[1:]]
|
||||
foundMime = mimeExtensions[extension[1:]]
|
||||
} else {
|
||||
mime = mimeExtensions[extension]
|
||||
foundMime = mimeExtensions[extension]
|
||||
}
|
||||
if len(mime) == 0 {
|
||||
return MIMEOctetStream
|
||||
|
||||
if len(foundMime) == 0 {
|
||||
if extension[0] != '.' {
|
||||
foundMime = mime.TypeByExtension("." + extension)
|
||||
} else {
|
||||
foundMime = mime.TypeByExtension(extension)
|
||||
}
|
||||
|
||||
if foundMime == "" {
|
||||
return MIMEOctetStream
|
||||
}
|
||||
}
|
||||
return mime
|
||||
return foundMime
|
||||
}
|
||||
|
||||
// ParseVendorSpecificContentType check if content type is vendor specific and
|
||||
|
|
|
@ -23,6 +23,14 @@ func Test_GetMIME(t *testing.T) {
|
|||
|
||||
res = GetMIME("unknown")
|
||||
AssertEqual(t, MIMEOctetStream, res)
|
||||
|
||||
err := mime.AddExtensionType(".mjs", "application/javascript")
|
||||
if err == nil {
|
||||
res = GetMIME(".mjs")
|
||||
AssertEqual(t, "application/javascript", res)
|
||||
}
|
||||
AssertEqual(t, nil, err)
|
||||
|
||||
// empty case
|
||||
res = GetMIME("")
|
||||
AssertEqual(t, "", res)
|
||||
|
|
Loading…
Reference in New Issue