-
-
Notifications
You must be signed in to change notification settings - Fork 460
/
params.go
499 lines (407 loc) · 11 KB
/
params.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"encoding/json"
"errors"
"fmt"
"math"
"net/url"
"strconv"
"strings"
"github.com/h2non/bimg"
)
var ErrUnsupportedValue = errors.New("unsupported value")
// Coercion is the type that type coerces a parameter and defines the appropriate field on ImageOptions
type Coercion func(*ImageOptions, interface{}) error
var paramTypeCoercions = map[string]Coercion{
"width": coerceWidth,
"height": coerceHeight,
"quality": coerceQuality,
"top": coerceTop,
"left": coerceLeft,
"areawidth": coerceAreaWidth,
"areaheight": coerceAreaHeight,
"compression": coerceCompression,
"rotate": coerceRotate,
"margin": coerceMargin,
"factor": coerceFactor,
"dpi": coerceDPI,
"textwidth": coerceTextWidth,
"opacity": coerceOpacity,
"flip": coerceFlip,
"flop": coerceFlop,
"nocrop": coerceNoCrop,
"noprofile": coerceNoProfile,
"norotation": coerceNoRotation,
"noreplicate": coerceNoReplicate,
"force": coerceForce,
"embed": coerceEmbed,
"stripmeta": coerceStripMeta,
"text": coerceText,
"image": coerceImage,
"font": coerceFont,
"type": coerceImageType,
"color": coerceColor,
"colorspace": coerceColorSpace,
"gravity": coerceGravity,
"background": coerceBackground,
"extend": coerceExtend,
"sigma": coerceSigma,
"minampl": coerceMinAmpl,
"operations": coerceOperations,
"interlace": coerceInterlace,
"aspectratio": coerceAspectRatio,
"palette": coercePalette,
"speed": coerceSpeed,
}
func coerceTypeInt(param interface{}) (int, error) {
if v, ok := param.(int); ok {
return v, nil
}
if v, ok := param.(float64); ok {
return int(v), nil
}
if v, ok := param.(string); ok {
return parseInt(v)
}
return 0, ErrUnsupportedValue
}
func coerceTypeFloat(param interface{}) (float64, error) {
if v, ok := param.(float64); ok {
return v, nil
}
if v, ok := param.(int); ok {
return float64(v), nil
}
if v, ok := param.(string); ok {
result, err := parseFloat(v)
if err != nil {
return 0, ErrUnsupportedValue
}
return result, nil
}
return 0, ErrUnsupportedValue
}
func coerceTypeBool(param interface{}) (bool, error) {
if v, ok := param.(bool); ok {
return v, nil
}
if v, ok := param.(string); ok {
result, err := parseBool(v)
if err != nil {
return false, ErrUnsupportedValue
}
return result, nil
}
return false, ErrUnsupportedValue
}
func coerceTypeString(param interface{}) (string, error) {
if v, ok := param.(string); ok {
return v, nil
}
return "", ErrUnsupportedValue
}
func coerceHeight(io *ImageOptions, param interface{}) (err error) {
io.Height, err = coerceTypeInt(param)
return err
}
func coerceWidth(io *ImageOptions, param interface{}) (err error) {
io.Width, err = coerceTypeInt(param)
return err
}
func coerceQuality(io *ImageOptions, param interface{}) (err error) {
io.Quality, err = coerceTypeInt(param)
return err
}
func coerceTop(io *ImageOptions, param interface{}) (err error) {
io.Top, err = coerceTypeInt(param)
return err
}
func coerceLeft(io *ImageOptions, param interface{}) (err error) {
io.Left, err = coerceTypeInt(param)
return err
}
func coerceAreaWidth(io *ImageOptions, param interface{}) (err error) {
io.AreaWidth, err = coerceTypeInt(param)
return err
}
func coerceAreaHeight(io *ImageOptions, param interface{}) (err error) {
io.AreaHeight, err = coerceTypeInt(param)
return err
}
func coerceCompression(io *ImageOptions, param interface{}) (err error) {
io.Compression, err = coerceTypeInt(param)
return err
}
func coerceRotate(io *ImageOptions, param interface{}) (err error) {
io.Rotate, err = coerceTypeInt(param)
return err
}
func coerceMargin(io *ImageOptions, param interface{}) (err error) {
io.Margin, err = coerceTypeInt(param)
return err
}
func coerceFactor(io *ImageOptions, param interface{}) (err error) {
io.Factor, err = coerceTypeInt(param)
return err
}
func coerceDPI(io *ImageOptions, param interface{}) (err error) {
io.DPI, err = coerceTypeInt(param)
return err
}
func coerceTextWidth(io *ImageOptions, param interface{}) (err error) {
io.TextWidth, err = coerceTypeInt(param)
return err
}
func coerceOpacity(io *ImageOptions, param interface{}) (err error) {
v, err := coerceTypeFloat(param)
io.Opacity = float32(v)
return err
}
func coerceFlip(io *ImageOptions, param interface{}) (err error) {
io.Flip, err = coerceTypeBool(param)
io.IsDefinedField.Flip = true
return err
}
func coerceFlop(io *ImageOptions, param interface{}) (err error) {
io.Flop, err = coerceTypeBool(param)
io.IsDefinedField.Flop = true
return err
}
func coerceNoCrop(io *ImageOptions, param interface{}) (err error) {
io.NoCrop, err = coerceTypeBool(param)
io.IsDefinedField.NoCrop = true
return err
}
func coerceNoProfile(io *ImageOptions, param interface{}) (err error) {
io.NoProfile, err = coerceTypeBool(param)
io.IsDefinedField.NoProfile = true
return err
}
func coerceNoRotation(io *ImageOptions, param interface{}) (err error) {
io.NoRotation, err = coerceTypeBool(param)
io.IsDefinedField.NoRotation = true
return err
}
func coerceNoReplicate(io *ImageOptions, param interface{}) (err error) {
io.NoReplicate, err = coerceTypeBool(param)
io.IsDefinedField.NoReplicate = true
return err
}
func coerceForce(io *ImageOptions, param interface{}) (err error) {
io.Force, err = coerceTypeBool(param)
io.IsDefinedField.Force = true
return err
}
func coerceEmbed(io *ImageOptions, param interface{}) (err error) {
io.Embed, err = coerceTypeBool(param)
io.IsDefinedField.Embed = true
return err
}
func coerceStripMeta(io *ImageOptions, param interface{}) (err error) {
io.StripMetadata, err = coerceTypeBool(param)
io.IsDefinedField.StripMetadata = true
return err
}
func coerceText(io *ImageOptions, param interface{}) (err error) {
io.Text, err = coerceTypeString(param)
return err
}
func coerceImage(io *ImageOptions, param interface{}) (err error) {
io.Image, err = coerceTypeString(param)
return err
}
func coerceFont(io *ImageOptions, param interface{}) (err error) {
io.Font, err = coerceTypeString(param)
return err
}
func coerceImageType(io *ImageOptions, param interface{}) (err error) {
io.Type, err = coerceTypeString(param)
return err
}
func coerceColor(io *ImageOptions, param interface{}) error {
if v, ok := param.(string); ok {
io.Color = parseColor(v)
return nil
}
return ErrUnsupportedValue
}
func coerceColorSpace(io *ImageOptions, param interface{}) error {
if v, ok := param.(string); ok {
io.Colorspace = parseColorspace(v)
return nil
}
return ErrUnsupportedValue
}
func coerceGravity(io *ImageOptions, param interface{}) error {
if v, ok := param.(string); ok {
io.Gravity = parseGravity(v)
return nil
}
return ErrUnsupportedValue
}
func coerceBackground(io *ImageOptions, param interface{}) error {
if v, ok := param.(string); ok {
io.Background = parseColor(v)
return nil
}
return ErrUnsupportedValue
}
func coerceAspectRatio(io *ImageOptions, param interface{}) (err error) {
io.AspectRatio, err = coerceTypeString(param)
return err
}
func coerceExtend(io *ImageOptions, param interface{}) error {
if v, ok := param.(string); ok {
io.Extend = parseExtendMode(v)
return nil
}
return ErrUnsupportedValue
}
func coerceSigma(io *ImageOptions, param interface{}) (err error) {
io.Sigma, err = coerceTypeFloat(param)
return err
}
func coerceMinAmpl(io *ImageOptions, param interface{}) (err error) {
io.MinAmpl, err = coerceTypeFloat(param)
return err
}
func coerceOperations(io *ImageOptions, param interface{}) (err error) {
if v, ok := param.(string); ok {
ops, err := parseJSONOperations(v)
if err == nil {
io.Operations = ops
}
return err
}
return ErrUnsupportedValue
}
func coerceInterlace(io *ImageOptions, param interface{}) (err error) {
io.Interlace, err = coerceTypeBool(param)
io.IsDefinedField.Interlace = true
return err
}
func coercePalette(io *ImageOptions, param interface{}) (err error) {
io.Palette, err = coerceTypeBool(param)
io.IsDefinedField.Palette = true
return err
}
func coerceSpeed(io *ImageOptions, param interface{}) (err error) {
io.Speed, err = coerceTypeInt(param)
return err
}
func buildParamsFromOperation(op PipelineOperation) (ImageOptions, error) {
var options ImageOptions
// Apply defaults
options.Extend = bimg.ExtendCopy
for key, value := range op.Params {
fn, ok := paramTypeCoercions[key]
if !ok {
continue
}
err := fn(&options, value)
if err != nil {
return ImageOptions{}, fmt.Errorf(`error while processing parameter "%s" with value %q, error: %s`, key, value, err)
}
}
return options, nil
}
// buildParamsFromQuery builds the ImageOptions type from untyped parameters
func buildParamsFromQuery(query url.Values) (ImageOptions, error) {
var options ImageOptions
// Apply defaults
options.Extend = bimg.ExtendCopy
// Extract only known parameters
for key := range query {
fn, ok := paramTypeCoercions[key]
if !ok {
continue
}
value := query.Get(key)
err := fn(&options, value)
if err != nil {
return ImageOptions{}, fmt.Errorf(`error while processing parameter "%s" with value %q, error: %s`, key, value, err)
}
}
return options, nil
}
func parseBool(val string) (bool, error) {
if val == "" {
return false, nil
}
return strconv.ParseBool(val)
}
func parseInt(param string) (int, error) {
if param == "" {
return 0, nil
}
f, err := parseFloat(param)
return int(math.Floor(f + 0.5)), err
}
func parseFloat(param string) (float64, error) {
if param == "" {
return 0.0, nil
}
val, err := strconv.ParseFloat(param, 64)
return math.Abs(val), err
}
func parseColorspace(val string) bimg.Interpretation {
if val == "bw" {
return bimg.InterpretationBW
}
return bimg.InterpretationSRGB
}
func parseColor(val string) []uint8 {
const max float64 = 255
var buf []uint8
if val != "" {
for _, num := range strings.Split(val, ",") {
n, _ := strconv.ParseUint(strings.Trim(num, " "), 10, 8)
buf = append(buf, uint8(math.Min(float64(n), max)))
}
}
return buf
}
func parseJSONOperations(data string) (PipelineOperations, error) {
var operations PipelineOperations
// Fewer than 2 characters cannot be valid JSON. We assume empty operation.
if len(data) < 2 {
return operations, nil
}
d := json.NewDecoder(strings.NewReader(data))
d.DisallowUnknownFields()
err := d.Decode(&operations)
return operations, err
}
func parseExtendMode(val string) bimg.Extend {
val = strings.TrimSpace(strings.ToLower(val))
if val == "white" {
return bimg.ExtendWhite
}
if val == "black" {
return bimg.ExtendBlack
}
if val == "copy" {
return bimg.ExtendCopy
}
if val == "background" {
return bimg.ExtendBackground
}
if val == "lastpixel" {
return bimg.ExtendLast
}
return bimg.ExtendMirror
}
func parseGravity(val string) bimg.Gravity {
var m = map[string]bimg.Gravity{
"south": bimg.GravitySouth,
"north": bimg.GravityNorth,
"east": bimg.GravityEast,
"west": bimg.GravityWest,
"smart": bimg.GravitySmart,
}
val = strings.TrimSpace(strings.ToLower(val))
if g, ok := m[val]; ok {
return g
}
return bimg.GravityCentre
}