forked from wanelo/image-server
-
Notifications
You must be signed in to change notification settings - Fork 7
/
new_image_handler.go
63 lines (52 loc) · 1.61 KB
/
new_image_handler.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
package server
import (
"net/http"
"strings"
"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/image-server/image-server/core"
"github.com/image-server/image-server/info"
"github.com/image-server/image-server/logger"
"github.com/image-server/image-server/request"
"github.com/image-server/image-server/uploader"
"github.com/unrolled/render"
)
// NewImageHandler handles posting new images
func NewImageHandler(w http.ResponseWriter, req *http.Request, sc *core.ServerConfiguration) {
go logger.ImagePosted()
IncrCounter(&status.Posting.Current)
defer DecrCounter(&status.Posting.Current)
IncrCounter(&status.Posting.TotalCount)
qs := req.URL.Query()
vars := mux.Vars(req)
sourceURL := qs.Get("source")
namespace := vars["namespace"]
outputs := []string{}
if qs.Get("outputs") != "" {
outputs = strings.Split(qs.Get("outputs"), ",")
}
request := &request.Request{
ServerConfiguration: sc,
Namespace: namespace,
Outputs: outputs,
Uploader: uploader.DefaultUploader(sc),
Paths: sc.Adapters.Paths,
SourceURL: sourceURL,
SourceData: req.Body,
}
imageDetails, err := request.Create()
if err != nil {
go logger.ImagePostingFailed()
IncrCounter(&status.Posting.FailedCount)
glog.Error("Failed to create image from ", sourceURL, " - ", err)
errorHandlerJSON(err, w, http.StatusNotFound)
return
}
renderImageDetails(w, imageDetails)
}
func renderImageDetails(w http.ResponseWriter, imageDetails *info.ImageDetails) {
r := render.New(render.Options{
IndentJSON: true,
})
r.JSON(w, http.StatusOK, imageDetails)
}