diff --git a/kernel/api/network.go b/kernel/api/network.go index 5b5478fb4..d298a7227 100644 --- a/kernel/api/network.go +++ b/kernel/api/network.go @@ -24,6 +24,7 @@ import ( "io" "math" "net/http" + "net/textproto" "net/url" "strings" "time" @@ -31,19 +32,67 @@ import ( "github.com/88250/gulu" "github.com/gin-gonic/gin" "github.com/imroc/req/v3" + "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/util" ) +type File struct { + Filename string + Header textproto.MIMEHeader + Size int64 + Content string +} + +type MultipartForm struct { + Value map[string][]string + File map[string][]File +} + func echo(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) - password, passwordSet := c.Request.URL.User.Password() + var ( + password string + passwordSet bool + multipartForm *MultipartForm + rawData any + ) + + password, passwordSet = c.Request.URL.User.Password() + + if form, err := c.MultipartForm(); nil != err || nil == form { + multipartForm = nil + } else { + multipartForm = &MultipartForm{ + Value: form.Value, + File: map[string][]File{}, + } + for k, handlers := range form.File { + files := make([]File, len(handlers)) + multipartForm.File[k] = files + for i, handler := range handlers { + files[i].Filename = handler.Filename + files[i].Header = handler.Header + files[i].Size = handler.Size + if file, err := handler.Open(); nil != err { + logging.LogWarnf("echo open form [%s] file [%s] error: %s", k, handler.Filename, err.Error()) + } else { + content := make([]byte, handler.Size) + if _, err := file.Read(content); nil != err { + logging.LogWarnf("echo read form [%s] file [%s] error: %s", k, handler.Filename, err.Error()) + } else { + files[i].Content = base64.StdEncoding.EncodeToString(content) + } + } + } + } + } - var rawData any if data, err := c.GetRawData(); nil == err { rawData = base64.StdEncoding.EncodeToString(data) } else { + logging.LogWarnf("echo get raw data error: %s", err.Error()) rawData = nil } c.Request.ParseForm() @@ -73,7 +122,7 @@ func echo(c *gin.Context) { "Host": c.Request.Host, "Form": c.Request.Form, "PostForm": c.Request.PostForm, - "MultipartForm": c.Request.MultipartForm, + "MultipartForm": multipartForm, "Trailer": c.Request.Trailer, "RemoteAddr": c.Request.RemoteAddr, "TLS": c.Request.TLS,