/
handlerUpload.go
293 lines (257 loc) · 10.1 KB
/
handlerUpload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"encoding/json"
"io"
"net/http"
"path"
"path/filepath"
"strings"
"github.com/openmpp/go/ompp/helper"
"github.com/openmpp/go/ompp/omppLog"
)
// runUploadPostHandler post of model run zip archive in home/io/upload folder.
// POST /api/upload/model/:model/run
// POST /api/upload/model/:model/run/:run
// Zip archive is the same as created by dbcopy command line utilty.
// Dimension(s) and enum-based parameters returned as enum codes, not enum id's.
func runUploadPostHandler(w http.ResponseWriter, r *http.Request) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
rName := getRequestParam(r, "run") // run name
// find model metadata by digest or name
mb, ok := theCatalog.modelBasicByDigestOrName(dn)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
m, ok := theCatalog.ModelDicByDigest(mb.digest)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
// parse multipart form: only single part expected with run.zip file attached
mr, err := r.MultipartReader()
if err != nil {
http.Error(w, "Error at multipart form open ", http.StatusBadRequest)
return
}
// open next part
part, err := mr.NextPart()
if err == io.EOF {
http.Error(w, "Invalid (empty) next part of multipart form", http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, "Failed to get next part of multipart form: "+err.Error(), http.StatusBadRequest)
return
}
defer part.Close()
// check file name: it should be modelName.run.RunName.zip
// if run name not specified in URL the get it from file name
fName := part.FileName()
ext := path.Ext(fName)
baseName := strings.TrimSuffix(fName, ext)
mpn := m.Name + ".run."
runName := strings.TrimPrefix(baseName, mpn)
if baseName == "" || baseName == "." || baseName == ".." ||
runName == "" || runName == "." || runName == ".." ||
fName != helper.CleanPath(fName) {
http.Error(w, "Error: invalid (or empty) file name: "+fName, http.StatusBadRequest)
return
}
if ext != ".zip" || !strings.HasPrefix(baseName, mpn) {
http.Error(w, "Error: file name must be: "+mpn+"Name.zip", http.StatusBadRequest)
return
}
if rName != "" && runName != rName {
http.Error(w, "Error: invalid file name, expected: "+mpn+rName+".zip", http.StatusBadRequest)
return
}
// if upload.progress.log file exist the retun error: upload in progress
omppLog.Log("Upload of: ", fName)
logPath := filepath.Join(theCfg.uploadDir, baseName+".progress.upload.log")
if isFileExist(logPath) == nil {
omppLog.Log("Error: upload already in progress: ", logPath)
http.Error(w, "Model run upload already in progress: "+baseName, http.StatusBadRequest)
return
}
// create new upload.progress.log file and write model run decsription
logPath, isLog := createUpDownLog(logPath)
if !isLog {
omppLog.Log("Failed to create upload log file: " + baseName + ".progress.upload.log")
http.Error(w, "Model run upload failed: "+baseName, http.StatusBadRequest)
return
}
hdrMsg := []string{
"------------------",
"Upload : " + fName,
"Model Name : " + m.Name,
"Model Version : " + m.Version + " " + m.CreateDateTime,
"Model Digest : " + m.Digest,
"Run Name : " + runName,
"Folder : " + baseName,
"------------------",
}
if !appendToUpDownLog(logPath, true, "Upload of: "+baseName) {
renameToUploadErrorLog(logPath, "")
omppLog.Log("Failed to write into upload log file: " + baseName + ".progress.upload.log")
http.Error(w, "Model run upload failed: "+baseName, http.StatusBadRequest)
return
}
if !appendToUpDownLog(logPath, false, hdrMsg...) {
renameToUploadErrorLog(logPath, "")
omppLog.Log("Failed to write into upload log file: " + baseName + ".progress.upload.log")
http.Error(w, "Model run upload failed: "+baseName, http.StatusBadRequest)
return
}
// save run.zip into upload directory
saveToPath := filepath.Join(theCfg.uploadDir, fName)
helper.SaveTo(saveToPath, part)
if err != nil {
omppLog.Log("Error: unable to write into ", saveToPath, err)
http.Error(w, "Error: unable to write into "+fName, http.StatusInternalServerError)
return
}
// create model run upload files on separate thread
cmd, cmdMsg := makeRunUploadCommand(mb, runName, logPath)
go makeUpload(baseName, cmd, cmdMsg, logPath)
// report to the client results location
w.Header().Set("Content-Location", "/api/upload/model/"+dn+"/run/"+runName+"/"+baseName)
}
// worksetUploadPostHandler post of model workset zip archive in home/io/upload folder.
// POST /api/upload/model/:model/workset
// POST /api/upload/model/:model/workset/:set
// Zip archive is the same as created by dbcopy command line utilty.
// Dimension(s) and enum-based parameters returned as enum codes, not enum id's.
// Posted multi-part form can have optional "workset-upload-options" part with json upload options
// Upload option NoDigestCheck=true do suppress model digest verification:
// model digest in source zip is ignored, only model name is used and that allows to upload worksets into different model version.
func worksetUploadPostHandler(w http.ResponseWriter, r *http.Request) {
// url or query parameters
dn := getRequestParam(r, "model") // model digest-or-name
wsn := getRequestParam(r, "set") // workset name
// find model metadata by digest or name
mb, ok := theCatalog.modelBasicByDigestOrName(dn)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
m, ok := theCatalog.ModelDicByDigest(mb.digest)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
// parse multipart form: only single part expected with set.zip file attached
mr, err := r.MultipartReader()
if err != nil {
http.Error(w, "Error at multipart form open ", http.StatusBadRequest)
return
}
// open first part: it can be workset-upload-options part or workset.zip file
part, err := mr.NextPart()
if err == io.EOF {
http.Error(w, "Invalid (empty) next part of multipart form", http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, "Failed to get next part of multipart form: "+err.Error(), http.StatusBadRequest)
return
}
// if this is workset-upload-options then decode json
isNoDigestCheck := false
if part.FormName() == "workset-upload-options" {
opts := struct{ NoDigestCheck bool }{}
err = json.NewDecoder(part).Decode(&opts)
if err != nil && err != io.EOF {
http.Error(w, "Json decode error at 'workset-upload-options' part of multipart form", http.StatusBadRequest)
part.Close()
return
}
isNoDigestCheck = opts.NoDigestCheck
// open next part: workset.zip file
part.Close()
part, err = mr.NextPart()
if err == io.EOF {
http.Error(w, "Invalid (empty) next part of multipart form", http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, "Failed to get next part of multipart form: "+err.Error(), http.StatusBadRequest)
return
}
}
defer part.Close()
// check file name: it should be modelName.set.WorksetName.zip
// if workset name not specified in URL the get it from file name
fName := part.FileName()
ext := path.Ext(fName)
baseName := strings.TrimSuffix(fName, ext)
mpn := m.Name + ".set."
setName := strings.TrimPrefix(baseName, mpn)
if baseName == "" || baseName == "." || baseName == ".." ||
setName == "" || setName == "." || setName == ".." ||
fName != helper.CleanPath(fName) {
http.Error(w, "Error: invalid (or empty) file name: "+fName, http.StatusBadRequest)
return
}
if ext != ".zip" || !strings.HasPrefix(baseName, mpn) {
http.Error(w, "Error: file name must be: "+mpn+"Name.zip", http.StatusBadRequest)
return
}
if wsn != "" && setName != wsn {
http.Error(w, "Error: invalid file name, expected: "+mpn+wsn+".zip", http.StatusBadRequest)
return
}
// if upload.progress.log file exist the retun error: upload in progress
omppLog.Log("Upload of: ", fName)
logPath := filepath.Join(theCfg.uploadDir, baseName+".progress.upload.log")
if isFileExist(logPath) == nil {
omppLog.Log("Error: upload already in progress: ", logPath)
http.Error(w, "Model scenario upload already in progress: "+baseName, http.StatusBadRequest)
return
}
// create new upload.progress.log file and write model scenario decsription
logPath, isLog := createUpDownLog(logPath)
if !isLog {
omppLog.Log("Failed to create upload log file: " + baseName + ".progress.upload.log")
http.Error(w, "Model scenario upload failed: "+baseName, http.StatusBadRequest)
return
}
hdrMsg := []string{
"------------------",
"Upload : " + fName,
"Model Name : " + m.Name,
"Model Version : " + m.Version + " " + m.CreateDateTime,
"Model Digest : " + m.Digest,
"Scenario Name : " + setName,
"Folder : " + baseName,
"------------------",
}
if !appendToUpDownLog(logPath, true, "Upload of: "+baseName) {
renameToUploadErrorLog(logPath, "")
omppLog.Log("Failed to write into upload log file: " + baseName + ".progress.upload.log")
http.Error(w, "Model scenario upload failed: "+baseName, http.StatusBadRequest)
return
}
if !appendToUpDownLog(logPath, false, hdrMsg...) {
renameToUploadErrorLog(logPath, "")
omppLog.Log("Failed to write into upload log file: " + baseName + ".progress.upload.log")
http.Error(w, "Model scenario upload failed: "+baseName, http.StatusBadRequest)
return
}
// save set.zip into upload directory
saveToPath := filepath.Join(theCfg.uploadDir, fName)
helper.SaveTo(saveToPath, part)
if err != nil {
omppLog.Log("Error: unable to write into ", saveToPath, err)
http.Error(w, "Error: unable to write into "+fName, http.StatusInternalServerError)
return
}
// create model scenario upload files on separate thread
cmd, cmdMsg := makeWorksetUploadCommand(mb, setName, logPath, isNoDigestCheck)
go makeUpload(baseName, cmd, cmdMsg, logPath)
// report to the client results location
w.Header().Set("Content-Location", "/api/upload/model/"+dn+"/workset/"+setName+"/"+baseName)
}