diff --git a/core/cmd/server/docs/x-log.json b/core/cmd/server/docs/x-log.json index ff9bba3a7..54fbb5573 100644 --- a/core/cmd/server/docs/x-log.json +++ b/core/cmd/server/docs/x-log.json @@ -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": [ diff --git a/core/middleware/operation.go b/core/middleware/operation.go index f9c3ed9d5..177dfc564 100644 --- a/core/middleware/operation.go +++ b/core/middleware/operation.go @@ -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 +}