Skip to content

Commit 8f14687

Browse files
committed
fix: improve multipart form data handling by detecting content type. fix #3007
1 parent 094ac54 commit 8f14687

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

relay/channel/task/sora/adaptor.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"mime/multipart"
88
"net/http"
9+
"net/textproto"
910
"strconv"
1011
"strings"
1112

@@ -186,7 +187,22 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayIn
186187
if err != nil {
187188
continue
188189
}
189-
part, err := writer.CreateFormFile(fieldName, fh.Filename)
190+
ct := fh.Header.Get("Content-Type")
191+
if ct == "" || ct == "application/octet-stream" {
192+
buf512 := make([]byte, 512)
193+
n, _ := io.ReadFull(f, buf512)
194+
ct = http.DetectContentType(buf512[:n])
195+
// Re-open after sniffing so the full content is copied below
196+
f.Close()
197+
f, err = fh.Open()
198+
if err != nil {
199+
continue
200+
}
201+
}
202+
h := make(textproto.MIMEHeader)
203+
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldName, fh.Filename))
204+
h.Set("Content-Type", ct)
205+
part, err := writer.CreatePart(h)
190206
if err != nil {
191207
f.Close()
192208
continue

0 commit comments

Comments
 (0)