commands/http: Parse multipart files into req.Files()
Matt Bell committed
Nov 16, 2014 at 00:47 UTC
53735263c28e66eb72a87b1e883fe459fca71a08
1 file changed
+17
-10
commands/http/parse.go
+17
-10
@@ -2,6 +2,7 @@ package http
2
3
import (
4
"errors"
5
+ "mime"
6
"net/http"
7
"strings"
8
@@ -49,10 +50,6 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
50
51
// count the number of provided argument values
52
valCount := len(stringArgs)
52
- // TODO: add total number of parts in request body (instead of just 1 if body is present)
53
- if r.Body != nil && r.ContentLength != 0 {
54
- valCount += 1
55
- }
53
54
args := make([]interface{}, valCount)
55
@@ -81,11 +78,6 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
78
} else {
79
break
80
}
84
-
85
- } else {
86
- // TODO: create multipart streams for file args
87
- args[valIndex] = r.Body
88
- valIndex++
81
}
82
}
83
@@ -94,7 +86,22 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
86
return nil, err
87
}
88
97
- req, err := cmds.NewRequest(path, opts, args, cmd, optDefs)
89
+ // create cmds.File from multipart/form-data contents
90
+ contentType := r.Header.Get(contentTypeHeader)
91
+ mediatype, _, err := mime.ParseMediaType(contentType)
92
+ if err != nil {
93
+ return nil, err
94
+ }
95
+ var f *cmds.MultipartFile
96
+ if mediatype == "multipart/form-data" {
97
+ f = &cmds.MultipartFile{Mediatype: mediatype}
98
+ f.Reader, err = r.MultipartReader()
99
+ if err != nil {
100
+ return nil, err
101
+ }
102
+ }
103
+
104
+ req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs)
105
if err != nil {
106
return nil, err
107
}