From 7315f8e694f74d8c3f12a7f1b2ee6bfbb6cd89f7 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 2 Mar 2024 04:54:29 -0500 Subject: [PATCH] fix: need to manually extract the multipart filename because goes internals strips file paths --- api/s5/s5.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/api/s5/s5.go b/api/s5/s5.go index 3e3aedd..7ee2ef2 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -8,12 +8,15 @@ import ( _ "embed" "encoding/base64" "encoding/hex" + "encoding/json" "errors" "fmt" "io" "math" + "mime" "mime/multipart" "net/http" + "net/textproto" "net/url" "slices" "strconv" @@ -1232,6 +1235,11 @@ func (s *S5API) processMultipartFiles(r *http.Request) (map[string]*metadata.Upl for _, files := range r.MultipartForm.File { for _, fileHeader := range files { + filename := extractMPFilename(fileHeader.Header) + if filename == "" { + return nil, NewS5Error(ErrKeyInvalidOperation, fmt.Errorf("filename not found in multipart file header")) + } + file, err := fileHeader.Open() if err != nil { return nil, NewS5Error(ErrKeyStorageOperationFailed, err) @@ -1256,7 +1264,7 @@ func (s *S5API) processMultipartFiles(r *http.Request) (map[string]*metadata.Upl return nil, NewS5Error(ErrKeyStorageOperationFailed, err) } - uploadMap[fileHeader.Filename] = upload + uploadMap[filename] = upload } } @@ -2038,3 +2046,22 @@ func setAuthCookie(jwt string, jc jape.Context) { http.SetCookie(jc.ResponseWriter, &authCookie) } + +func extractMPFilename(header textproto.MIMEHeader) string { + cd := header.Get("Content-Disposition") + if cd == "" { + return "" + } + + _, params, err := mime.ParseMediaType(cd) + if err != nil { + return "" + } + + filename := params["filename"] + if filename == "" { + return "" + } + + return filename +}