forked from wanelo/image-server
/
resize_handler.go
59 lines (48 loc) · 1.65 KB
/
resize_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
package server
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/image-server/image-server/core"
"github.com/image-server/image-server/logger"
"github.com/image-server/image-server/parser"
"github.com/image-server/image-server/request"
"github.com/image-server/image-server/uploader"
)
// ResizeHandler asumes the original image is either stores locally or on the remote server
// it returns the processed image in given dimension and format.
// When an image is requested more than once, only one will do the processing,
// and both requests will return the same output
func ResizeHandler(w http.ResponseWriter, req *http.Request, sc *core.ServerConfiguration) {
defer logger.RequestLatency("resize_image", time.Now())
vars := mux.Vars(req)
filename := vars["filename"]
ic, err := parser.NameToConfiguration(sc, filename)
if err != nil {
errorHandler(err, w, req, http.StatusNotFound)
return
}
ic.ID = varsToHash(vars)
ic.Namespace = vars["namespace"]
qs := req.URL.Query()
ir := request.Request{
ServerConfiguration: sc,
Namespace: vars["namespace"],
Outputs: strings.Split(qs.Get("outputs"), ","),
Uploader: uploader.DefaultUploader(sc),
Paths: sc.Adapters.Paths,
Hash: ic.ID,
}
err = ir.Process(ic)
if err != nil {
errorHandlerJSON(err, w, http.StatusNotFound)
return
}
localResizedPath := sc.Adapters.Paths.LocalImagePath(ic.Namespace, ic.ID, ic.Filename)
http.ServeFile(w, req, localResizedPath)
}
func varsToHash(vars map[string]string) string {
return fmt.Sprintf("%s%s%s%s", vars["id1"], vars["id2"], vars["id3"], vars["id4"])
}