mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-08 22:46:51 +08:00
fix: Fixed logging errors for uploaded files (#9204)
This commit is contained in:
parent
2edddaf487
commit
725786d6aa
2 changed files with 41 additions and 4 deletions
|
@ -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": [
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue