Utils: Handle edge case and just use path to get mime type

This commit is contained in:
Jan-Lukas Else 2022-11-21 16:34:06 +01:00
parent a60a3f92bd
commit e5d0d136c0
2 changed files with 8 additions and 2 deletions

View File

@ -405,8 +405,12 @@ func loStringNotEmpty(s string, _ int) bool {
return s != "" return s != ""
} }
func mimeTypeFromUrl(url string) string { func mimeTypeFromUrl(urlString string) string {
ext := path.Ext(url) parsedUrl, err := url.Parse(urlString)
if err != nil {
return ""
}
ext := path.Ext(parsedUrl.Path)
mimeType := mime.TypeByExtension(ext) mimeType := mime.TypeByExtension(ext)
if mimeType == "" { if mimeType == "" {
switch ext { switch ext {

View File

@ -178,6 +178,8 @@ func Test_mimeTypeFromUrl(t *testing.T) {
{url: "https://example.com/profile.jpg", want: "image/jpeg"}, {url: "https://example.com/profile.jpg", want: "image/jpeg"},
{url: "https://example.com/profile.jpeg", want: "image/jpeg"}, {url: "https://example.com/profile.jpeg", want: "image/jpeg"},
{url: "https://example.com/profile.png", want: "image/png"}, {url: "https://example.com/profile.png", want: "image/png"},
{url: "https://example.com/profile.png?v=3", want: "image/png"},
{url: "/profile.png?v=3", want: "image/png"},
} }
for i, tt := range tests { for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) { t.Run(strconv.Itoa(i), func(t *testing.T) {