Skip to content

Commit

Permalink
Get mime fallback (#2340)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
derkan and René Werner authored Mar 24, 2023
1 parent 1f52799 commit 547db83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 16 additions & 6 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package utils

import (
"mime"
"strings"
)

Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions utils/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 547db83

Please sign in to comment.