fix: improve multipart form data handling by detecting content type. fix #3007

This commit is contained in:
CaIon
2026-02-25 12:51:46 +08:00
parent c5365e4b43
commit 4a4cf0a0df

View File

@@ -6,6 +6,7 @@ import (
"io" "io"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/textproto"
"strconv" "strconv"
"strings" "strings"
@@ -186,7 +187,22 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayIn
if err != nil { if err != nil {
continue continue
} }
part, err := writer.CreateFormFile(fieldName, fh.Filename) ct := fh.Header.Get("Content-Type")
if ct == "" || ct == "application/octet-stream" {
buf512 := make([]byte, 512)
n, _ := io.ReadFull(f, buf512)
ct = http.DetectContentType(buf512[:n])
// Re-open after sniffing so the full content is copied below
f.Close()
f, err = fh.Open()
if err != nil {
continue
}
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldName, fh.Filename))
h.Set("Content-Type", ct)
part, err := writer.CreatePart(h)
if err != nil { if err != nil {
f.Close() f.Close()
continue continue