Skip to content

Commit a262784

Browse files
authored
fix: reading file uploads map by ignoring actual indices in map (#1653)
1 parent e8d410b commit a262784

1 file changed

Lines changed: 22 additions & 35 deletions

File tree

router/core/parse_multipart.go

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"mime/multipart"
1111
"net/http"
1212
"os"
13-
"strconv"
1413
"strings"
1514

1615
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/datasource/httpclient"
@@ -61,17 +60,7 @@ func (p *MultipartParser) processInMemoryFile(filePart []*multipart.FileHeader,
6160
return err
6261
}
6362

64-
func (p *MultipartParser) processFilePart(filePart []*multipart.FileHeader, uploadsMap map[int]string) error {
65-
fileIndex := p.nextFileIndex()
66-
67-
variablePath, ok := uploadsMap[fileIndex]
68-
if !ok {
69-
return &httpGraphqlError{
70-
message: "no such file in the uploads map",
71-
statusCode: http.StatusOK,
72-
}
73-
}
74-
63+
func (p *MultipartParser) processFilePart(filePart []*multipart.FileHeader, uploadsPath string) error {
7564
file, err := filePart[0].Open()
7665
if err != nil {
7766
return err
@@ -88,10 +77,10 @@ func (p *MultipartParser) processFilePart(filePart []*multipart.FileHeader, uplo
8877
// Check if the file was written to the disk
8978
if diskFile, ok := file.(*os.File); ok {
9079
p.fileHandlers = append(p.fileHandlers, diskFile)
91-
p.files = append(p.files, httpclient.NewFileUpload(diskFile.Name(), filePart[0].Filename, variablePath))
80+
p.files = append(p.files, httpclient.NewFileUpload(diskFile.Name(), filePart[0].Filename, uploadsPath))
9281
} else {
9382
// The file is in memory. We write it manually to the disk.
94-
err = p.processInMemoryFile(filePart, file, variablePath)
83+
err = p.processInMemoryFile(filePart, file, uploadsPath)
9584
}
9685

9786
return err
@@ -132,26 +121,36 @@ func (p *MultipartParser) Parse(r *http.Request, buf *bytes.Buffer) ([]byte, []*
132121
return body, p.files, err
133122
}
134123

135-
var uploadsMap map[int]string
124+
var uploadsList []string
136125
rawUploadsMap, ok := p.form.Value["map"]
137126
if ok {
138-
uploadsMap, err = p.parseUploadMap(rawUploadsMap[0])
127+
uploadsList, err = p.parseUploadMap(rawUploadsMap[0])
139128
if err != nil {
140129
return body, p.files, err
141130
}
142131
}
143132

133+
if len(uploadsList) != len(p.form.File) {
134+
return body, p.files, &httpGraphqlError{
135+
message: "number of files does not match the number of entries in the upload map",
136+
statusCode: http.StatusOK,
137+
}
138+
}
139+
140+
fileIndex := 0
144141
for _, filePart := range p.form.File {
145-
err = p.processFilePart(filePart, uploadsMap)
142+
uploadPath := uploadsList[fileIndex]
143+
err = p.processFilePart(filePart, uploadPath)
146144
if err != nil {
147145
return body, p.files, err
148146
}
147+
fileIndex++
149148
}
150149

151150
return body, p.files, err
152151
}
153152

154-
func (p *MultipartParser) parseUploadMap(rawUploadsMap string) (map[int]string, error) {
153+
func (p *MultipartParser) parseUploadMap(rawUploadsMap string) ([]string, error) {
155154
var uploadsMap map[string][]string
156155
if err := json.Unmarshal([]byte(rawUploadsMap), &uploadsMap); err != nil {
157156
return nil, &httpGraphqlError{
@@ -160,36 +159,24 @@ func (p *MultipartParser) parseUploadMap(rawUploadsMap string) (map[int]string,
160159
}
161160
}
162161

163-
result := make(map[int]string)
164-
for index, variableName := range uploadsMap {
165-
fileIndex, err := strconv.Atoi(index)
166-
if err != nil {
167-
return nil, &httpGraphqlError{
168-
message: fmt.Sprintf("invalid upload index %s: %s", index, err.Error()),
169-
statusCode: http.StatusOK,
170-
}
171-
}
172-
162+
result := make([]string, 0, len(uploadsMap))
163+
for fileIndex, variableName := range uploadsMap {
173164
if len(variableName) == 0 {
174165
return nil, &httpGraphqlError{
175-
message: fmt.Sprintf("empty variable name for upload index %d", fileIndex),
166+
message: fmt.Sprintf("empty variable name for upload index %s", fileIndex),
176167
statusCode: http.StatusOK,
177168
}
178169
}
179170

180171
if len(variableName) > 1 {
181172
return nil, &httpGraphqlError{
182-
message: fmt.Sprintf("multiple variable names for upload index %d", fileIndex),
173+
message: fmt.Sprintf("multiple variable names for upload index %s", fileIndex),
183174
statusCode: http.StatusOK,
184175
}
185176
}
186177

187-
result[fileIndex] = variableName[0]
178+
result = append(result, variableName[0])
188179
}
189180

190181
return result, nil
191182
}
192-
193-
func (p *MultipartParser) nextFileIndex() int {
194-
return len(p.files)
195-
}

0 commit comments

Comments
 (0)