1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-12-09 09:56:01 +02:00
imgproxy/options/presets.go

70 lines
1.3 KiB
Go
Raw Normal View History

2021-04-26 13:52:50 +02:00
package options
2018-09-07 19:41:06 +02:00
import (
2018-11-06 13:19:34 +02:00
"fmt"
"strings"
)
2018-09-07 19:41:06 +02:00
2021-04-26 13:52:50 +02:00
var presets map[string]urlOptions
2018-09-07 19:41:06 +02:00
2021-04-26 13:52:50 +02:00
func ParsePresets(presetStrs []string) error {
for _, presetStr := range presetStrs {
if err := parsePreset(presetStr); err != nil {
return err
}
}
return nil
}
func parsePreset(presetStr string) error {
2018-09-07 19:41:06 +02:00
presetStr = strings.Trim(presetStr, " ")
if len(presetStr) == 0 || strings.HasPrefix(presetStr, "#") {
2018-11-06 13:19:34 +02:00
return nil
2018-09-07 19:41:06 +02:00
}
parts := strings.Split(presetStr, "=")
if len(parts) != 2 {
2018-11-06 13:19:34 +02:00
return fmt.Errorf("Invalid preset string: %s", presetStr)
2018-09-07 19:41:06 +02:00
}
name := strings.Trim(parts[0], " ")
if len(name) == 0 {
2018-11-06 13:19:34 +02:00
return fmt.Errorf("Empty preset name: %s", presetStr)
2018-09-07 19:41:06 +02:00
}
value := strings.Trim(parts[1], " ")
if len(value) == 0 {
2018-11-06 13:19:34 +02:00
return fmt.Errorf("Empty preset value: %s", presetStr)
2018-09-07 19:41:06 +02:00
}
optsStr := strings.Split(value, "/")
opts, rest := parseURLOptions(optsStr)
if len(rest) > 0 {
2018-11-06 13:19:34 +02:00
return fmt.Errorf("Invalid preset value: %s", presetStr)
}
2021-04-26 13:52:50 +02:00
if presets == nil {
presets = make(map[string]urlOptions)
}
presets[name] = opts
2018-11-06 13:19:34 +02:00
return nil
}
2021-04-26 13:52:50 +02:00
func ValidatePresets() error {
var po ProcessingOptions
2021-04-26 13:52:50 +02:00
for name, opts := range presets {
if err := applyURLOptions(&po, opts); err != nil {
2018-11-06 13:19:34 +02:00
return fmt.Errorf("Error in preset `%s`: %s", name, err)
}
2018-09-07 19:41:06 +02:00
}
2018-11-06 13:19:34 +02:00
return nil
2018-09-07 19:41:06 +02:00
}