MCPcopy Create free account
hub / github.com/MG-RAST/Shock / ParseMultipartForm

Function ParseMultipartForm

shock-server/request/request.go:101–203  ·  view source on GitHub ↗

helper function for create & update

(r *http.Request)

Source from the content-addressed store, hash-verified

99
100// helper function for create & update
101func ParseMultipartForm(r *http.Request) (params map[string]string, files file.FormFiles, err error) {
102 params = make(map[string]string)
103 files = make(file.FormFiles)
104 reader, err := r.MultipartReader()
105 if err != nil {
106 return
107 }
108
109 tmpPath := ""
110 for {
111 if part, err := reader.NextPart(); err == nil {
112 // params don't have a FileName()
113 // files must have FormName() of either "upload", "gzip", "bzip2", "attributes", "subset_indices", or an integer
114 if part.FileName() == "" {
115 if !util.IsValidParamName(part.FormName()) {
116 return nil, files, errors.New("invalid param: " + part.FormName())
117 }
118 buffer := make([]byte, 32*1024)
119 n, err := part.Read(buffer)
120 if n == 0 || err != nil {
121 break
122 }
123 formValue := fmt.Sprintf("%s", buffer[0:n])
124 if part.FormName() == "upload_url" {
125 tmpPath = fmt.Sprintf("%s/temp/%d%d", conf.PATH_DATA, rand.Int(), rand.Int())
126 files[part.FormName()] = file.FormFile{Name: "", Path: tmpPath, Checksum: make(map[string]string)}
127 // download from url
128 if tmpFile, err := os.Create(tmpPath); err == nil {
129 defer tmpFile.Close()
130 var tmpform = files[part.FormName()]
131 md5h := md5.New()
132 dst := io.MultiWriter(tmpFile, md5h)
133 fileName, body, err := fetchFileStream(formValue)
134 if err != nil {
135 return nil, files, errors.New("unable to stream url: " + err.Error())
136 }
137 defer body.Close()
138 if _, err = io.Copy(dst, body); err != nil {
139 return nil, files, err
140 }
141 tmpform.Name = fileName
142 tmpform.Checksum["md5"] = fmt.Sprintf("%x", md5h.Sum(nil))
143 files[part.FormName()] = tmpform
144 } else {
145 return nil, files, err
146 }
147 } else {
148 // regular form field
149 params[part.FormName()] = formValue
150 }
151 } else {
152 // determine file type
153 isSubsetFile := false
154 if part.FormName() == "subset_indices" {
155 isSubsetFile = true
156 }
157 isPartsFile := false
158 if _, er := strconv.Atoi(part.FormName()); er == nil {

Callers

nothing calls this directly

Calls 5

fetchFileStreamFunction · 0.85
ReadMethod · 0.65
CreateMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected