From e4d26800192fb842eb6f989ad77137e27f0a0d2f Mon Sep 17 00:00:00 2001 From: junwen-lee Date: Thu, 23 Oct 2025 15:01:25 +0800 Subject: [PATCH] fix: optimize base64url parser (#2376) --- backend/pkg/urltobase64url/parser.go | 31 +++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/backend/pkg/urltobase64url/parser.go b/backend/pkg/urltobase64url/parser.go index 40d30b7bf..eebf65f86 100644 --- a/backend/pkg/urltobase64url/parser.go +++ b/backend/pkg/urltobase64url/parser.go @@ -23,6 +23,7 @@ import ( "mime" "net/http" "path/filepath" + "strings" ) type FileData struct { @@ -49,12 +50,36 @@ func URLToBase64(url string) (*FileData, error) { 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 == "" { - 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 != "" { - mimeType = mime.TypeByExtension(ext) + extMimeType := mime.TypeByExtension(ext) + if extMimeType != "" { + mimeType = extMimeType + } } }