-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
328 lines (294 loc) · 8.54 KB
/
config.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
package main
import (
"errors"
"fmt"
"os"
"github.com/johan-st/go-image-server/images"
"github.com/johan-st/go-image-server/units/size"
"gopkg.in/yaml.v3"
)
type config struct {
LogLevel string `yaml:"log_level"`
Http confHttp `yaml:"http"`
Files confFiles `yaml:"files"`
Cache confCache `yaml:"cache_rules"`
ImageDefaults confImageDefault `yaml:"image_defaults"`
ImagePresets []confImagePreset `yaml:"image_presets"`
}
type confHttp struct {
Port int `yaml:"port"`
Host string `yaml:"host"`
Docs bool `yaml:"documentation"`
AccessLog string `yaml:"access_logfile"`
MaxUploadSize string `yaml:"max_upload_size"`
}
type confFiles struct {
ClearOnStart bool `yaml:"clear_on_start"`
ClearOnExit bool `yaml:"clear_on_exit"`
SetPerms bool `yaml:"set_perms"`
CreateDirs bool `yaml:"create_dirs"`
DirOriginals string `yaml:"originals_dir"`
DirCache string `yaml:"cache_dir"`
PopulateFrom string `yaml:"populate_from"`
}
type confCache struct {
Cap int `yaml:"max_objects"`
MaxSize string `yaml:"max_size"`
}
type confImageDefault struct {
Format string `yaml:"format"`
QualityJpeg int `yaml:"quality_jpeg"`
QualityGif int `yaml:"quality_gif"`
Width int `yaml:"width"`
Height int `yaml:"height"`
MaxSize string `yaml:"max_size"`
Interpolation string `yaml:"interpolation"`
}
type confImagePreset struct {
Name string `yaml:"name"`
Alias []string `yaml:"alias"`
Format string `yaml:"format,omitempty"`
Quality int `yaml:"quality,omitempty"`
Width int `yaml:"width"`
Height int `yaml:"height"`
MaxSize string `yaml:"max_size,omitempty"`
Interpolation string `yaml:"interpolation,omitempty"`
}
func saveConfig(c config, filename string) error {
bytes, err := yaml.Marshal(c)
if err != nil {
return err
}
return os.WriteFile(filename, bytes, 0644)
}
func loadConfig(filename string) (config, error) {
bytes, err := os.ReadFile(filename)
if err != nil {
return config{}, err
}
var c config
err = yaml.Unmarshal(bytes, &c)
if err != nil {
return config{}, err
}
return c, nil
}
// validate enforces config rules and returns an error if any are broken
func (c *config) validate() error {
errs := []error{}
// HTTP
// port needed
if c.Http.Port == 0 {
errs = append(errs, fmt.Errorf("server port must be set"))
}
if c.Http.MaxUploadSize == "" {
c.Http.MaxUploadSize = "20MB"
}
_, err := size.Parse(c.Http.MaxUploadSize)
if err != nil {
errs = append(errs, fmt.Errorf("server max upload size must be a valid size (e.g. 20MB)"))
}
// empty host is ok
// TODO: validate host format
// FILES
if c.Files.DirOriginals == "" {
errs = append(errs, fmt.Errorf("path for originals must be set"))
}
if c.Files.DirCache == "" {
errs = append(errs, fmt.Errorf("paths for cache must be set"))
}
if c.Cache.Cap == 0 {
errs = append(errs, fmt.Errorf("cache num must be greater than 0"))
}
// DEFAULT IMAGE PARAMETERS
if c.ImageDefaults.Format != "jpeg" && c.ImageDefaults.Format != "png" && c.ImageDefaults.Format != "gif" {
errs = append(errs, fmt.Errorf("default image parameters format must be set to a valid value. Valid values are: jpeg, png, gif"))
}
if c.ImageDefaults.QualityJpeg == 0 {
errs = append(errs, fmt.Errorf("default image parameters quality jpeg must be set to a value greater between 1 and 100 (inclusive)"))
}
if c.ImageDefaults.QualityGif == 0 {
errs = append(errs, fmt.Errorf("default image parameters quality gif must be set to a value greater between 1 and 256 (inclusive)"))
}
if c.ImageDefaults.Width == 0 && c.ImageDefaults.Height == 0 {
errs = append(errs, fmt.Errorf("default image parameters width or height (or both) must be set"))
}
// TODO: validate max_size format
// 0 is ok for max_size, it means no limit
// TODO: validate resize format
// IMAGE PARAMETERS
for _, p := range c.ImagePresets {
name := p.Name
if name == "" {
errs = append(errs, fmt.Errorf("image parameters name must be set"))
}
if p.Format != "" && p.Format != "jpeg" && p.Format != "png" && p.Format != "gif" {
errs = append(errs, fmt.Errorf("image parameters (name: \"%s\") format must be set to a valid value. Valid values are: jpeg, png, gif", name))
}
if p.Quality == 0 && p.Format == "jpeg" {
errs = append(errs, fmt.Errorf("image parameters (name: \"%s\") quality must be set to a value greater between 1 and 100 (inclusive)", name))
}
if p.Quality == 0 && p.Format == "gif" {
errs = append(errs, fmt.Errorf("image parameters (name: \"%s\") quality must be set to a value greater between 1 and 256 (inclusive)", name))
}
if p.Width == 0 && p.Height == 0 {
errs = append(errs, fmt.Errorf("image parameters (name: \"%s\") width or height (or both) must be set", name))
}
// TODO: validate max_size format
// 0 is ok for max_size, it means no limit
// TODO: validate resize format
}
// Return errors if any
if len(errs) > 0 {
errs = append(errs, fmt.Errorf("config validation failed"))
return errors.Join(errs...)
// return fmt.Errorf("config validation failed: %v", errs)
}
return nil
}
// TODO: handle errors by returning them?
func toImageDefaults(c confImageDefault) (images.ImageDefaults, error) {
errs := []error{}
format, err := images.ParseFormat(c.Format)
if err != nil {
errs = append(errs, err)
}
size, err := size.Parse(c.MaxSize)
if err != nil {
errs = append(errs, err)
}
interpolation, err := images.ParseInterpolation(c.Interpolation)
if err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
newErrs := []error{fmt.Errorf("(%d) errors while building ImageDefaults", len(errs))}
newErrs = append(newErrs, errs...)
return images.ImageDefaults{}, errors.Join(newErrs...)
}
return images.ImageDefaults{
Format: format,
QualityJpeg: c.QualityJpeg,
QualityGif: c.QualityGif,
Width: c.Width,
Height: c.Height,
MaxSize: size,
Interpolation: interpolation,
}, nil
}
func toImagePresets(conf []confImagePreset, def images.ImageDefaults) ([]images.ImagePreset, error) {
presets := []images.ImagePreset{}
var err error
errs := []error{}
for _, cip := range conf {
// format
var format images.Format
if cip.Format != "" {
format, err = images.ParseFormat(cip.Format)
if err != nil {
errs = append(errs, err)
}
} else {
format = def.Format
}
// size
var s size.S
if cip.MaxSize != "" {
s, err = size.Parse(cip.MaxSize)
if err != nil {
errs = append(errs, err)
}
} else {
s = def.MaxSize
}
// interpolation
var interpolation images.Interpolation
if cip.Interpolation != "" {
interpolation, err = images.ParseInterpolation(cip.Interpolation)
if err != nil {
errs = append(errs, err)
}
} else {
interpolation = def.Interpolation
}
// resulting preset
p := images.ImagePreset{
Name: cip.Name,
Alias: cip.Alias,
Format: format,
Quality: cip.Quality,
Width: cip.Width,
Height: cip.Height,
MaxSize: s,
Interpolation: interpolation,
}
presets = append(presets, p)
}
if len(errs) > 0 {
newErrs := []error{fmt.Errorf("(%d) errors while building ImagePresets", len(errs))}
newErrs = append(newErrs, errs...)
return []images.ImagePreset{}, errors.Join(newErrs...)
}
return presets, nil
}
func defaultConfig() config {
return config{
LogLevel: "info",
Http: confHttp{
Port: 8080,
Host: "",
Docs: false,
},
Files: confFiles{
ClearOnStart: false,
PopulateFrom: "",
SetPerms: false,
CreateDirs: false,
DirOriginals: "img/originals",
DirCache: "img/cached",
},
Cache: confCache{
Cap: 100000,
MaxSize: "500 GB",
},
ImageDefaults: confImageDefault{
Format: "jpeg",
QualityJpeg: 80,
QualityGif: 256,
Width: 0,
Height: 800,
MaxSize: "1 MB",
Interpolation: "nearestNeighbor",
},
ImagePresets: []confImagePreset{
{
Name: "thumbnail",
Alias: []string{"thumb", "th"},
Format: "jpeg",
Quality: 80,
Width: 150,
Height: 150,
MaxSize: "10 KB",
Interpolation: "lanczos3",
},
{
Name: "small",
Alias: []string{"small", "s"},
Height: 400,
Width: 0,
},
{
Name: "medium",
Alias: []string{"medium", "m"},
Height: 800,
Width: 0,
},
{
Name: "large",
Alias: []string{"large", "l"},
Height: 1600,
Width: 0,
},
},
}
}