-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
313 lines (262 loc) · 8.23 KB
/
context.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
package kimg
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
)
// KimgRequest define a image request.
type KimgRequest struct {
Md5 string `json:"-"`
Origin bool `json:"-"`
Style string `json:"-"`
Save bool `json:"-"`
// scale params
Scale bool `json:"scale,omitempty"`
ScaleM string `json:"scale_m,omitempty"`
ScaleW int `json:"scale_w,omitempty"`
ScaleH int `json:"scale_h,omitempty"`
ScaleWP int `json:"scale_wp,omitempty"`
ScaleHP int `json:"scale_hp,omitempty"`
ScaleP int `json:"scale_p,omitempty"`
// crop params
Crop bool `json:"crop,omitempty"`
Gravity string `json:"gravity,omitempty"`
CropW int `json:"crop_w,omitempty"`
CropH int `json:"crop_h,omitempty"`
Offset string `json:"offset,omitempty"`
OffsetX int `json:"offset_x,omitempty"`
OffsetY int `json:"offset_y,omitempty"`
Format string `json:"format,omitempty"`
Quality int `json:"quality,omitempty"`
Rotate int `json:"rotate,omitempty"`
BGColor string `json:"bg_color,omitempty"`
Gray bool `json:"gray,omitempty"`
AutoOrient bool `json:"auto_orient,omitempty"`
Strip bool `json:"strip,omitempty"`
}
// KimgResponse define a image response.
type KimgResponse struct {
Md5 string `json:"md5"`
URL string `json:"url"`
Style string `json:"style,omitempty"`
Size int `json:"size"`
Width int `json:"width"`
Height int `json:"height"`
Format string `json:"format"`
Orientation string `json:"orientation"`
Exif map[string]string `json:"exif"`
}
// KimgContext context of kimg.
type KimgContext struct {
Config *KimgConfig
Cache KimgCache
Logger KimgLogger
Storage KimgStorage
Image *KimgImagick
}
// Key generate a key according to image style request params.
func (req *KimgRequest) Key() string {
if req.Origin {
return req.Md5
}
if len(req.Style) > 0 {
return req.Style
}
b, _ := json.Marshal(req)
m := md5.New()
m.Write(b)
return hex.EncodeToString(m.Sum(nil))
}
// NewKimgContext create a instance of kimg context.
func NewKimgContext(configFile string) (*KimgContext, error) {
var ctx KimgContext
config, err := NewKimgConfig(configFile)
if err != nil {
return nil, err
}
ctx.Config = config
logger, err := NewKimgLogger(config)
if err != nil {
return nil, err
}
ctx.Logger = logger
cache, err := NewKimgCache(config)
if err != nil {
return nil, err
}
ctx.Cache = cache
storage, err := NewKimgStorage(&ctx)
if err != nil {
return nil, err
}
ctx.Storage = storage
ctx.Image = NewKimgImagick(&ctx)
logger.Info("%+v", config)
return &ctx, nil
}
// Release release resource in kimg context.
func (ctx *KimgContext) Release() {
ctx.Image.Release()
}
// SaveImage save a image to kimg and make a kimg response.
func (ctx *KimgContext) SaveImage(data []byte) (*KimgResponse, error) {
m := md5.New()
m.Write(data)
md5Sum := hex.EncodeToString(m.Sum(nil))
ctx.Logger.Debug("SaveImage md5Sum: %s", md5Sum)
req := ctx.originRequest(md5Sum)
err := ctx.Storage.Set(req, data)
if err != nil {
return nil, err
}
if ctx.isCacheEnable(data) {
cacheKey := ctx.cacheKey(req)
if err = ctx.Cache.Set(cacheKey, data); err != nil {
ctx.Logger.Warn("SaveImage md5Sum: %s, SetCache %s err: %s", md5Sum, cacheKey, err)
} else {
ctx.Logger.Debug("SaveImage md5Sum: %s, SetCache %s", md5Sum, cacheKey)
}
}
resp, err := ctx.Image.Info(req, data)
if err != nil {
ctx.Logger.Warn("SaveImage md5Sum: %s, Image.Info err: %s", md5Sum, err)
return nil, err
}
return resp, nil
}
// GetImage get a image data from kimg according to a image request.
func (ctx *KimgContext) GetImage(req *KimgRequest) ([]byte, error) {
ctx.Logger.Debug("GetImage md5Sum: %s, req: %#v", req.Md5, req)
cacheKey := ctx.cacheKey(req)
if ctx.isCacheEnable(nil) {
data, err := ctx.Cache.Get(cacheKey)
if err == nil {
ctx.Logger.Debug("GetImage md5Sum: %s, GetCache %s", req.Md5, cacheKey)
return data, nil
}
ctx.Logger.Debug("GetImage md5Sum: %s, GetCache %s err: %s", req.Md5, cacheKey, err)
}
data, err := ctx.Storage.Get(req)
if err == nil {
if ctx.isCacheEnable(data) {
if err = ctx.Cache.Set(cacheKey, data); err != nil {
ctx.Logger.Warn("GetImage md5Sum: %s, SetCache %s err: %s", req.Md5, cacheKey, err)
} else {
ctx.Logger.Debug("GetImage md5Sum: %s, SetCache %s", req.Md5, cacheKey)
}
}
return data, nil
}
var originData []byte
saveToCache := false
originReq := ctx.originRequest(req.Md5)
if ctx.isCacheEnable(nil) {
originCacheKey := ctx.cacheKey(originReq)
originData, err = ctx.Cache.Get(originCacheKey)
if err != nil {
saveToCache = true
ctx.Logger.Debug("GetImage md5Sum: %s, GetOriginCache %s err: %s", req.Md5, originCacheKey, err)
} else {
ctx.Logger.Debug("GetImage md5Sum: %s, GetOriginCache %s", req.Md5, originCacheKey)
}
}
if originData == nil {
originData, err = ctx.Storage.Get(originReq)
if err != nil {
ctx.Logger.Warn("GetImage md5Sum: %s, GetStorage err: %s", req.Md5, err)
return nil, err
} else if ctx.isCacheEnable(originData) && saveToCache {
originCacheKey := ctx.cacheKey(originReq)
if err = ctx.Cache.Set(originCacheKey, originData); err != nil {
ctx.Logger.Warn("GetImage md5Sum: %s, SetOriginCache %s err: %s", req.Md5, originCacheKey, err)
} else {
ctx.Logger.Debug("GetImage md5Sum: %s, SetOriginCache %s", req.Md5, originCacheKey)
}
}
}
data, err = ctx.Image.Convert(originData, *req)
if err != nil {
ctx.Logger.Warn("GetImage md5Sum: %s, Image.Convert err: %s", req.Md5, err)
return nil, err
}
if ctx.isCacheEnable(nil) {
if err = ctx.Cache.Set(cacheKey, data); err != nil {
ctx.Logger.Warn("GetImage md5Sum: %s, SetCache %s err: %s", req.Md5, cacheKey, err)
} else {
ctx.Logger.Debug("GetImage md5Sum: %s, SetCache %s", req.Md5, cacheKey)
}
}
if req.Save {
if err := ctx.Storage.Set(req, data); err != nil {
ctx.Logger.Warn("GetImage md5Sum: %s, save new image err :%s", req.Md5, err)
}
}
return data, nil
}
// InfoImage get a image information according the md5 key and make a image response.
func (ctx *KimgContext) InfoImage(req *KimgRequest) (*KimgResponse, error) {
ctx.Logger.Debug("InfoImage md5Sum: %s", req.Md5)
var data []byte
var err error
cacheKey := ctx.cacheKey(req)
saveToCache := false
if ctx.isCacheEnable(nil) {
data, err = ctx.Cache.Get(cacheKey)
if err != nil {
saveToCache = true
ctx.Logger.Debug("InfoImage md5Sum: %s, GetCache %s err: %s", req.Md5, cacheKey, err)
}
}
if data == nil {
data, err = ctx.Storage.Get(req)
if err != nil {
ctx.Logger.Warn("InfoImage md5Sum: %s, GetStorage err: %s", req.Md5, err)
return nil, err
} else if ctx.isCacheEnable(data) && saveToCache {
if err = ctx.Cache.Set(cacheKey, data); err != nil {
ctx.Logger.Warn("InfoImage md5Sum: %s, SetCache %s err: %s", req.Md5, cacheKey, err)
} else {
ctx.Logger.Debug("InfoImage md5Sum: %s, SetCache %s", req.Md5, cacheKey)
}
}
}
resp, err := ctx.Image.Info(req, data)
if err != nil {
ctx.Logger.Warn("InfoImage md5Sum: %s, Image.Info err: %s", req.Md5, err)
return nil, err
}
return resp, err
}
// DeleteImage delete a image from kimg according the md5 key.
func (ctx *KimgContext) DeleteImage(md5Sum string) error {
ctx.Logger.Debug("DeleteImage md5Sum: %s", md5Sum)
req := ctx.originRequest(md5Sum)
if ctx.isCacheEnable(nil) {
cacheKey := ctx.cacheKey(req)
err := ctx.Cache.Del(cacheKey)
if err != nil {
ctx.Logger.Warn("DeleteImage md5Sum: %s, DelCache %s err: %s", req.Md5, cacheKey, err)
} else {
ctx.Logger.Debug("DeleteImage md5Sum: %s, DelCache %s", req.Md5, cacheKey)
}
}
err := ctx.Storage.Del(req)
if err != nil {
ctx.Logger.Warn("DeleteImage md5Sum: %s, DelStorage err: %s", req.Md5, err)
return err
}
return nil
}
func (ctx *KimgContext) isCacheEnable(data []byte) bool {
return ctx.Cache != nil && (data != nil || ctx.Config.Cache.MaxSize >= len(data))
}
func (ctx *KimgContext) cacheKey(req *KimgRequest) string {
if req.Origin {
return req.Md5
}
return fmt.Sprintf("%s:%s", req.Md5, req.Key())
}
func (ctx *KimgContext) originRequest(md5Sum string) *KimgRequest {
return &KimgRequest{Md5: md5Sum, Origin: true}
}