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 != ""
}
func mimeTypeFromUrl(url string) string {
ext := path.Ext(url)
func mimeTypeFromUrl(urlString string) string {
parsedUrl, err := url.Parse(urlString)
if err != nil {
return ""
}
ext := path.Ext(parsedUrl.Path)
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
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.jpeg", want: "image/jpeg"},
{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 {
t.Run(strconv.Itoa(i), func(t *testing.T) {