fix: optimize base64url parser (#2376)
This commit is contained in:
@ -23,6 +23,7 @@ import (
|
|||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileData struct {
|
type FileData struct {
|
||||||
@ -49,12 +50,36 @@ func URLToBase64(url string) (*FileData, error) {
|
|||||||
|
|
||||||
var mimeType string
|
var mimeType string
|
||||||
|
|
||||||
mimeType = resp.Header.Get("Content-Type")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
if contentType != "" {
|
||||||
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||||
|
if err == nil && mediaType != "" {
|
||||||
|
mimeType = mediaType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if mimeType == "" {
|
if mimeType == "" {
|
||||||
ext := filepath.Ext(url)
|
detectedType := http.DetectContentType(fileContent)
|
||||||
|
if detectedType != "application/octet-stream" {
|
||||||
|
mimeType = detectedType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if mimeType == "" || mimeType == "application/octet-stream" {
|
||||||
|
urlPath := url
|
||||||
|
if idx := strings.Index(urlPath, "?"); idx != -1 {
|
||||||
|
urlPath = urlPath[:idx]
|
||||||
|
}
|
||||||
|
if idx := strings.Index(urlPath, "#"); idx != -1 {
|
||||||
|
urlPath = urlPath[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
ext := filepath.Ext(urlPath)
|
||||||
if ext != "" {
|
if ext != "" {
|
||||||
mimeType = mime.TypeByExtension(ext)
|
extMimeType := mime.TypeByExtension(ext)
|
||||||
|
if extMimeType != "" {
|
||||||
|
mimeType = extMimeType
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user