forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
48 lines (38 loc) · 979 Bytes
/
web.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
package upload
import (
"net/http"
httputil "github.com/goodrain/rainbond/util/http"
)
type Storage struct {
output string
verbosity int
}
func (s *Storage) StorageDir() string {
return s.output
}
func NewStorage(rootDir string) *Storage {
return &Storage{output: rootDir}
}
// UploadHandler is the endpoint for uploading and storing files.
func (s *Storage) UploadHandler(w http.ResponseWriter, r *http.Request) {
// Performs the processing of writing data into chunk files.
files, err := process(r, s.StorageDir())
if err == incomplete {
httputil.ReturnSuccess(r, w, nil)
return
}
if err != nil {
httputil.ReturnError(r, w, 500, err.Error())
return
}
data := make([]map[string]interface{}, 0)
for _, file := range files {
attachment, err := create(s.StorageDir(), file, true)
if err != nil {
httputil.ReturnError(r, w, 500, err.Error())
return
}
data = append(data, attachment.ToJson())
}
httputil.ReturnSuccess(r, w, data)
}