-
Notifications
You must be signed in to change notification settings - Fork 6
/
legacy_handlers.go
115 lines (98 loc) · 3.1 KB
/
legacy_handlers.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
package main
import (
"image"
"image/jpeg"
"net/http"
"regexp"
"strconv"
)
var tilePathRegex = regexp.MustCompile(`^/images/tiles/(?P<path>.+)/image_(?P<width>\d+)x(?P<height>\d+)_from_(?P<x1>\d+),(?P<y1>\d+)_to_(?P<x2>\d+),(?P<y2>\d+).jpg`)
var resizePathRegex = regexp.MustCompile(`^/images/resize/(.+)/(\d+)x(\d+)`)
var infoPathRegex = regexp.MustCompile(`^/images/info/(.+)$`)
// TileHandler is responsible for all chronam-like tile requests
func TileHandler(w http.ResponseWriter, req *http.Request) {
// Extract request path's regex parts into local variables
parts := tilePathRegex.FindStringSubmatch(req.URL.Path)
if parts == nil {
http.Error(w, "Invalid tile request", 400)
return
}
d := map[string]string{}
for i, name := range tilePathRegex.SubexpNames() {
d[name] = parts[i]
}
path := d["path"]
x1, _ := strconv.Atoi(d["x1"])
y1, _ := strconv.Atoi(d["y1"])
x2, _ := strconv.Atoi(d["x2"])
y2, _ := strconv.Atoi(d["y2"])
r := image.Rect(x1, y1, x2, y2)
width, _ := strconv.Atoi(d["width"])
height, _ := strconv.Atoi(d["height"])
filepath := tilePath + "/" + path
if err := sendHeaders(w, req, filepath); err != nil {
return
}
res, err := NewImageResource("", filepath)
if err != nil {
http.Error(w, "Unable to read source image", 500)
Logger.Errorf("Unable to read source image: %s", err)
return
}
i := res.Decoder
// Pull raw tile data
i.SetResizeWH(width, height)
i.SetCrop(r)
img, err := i.DecodeImage()
if err != nil {
http.Error(w, "Unable to decode image", 500)
Logger.Errorf("Unable to decode image: %s", err)
return
}
// Encode as JPEG straight to the client
w.Header().Set("Content-Type", "image/jpeg")
if err = jpeg.Encode(w, img, &jpeg.Options{Quality: 80}); err != nil {
http.Error(w, "Unable to encode tile", 500)
Logger.Errorf("Unable to encode tile into JPEG: %s", err)
return
}
}
// ResizeHandler is responsible for all chronam-like resize requests
func ResizeHandler(w http.ResponseWriter, req *http.Request) {
// Extract request path's regex parts into local variables
parts := resizePathRegex.FindStringSubmatch(req.URL.Path)
if parts == nil {
http.Error(w, "Invalid resize request", 400)
return
}
path := parts[1]
width, _ := strconv.Atoi(parts[2])
height, _ := strconv.Atoi(parts[3])
// Get file's last modified time, returning a 404 if we can't stat the file
filepath := tilePath + "/" + path
if err := sendHeaders(w, req, filepath); err != nil {
return
}
res, err := NewImageResource("", filepath)
if err != nil {
http.Error(w, "Unable to read source image", 500)
Logger.Errorf("Unable to read source image: %s", err)
return
}
i := res.Decoder
// Pull raw tile data
i.SetResizeWH(width, height)
img, err := i.DecodeImage()
if err != nil {
http.Error(w, "Unable to decode image", 500)
Logger.Errorf("Unable to decode image: %s", err)
return
}
// Encode as JPEG straight to the client
w.Header().Set("Content-Type", "image/jpeg")
if err = jpeg.Encode(w, img, &jpeg.Options{Quality: 80}); err != nil {
http.Error(w, "Unable to encode tile", 500)
Logger.Errorf("Unable to encode tile into JPEG: %s", err)
return
}
}