-
Notifications
You must be signed in to change notification settings - Fork 6
/
image_handler.go
473 lines (402 loc) · 11.9 KB
/
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package main
import (
"bytes"
"encoding/json"
"fmt"
"iiif"
"io"
"io/ioutil"
"math"
"mime"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)
// AllFeatures is the complete list of everything supported by RAIS at this time
var AllFeatures = &iiif.FeatureSet{
RegionByPx: true,
RegionByPct: true,
SizeByWhListed: true,
SizeByW: true,
SizeByH: true,
SizeByPct: true,
SizeByWh: true,
SizeByForcedWh: true,
SizeAboveFull: true,
RotationBy90s: true,
RotationArbitrary: false,
Mirroring: true,
Default: true,
Color: true,
Gray: true,
Bitonal: true,
Jpg: true,
Png: true,
Gif: true,
Tif: true,
Jp2: false,
Pdf: false,
Webp: false,
BaseURIRedirect: true,
Cors: true,
JsonldMediaType: true,
ProfileLinkHeader: false,
CanonicalLinkHeader: false,
}
func acceptsLD(req *http.Request) bool {
for _, h := range req.Header["Accept"] {
for _, accept := range strings.Split(h, ",") {
if accept == "application/ld+json" {
return true
}
}
}
return false
}
// DZITileSize defines deep zoom tile size
const DZITileSize = 1024
// DZI "constants" - these can be declared once (unlike IIIF regexes) because
// we aren't allowing a custom DZI handler path
var (
DZIInfoRegex = regexp.MustCompile(`^/images/dzi/(.+).dzi$`)
DZITilePathRegex = regexp.MustCompile(`^/images/dzi/(.+)_files/(\d+)/(\d+)_(\d+).jpg$`)
)
// ImageHandler responds to an IIIF URL request and parses the requested
// transformation within the limits of the handler's capabilities
type ImageHandler struct {
IIIFBase *url.URL
IIIFBaseRegex *regexp.Regexp
IIIFBaseOnlyRegex *regexp.Regexp
IIIFInfoPathRegex *regexp.Regexp
FeatureSet *iiif.FeatureSet
TilePath string
}
// NewImageHandler sets up a base ImageHandler with no features
func NewImageHandler(tp string) *ImageHandler {
return &ImageHandler{TilePath: tp}
}
// EnableIIIF sets up regexes for IIIF responses
func (ih *ImageHandler) EnableIIIF(u *url.URL) {
rprefix := fmt.Sprintf(`^%s`, u.Path)
ih.IIIFBase = u
ih.IIIFBaseRegex = regexp.MustCompile(rprefix + `/([^/]+)`)
ih.IIIFBaseOnlyRegex = regexp.MustCompile(rprefix + `/[^/]+$`)
ih.IIIFInfoPathRegex = regexp.MustCompile(rprefix + `/([^/]+)/info.json$`)
ih.FeatureSet = AllFeatures
}
// IIIFRoute takes an HTTP request and parses it to see what (if any) IIIF
// translation is requested
func (ih *ImageHandler) IIIFRoute(w http.ResponseWriter, req *http.Request) {
// Pull identifier from base so we know if we're even dealing with a valid
// file in the first place
var url = *req.URL
url.RawQuery = ""
p := url.String()
parts := ih.IIIFBaseRegex.FindStringSubmatch(p)
// If it didn't even match the base, something weird happened, so we just
// spit out a generic 404
if parts == nil {
http.NotFound(w, req)
return
}
id := iiif.ID(parts[1])
fp := ih.TilePath + "/" + id.Path()
// Check for base path and redirect if that's all we have
if ih.IIIFBaseOnlyRegex.MatchString(p) {
http.Redirect(w, req, p+"/info.json", 303)
return
}
// Handle info.json prior to reading the image, in case of cached info
if ih.IIIFInfoPathRegex.MatchString(p) {
ih.Info(w, req, id, fp)
return
}
// No info path should mean a full command path - start reading the image
res, err := NewImageResource(id, fp)
if err != nil {
e := newImageResError(err)
http.Error(w, e.Message, e.Code)
return
}
u := iiif.NewURL(p)
if !u.Valid() {
// This means the URI was probably a command, but had an invalid syntax
http.Error(w, "Invalid IIIF request", 400)
return
}
// Attempt to run the command
ih.Command(w, req, u, res)
}
func convertStrings(s1, s2, s3 string) (i1, i2, i3 int, err error) {
i1, err = strconv.Atoi(s1)
if err != nil {
return
}
i2, err = strconv.Atoi(s2)
if err != nil {
return
}
i3, err = strconv.Atoi(s3)
return
}
// DZIRoute takes an HTTP request and parses for responding to DZI info and
// tile requests
func (ih *ImageHandler) DZIRoute(w http.ResponseWriter, req *http.Request) {
p := req.RequestURI
var id iiif.ID
var filePath string
var handler func(*ImageResource)
parts := DZIInfoRegex.FindStringSubmatch(p)
if parts != nil {
id = iiif.ID(parts[1])
filePath = ih.TilePath + "/" + id.Path()
handler = func(r *ImageResource) { ih.DZIInfo(w, r) }
}
parts = DZITilePathRegex.FindStringSubmatch(p)
if parts != nil {
id = iiif.ID(parts[1])
filePath = ih.TilePath + "/" + id.Path()
level, tileX, tileY, err := convertStrings(parts[2], parts[3], parts[4])
if err != nil {
http.Error(w, "Invalid DZI request", 400)
return
}
handler = func(r *ImageResource) { ih.DZITile(w, req, r, level, tileX, tileY) }
}
if handler == nil {
// Some kind of invalid request - just spit out a generic 404
http.NotFound(w, req)
return
}
res, err := NewImageResource(id, filePath)
if err != nil {
e := newImageResError(err)
http.Error(w, e.Message, e.Code)
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
handler(res)
}
// DZIInfo returns the info response for a deep-zoom request. This is very
// hard-coded because the XML is simple, and deep-zoom is really a minor
// addition to RAIS.
func (ih *ImageHandler) DZIInfo(w http.ResponseWriter, res *ImageResource) {
format := `<?xml version="1.0" encoding="UTF-8"?>
<Image xmlns="http://schemas.microsoft.com/deepzoom/2008" TileSize="%d" Overlap="0" Format="jpg">
<Size Width="%d" Height="%d"/>
</Image>`
d := res.Decoder
xml := fmt.Sprintf(format, DZITileSize, d.GetWidth(), d.GetHeight())
w.Write([]byte(xml))
}
// DZITile returns a tile by setting up the appropriate IIIF data structure
// based on the deep-zoom request
func (ih *ImageHandler) DZITile(w http.ResponseWriter, req *http.Request, res *ImageResource, l, tX, tY int) {
// We always return 256x256 or bigger
if l < 8 {
l = 8
}
// Figure out max level
d := res.Decoder
srcWidth := uint64(d.GetWidth())
srcHeight := uint64(d.GetHeight())
var maxDimension uint64
if srcWidth > srcHeight {
maxDimension = srcWidth
} else {
maxDimension = srcHeight
}
maxLevel := uint64(math.Ceil(math.Log2(float64(maxDimension))))
// Ignore absurd levels - even above 20 is pretty unlikely, but this is
// called "future-proofing". Or something.
if uint64(l) > maxLevel {
http.Error(w, fmt.Sprintf("Image doesn't support zoom level %d", l), 400)
return
}
// Construct an IIIF URL so we can just reuse the IIIF-centric Command function
var reduction uint64 = 1 << (maxLevel - uint64(l))
width := reduction * DZITileSize
height := reduction * DZITileSize
left := uint64(tX) * width
top := uint64(tY) * height
// Fail early if the tile requested isn't legit
if tX < 0 || tY < 0 || left > srcWidth || top > srcHeight {
http.Error(w, "Invalid tile request", 400)
return
}
// If any dimension is outside the image area, we have to adjust the requested image and tilesize
finalWidth := width
finalHeight := height
if left+width > srcWidth {
finalWidth = srcWidth - left
}
if top+height > srcHeight {
finalHeight = srcHeight - top
}
requestedWidth := DZITileSize * finalWidth / width
u := iiif.NewURL(fmt.Sprintf("%s/%d,%d,%d,%d/%d,/0/default.jpg",
res.FilePath, left, top, finalWidth, finalHeight, requestedWidth))
ih.Command(w, req, u, res)
}
// Info responds to a IIIF info request with appropriate JSON based on the
// image's data and the handler's capabilities
func (ih *ImageHandler) Info(w http.ResponseWriter, req *http.Request, id iiif.ID, fp string) {
// Check for cached image data first, and use that to create JSON
json, e := ih.loadInfoJSONFromCache(id)
if e != nil {
http.Error(w, e.Message, e.Code)
return
}
// Next, check for an overridden info.json file, and just spit that out
// directly if it exists
if json == nil {
json = ih.loadInfoJSONOverride(id, fp)
}
if json == nil {
json, e = ih.loadInfoJSONFromImageResource(id, fp)
if e != nil {
http.Error(w, e.Message, e.Code)
return
}
}
// Set headers - content type is dependent on client
ct := "application/json"
if acceptsLD(req) {
ct = "application/ld+json"
}
w.Header().Set("Content-Type", ct)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(json)
}
func newImageResError(err error) *HandlerError {
switch err {
case ErrImageDoesNotExist:
return NewError("image resource does not exist", 404)
default:
return NewError(err.Error(), 500)
}
}
func (ih *ImageHandler) loadInfoJSONFromCache(id iiif.ID) ([]byte, *HandlerError) {
if infoCache == nil {
return nil, nil
}
data, ok := infoCache.Get(id)
if !ok {
return nil, nil
}
return ih.buildInfoJSON(id, data.(ImageInfo))
}
func (ih *ImageHandler) loadInfoJSONOverride(id iiif.ID, fp string) []byte {
// If an override file isn't found or has an error, just skip it
json, err := ioutil.ReadFile(fp + "-info.json")
if err != nil {
return nil
}
// If an override file *is* found, replace the id
fullid := ih.IIIFBase.String() + "/" + id.String()
return bytes.Replace(json, []byte("%ID%"), []byte(fullid), 1)
}
func (ih *ImageHandler) loadInfoJSONFromImageResource(id iiif.ID, fp string) ([]byte, *HandlerError) {
Logger.Debugf("Loading image data from image resource (id: %s)", id)
res, err := NewImageResource(id, fp)
if err != nil {
return nil, newImageResError(err)
}
d := res.Decoder
imageInfo := ImageInfo{
Width: d.GetWidth(),
Height: d.GetHeight(),
TileWidth: d.GetTileWidth(),
TileHeight: d.GetTileHeight(),
Levels: d.GetLevels(),
}
if infoCache != nil {
infoCache.Add(id, imageInfo)
}
return ih.buildInfoJSON(id, imageInfo)
}
func (ih *ImageHandler) buildInfoJSON(id iiif.ID, i ImageInfo) ([]byte, *HandlerError) {
info := ih.FeatureSet.Info()
info.Width = i.Width
info.Height = i.Height
// Set up tile sizes
if i.TileWidth > 0 {
var sf []int
scale := 1
for x := 0; x < i.Levels; x++ {
// For sanity's sake, let's not tell viewers they can get at absurdly
// small sizes
if info.Width/scale < 16 {
break
}
if info.Height/scale < 16 {
break
}
sf = append(sf, scale)
scale <<= 1
}
info.Tiles = make([]iiif.TileSize, 1)
info.Tiles[0] = iiif.TileSize{
Width: i.TileWidth,
ScaleFactors: sf,
}
if i.TileHeight > 0 {
info.Tiles[0].Height = i.TileHeight
}
}
// The info id is actually the full URL to the resource, not just its ID
info.ID = ih.IIIFBase.String() + "/" + id.String()
json, err := json.Marshal(info)
if err != nil {
Logger.Errorf("Unable to marshal IIIFInfo response: %s", err)
return nil, NewError("server error", 500)
}
return json, nil
}
// Command handles image processing operations
func (ih *ImageHandler) Command(w http.ResponseWriter, req *http.Request, u *iiif.URL, res *ImageResource) {
// For now the cache is very limited to ensure only relatively small requests
// are actually cached
willCache := tileCache != nil && u.Format == iiif.FmtJPG && u.Size.W > 0 && u.Size.W <= 1024 && u.Size.H == 0
cacheKey := u.Path
// Send last modified time
if err := sendHeaders(w, req, res.FilePath); err != nil {
return
}
// Do we support this request? If not, return a 501
if !ih.FeatureSet.Supported(u) {
http.Error(w, "Feature not supported", 501)
return
}
// Check the cache now that we're sure everything is valid and supported.
if willCache {
data, ok := tileCache.Get(cacheKey)
if ok {
w.Header().Set("Content-Type", mime.TypeByExtension("."+string(u.Format)))
w.Write(data.([]byte))
return
}
}
img, err := res.Apply(u)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", mime.TypeByExtension("."+string(u.Format)))
var cacheBuf *bytes.Buffer
var out io.Writer = w
if willCache {
cacheBuf = bytes.NewBuffer(nil)
out = io.MultiWriter(w, cacheBuf)
}
if err := EncodeImage(out, img, u.Format); err != nil {
http.Error(w, "Unable to encode", 500)
Logger.Errorf("Unable to encode to %s: %s", u.Format, err)
return
}
if willCache {
tileCache.Add(cacheKey, cacheBuf.Bytes())
}
}