fix: Fixed logging errors for uploaded files (#9204)

This commit is contained in:
Swt242 2025-06-25 11:46:27 +08:00 committed by GitHub
parent 2edddaf487
commit 725786d6aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 4 deletions

View file

@ -1672,12 +1672,13 @@
},
"/files/upload": {
"bodyKeys": [
"path"
"path",
"file"
],
"paramKeys": [],
"beforeFunctions": [],
"formatZH": "上传文件[path]",
"formatEN": "Uploadfile[path]"
"formatZH": "上传文件[path]/[file]",
"formatEN": "Uploadfile[path]/[file]"
},
"/files/wget": {
"bodyKeys": [

View file

@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/url"
"path"
@ -65,7 +67,11 @@ func OperationLog() gin.HandlerFunc {
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
}
bodyMap := make(map[string]interface{})
_ = json.Unmarshal(body, &bodyMap)
if strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") {
bodyMap, _ = parseMultipart(body, c.Request.Header.Get("Content-Type"))
} else {
_ = json.Unmarshal(body, &bodyMap)
}
for _, key := range operationDic.BodyKeys {
if _, ok := bodyMap[key]; ok {
formatMap[key] = bodyMap[key]
@ -247,3 +253,33 @@ func replaceStr(val string, rep ...string) string {
}
return val
}
func parseMultipart(formData []byte, contentType string) (map[string]interface{}, error) {
d, params, err := mime.ParseMediaType(contentType)
if err != nil || d != "multipart/form-data" {
return nil, http.ErrNotMultipart
}
boundary, ok := params["boundary"]
if !ok {
return nil, http.ErrMissingBoundary
}
reader := multipart.NewReader(bytes.NewReader(formData), boundary)
ret := make(map[string]interface{})
f, err := reader.ReadForm(32 << 20)
if err != nil {
return nil, err
}
for k, v := range f.Value {
if len(v) > 0 {
ret[k] = v[0]
}
}
for k, v := range f.File {
if len(v) > 0 {
ret[k] = v[0].Filename
}
}
return ret, nil
}