-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhandler_sandbox.go
88 lines (69 loc) · 1.88 KB
/
handler_sandbox.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
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"github.com/tokubai/kinu/logger"
"github.com/tokubai/kinu/resource"
)
const SANDBOX_IMAGE_TYPE = "__sandbox__"
func UploadImageToSandboxHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
r.ParseMultipartForm(0)
uid, err := uuid.NewV4()
if err != nil {
RespondInternalServerError(w, err)
return
}
imageId := uid.String()
file, _, err := r.FormFile("image")
if err != nil {
RespondBadRequest(w, "invalid file")
return
}
err = resource.New(SANDBOX_IMAGE_TYPE, imageId).Store(file)
if err != nil {
if _, ok := err.(*ErrInvalidRequest); ok {
RespondBadRequest(w, err.Error())
} else {
RespondInternalServerError(w, err)
}
}
RespondImageUploadSuccessJson(w, SANDBOX_IMAGE_TYPE, imageId)
logger.WithFields(logrus.Fields{
"path": r.URL.Path,
"params": r.URL.Query(),
"method": r.Method,
}).Info("success")
}
func ApplyFromSandboxHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
r.ParseMultipartForm(0)
sandboxId := r.FormValue("sandbox_id")
if len(sandboxId) == 0 {
RespondBadRequest(w, "required sandbox_id")
return
}
imageType := r.FormValue("name")
if len(imageType) == 0 {
RespondBadRequest(w, "required name parameter")
return
}
imageId := r.FormValue("id")
if len(imageId) == 0 {
RespondBadRequest(w, "required id parameter")
return
}
err := resource.New(SANDBOX_IMAGE_TYPE, sandboxId).MoveTo(imageType, imageId)
if err != nil {
RespondInternalServerError(w, err)
return
}
RespondImageUploadSuccessJson(w, imageType, imageId)
logger.WithFields(logrus.Fields{
"path": r.URL.Path,
"params": r.URL.Query(),
"method": r.Method,
}).Info("success")
}