-
Notifications
You must be signed in to change notification settings - Fork 179
/
parameters.go
255 lines (215 loc) · 5.47 KB
/
parameters.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
package picfit
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/ulule/gostorages"
"github.com/thoas/picfit/constants"
"github.com/thoas/picfit/engine"
"github.com/thoas/picfit/engine/backend"
"github.com/thoas/picfit/failure"
"github.com/thoas/picfit/image"
)
const (
defaultDegree = 90
defaultHeight = 0
defaultUpscale = true
defaultWidth = 0
)
var formats = map[string]image.Format{
"bmp": image.BMP,
"gif": image.GIF,
"jpeg": image.JPEG,
"jpg": image.JPEG,
"png": image.PNG,
"tiff": image.TIFF,
"webp": image.WEBP,
}
type Parameters struct {
output *image.ImageFile
operations []engine.EngineOperation
}
// newParameters returns Parameters for engine.
func (p *Processor) NewParameters(ctx context.Context, input *image.ImageFile, qs map[string]interface{}) (*Parameters, error) {
format, ok := qs["fmt"].(string)
filepath := input.Filepath
if ok {
if _, ok := engine.ContentTypes[format]; !ok {
return nil, fmt.Errorf("Unknown format %s", format)
}
}
if format == "" && p.engine.Format != "" {
format = p.engine.Format
}
if format == "" {
format = input.Format()
}
if format == "" {
format = p.engine.DefaultFormat
}
if format != input.Format() {
index := len(filepath) - len(input.Format())
filepath = filepath[:index] + format
if contentType, ok := engine.ContentTypes[format]; ok {
input.Headers["Content-Type"] = contentType
}
}
output := &image.ImageFile{
Source: input.Source,
Key: input.Key,
Headers: input.Headers,
Filepath: filepath,
}
var operations []engine.EngineOperation
op, ok := qs["op"].(string)
if ok {
operation := engine.Operation(op)
opts, err := p.newBackendOptionsFromParameters(operation, qs)
if err != nil {
return nil, err
}
opts.Format = formats[format]
operations = append(operations, engine.EngineOperation{
Options: opts,
Operation: operation,
})
}
ops, ok := qs["op"].([]string)
if ok {
for i := range ops {
var err error
engineOperation := &engine.EngineOperation{}
operation, k := engine.Operations[ops[i]]
if k {
engineOperation.Operation = operation
engineOperation.Options, err = p.newBackendOptionsFromParameters(operation, qs)
if err != nil {
return nil, err
}
} else {
engineOperation, err = p.NewEngineOperationFromQuery(ctx, ops[i])
if err != nil {
return nil, err
}
}
if engineOperation != nil {
engineOperation.Options.Format = formats[format]
operations = append(operations, *engineOperation)
}
}
}
return &Parameters{
output: output,
operations: operations,
}, nil
}
func (p Processor) NewEngineOperationFromQuery(ctx context.Context, op string) (*engine.EngineOperation, error) {
params := make(map[string]interface{})
var imagePaths []string
for _, p := range strings.Split(op, " ") {
l := strings.Split(p, ":")
if len(l) > 1 {
if l[0] == "path" {
imagePaths = append(imagePaths, l[1])
} else {
params[l[0]] = l[1]
}
}
}
op, ok := params["op"].(string)
if !ok {
return nil, nil
}
operation := engine.Operation(op)
opts, err := p.newBackendOptionsFromParameters(operation, params)
if err != nil {
return nil, err
}
for i := range imagePaths {
if _, err := p.sourceStorage.Stat(ctx, imagePaths[i]); errors.Is(err, gostorages.ErrNotExist) {
return nil, errors.Wrapf(failure.ErrFileNotExists, "file does not exist: %s", imagePaths[i])
}
file, err := image.FromStorage(ctx, p.sourceStorage, imagePaths[i])
if err != nil {
return nil, errors.Wrapf(err, "unable to load file from storage: %s", imagePaths[i])
}
opts.Images = append(opts.Images, *file)
}
return &engine.EngineOperation{
Options: opts,
Operation: operation,
}, nil
}
func (p Processor) newBackendOptionsFromParameters(operation engine.Operation, qs map[string]interface{}) (*backend.Options, error) {
var (
err error
quality = p.engine.DefaultQuality
upscale = defaultUpscale
height = defaultHeight
width = defaultWidth
degree = defaultDegree
)
q, ok := qs["q"].(string)
if ok {
quality, err = strconv.Atoi(q)
if err != nil {
return nil, err
}
if quality > 100 {
return nil, failure.ErrQuality
}
}
position, ok := qs["pos"].(string)
if !ok && operation == engine.Flip {
return nil, fmt.Errorf("Parameter \"pos\" not found in query string")
}
stick, ok := qs["stick"].(string)
if ok {
if !slices.Contains(constants.StickPositions, stick) {
return nil, fmt.Errorf("parameter \"stick\" has wrong value. Available values are: %v", constants.StickPositions)
}
}
color, _ := qs["color"].(string)
if deg, ok := qs["deg"].(string); ok {
degree, err = strconv.Atoi(deg)
if err != nil {
return nil, err
}
}
if up, ok := qs["upscale"].(string); ok {
upscale, err = strconv.ParseBool(up)
if err != nil {
return nil, err
}
}
if w, ok := qs["w"].(string); ok {
width, err = strconv.Atoi(w)
if err != nil {
return nil, err
}
}
if h, ok := qs["h"].(string); ok {
height, err = strconv.Atoi(h)
if err != nil {
return nil, err
}
}
filter, ok := qs["filter"].(string)
if ok && !slices.Contains(constants.Filters, filter) {
return nil, fmt.Errorf("parameter \"filter\" has wrong value. Available values are: %v", constants.Filters)
}
return &backend.Options{
Color: color,
Degree: degree,
Filter: filter,
Height: height,
Position: position,
Quality: quality,
Stick: stick,
Upscale: upscale,
Width: width,
}, nil
}