-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.go
114 lines (81 loc) · 2.38 KB
/
driver.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
package vips
import (
iiifcache "github.com/go-iiif/go-iiif/v3/cache"
iiifconfig "github.com/go-iiif/go-iiif/v3/config"
iiifdriver "github.com/go-iiif/go-iiif/v3/driver"
iiifimage "github.com/go-iiif/go-iiif/v3/image"
iiifsource "github.com/go-iiif/go-iiif/v3/source"
"gopkg.in/h2non/bimg.v1"
_ "log"
)
func init() {
dr, err := NewVIPSDriver()
if err != nil {
panic(err)
}
iiifdriver.RegisterDriver("vips", dr)
}
type VIPSDriver struct {
iiifdriver.Driver
}
func NewVIPSDriver() (iiifdriver.Driver, error) {
dr := &VIPSDriver{}
return dr, nil
}
func (dr *VIPSDriver) NewImageFromConfigWithSource(config *iiifconfig.Config, src iiifsource.Source, id string) (iiifimage.Image, error) {
body, err := src.Read(id)
if err != nil {
return nil, err
}
bimg := bimg.NewImage(body)
im := VIPSImage{
config: config,
source: src,
source_id: id,
id: id,
bimg: bimg,
isgif: false,
}
/*
Hey look - see the 'isgif' flag? We're going to hijack the fact that
bimg doesn't handle GIF files and if someone requests them then we
will do the conversion after the final call to im.bimg.Process and
after we do handle any custom features. We are relying on the fact
that both bimg.NewImage and bimg.Image() expect and return raw bytes
and we are ignoring whatever bimg thinks in the Format() function.
So basically you should not try to any processing in bimg/libvips
after the -> GIF transformation. (20160922/thisisaaronland)
See also: https://github.com/h2non/bimg/issues/41
*/
return &im, nil
}
func (dr *VIPSDriver) NewImageFromConfigWithCache(config *iiifconfig.Config, cache iiifcache.Cache, id string) (iiifimage.Image, error) {
var image iiifimage.Image
body, err := cache.Get(id)
if err == nil {
source, err := iiifsource.NewMemorySource(body)
if err != nil {
return nil, err
}
image, err = dr.NewImageFromConfigWithSource(config, source, id)
if err != nil {
return nil, err
}
} else {
image, err = dr.NewImageFromConfig(config, id)
if err != nil {
return nil, err
}
go func() {
cache.Set(id, image.Body())
}()
}
return image, nil
}
func (dr *VIPSDriver) NewImageFromConfig(config *iiifconfig.Config, id string) (iiifimage.Image, error) {
source, err := iiifsource.NewSourceFromConfig(config)
if err != nil {
return nil, err
}
return dr.NewImageFromConfigWithSource(config, source, id)
}