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
Erkan Durmuş 2023-03-24 16:29:42 +03:00 committed by GitHub
parent 1f52799686
commit 547db83cdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View File

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

View File

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