mirror of https://github.com/jlelse/GoBlog
Utils: Handle edge case and just use path to get mime type
This commit is contained in:
parent
a60a3f92bd
commit
e5d0d136c0
8
utils.go
8
utils.go
|
@ -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 {
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue