mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-02-02 11:34:20 +02:00
Check presets options on start; Exit on preset error
This commit is contained in:
parent
11acf1f96a
commit
017724d20d
14
config.go
14
config.go
@ -75,7 +75,7 @@ func hexFileConfig(b *[]byte, filepath string) {
|
||||
*b = dst[:n]
|
||||
}
|
||||
|
||||
func presetEnvConfig(p *presets, name string) {
|
||||
func presetEnvConfig(p presets, name string) {
|
||||
if env := os.Getenv(name); len(env) > 0 {
|
||||
presetStrings := strings.Split(env, ",")
|
||||
|
||||
@ -85,7 +85,7 @@ func presetEnvConfig(p *presets, name string) {
|
||||
}
|
||||
}
|
||||
|
||||
func presetFileConfig(p *presets, filepath string) {
|
||||
func presetFileConfig(p presets, filepath string) {
|
||||
if len(filepath) == 0 {
|
||||
return
|
||||
}
|
||||
@ -99,6 +99,10 @@ func presetFileConfig(p *presets, filepath string) {
|
||||
for scanner.Scan() {
|
||||
parsePreset(p, scanner.Text())
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatalf("Failed to read presets file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
type config struct {
|
||||
@ -215,8 +219,8 @@ func init() {
|
||||
strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
|
||||
|
||||
conf.Presets = make(presets)
|
||||
presetEnvConfig(&conf.Presets, "IMGPROXY_PRESETS")
|
||||
presetFileConfig(&conf.Presets, *presetsPath)
|
||||
presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS")
|
||||
presetFileConfig(conf.Presets, *presetsPath)
|
||||
|
||||
if len(conf.Key) == 0 {
|
||||
log.Fatalln("Key is not defined")
|
||||
@ -295,6 +299,8 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
checkPresets(conf.Presets)
|
||||
|
||||
initVips()
|
||||
initDownloading()
|
||||
}
|
||||
|
33
presets.go
33
presets.go
@ -1,10 +1,13 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type presets map[string]urlOptions
|
||||
|
||||
func parsePreset(p *presets, presetStr string) {
|
||||
func parsePreset(p presets, presetStr string) {
|
||||
presetStr = strings.Trim(presetStr, " ")
|
||||
|
||||
if len(presetStr) == 0 || strings.HasPrefix(presetStr, "#") {
|
||||
@ -14,27 +17,39 @@ func parsePreset(p *presets, presetStr string) {
|
||||
parts := strings.Split(presetStr, "=")
|
||||
|
||||
if len(parts) != 2 {
|
||||
warning("Invalid preset string, omitted: %s", presetStr)
|
||||
log.Fatalf("Invalid preset string: %s", presetStr)
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.Trim(parts[0], " ")
|
||||
if len(name) == 0 {
|
||||
warning("Empty preset name, omitted: %s", presetStr)
|
||||
log.Fatalf("Empty preset name: %s", presetStr)
|
||||
return
|
||||
}
|
||||
|
||||
value := strings.Trim(parts[1], " ")
|
||||
if len(value) == 0 {
|
||||
warning("Empty preset value, omitted: %s", presetStr)
|
||||
log.Fatalf("Empty preset value: %s", presetStr)
|
||||
return
|
||||
}
|
||||
|
||||
optsStr := strings.Split(value, "/")
|
||||
|
||||
if opts, rest := parseURLOptions(optsStr); len(rest) == 0 {
|
||||
(*p)[name] = opts
|
||||
} else {
|
||||
warning("Invalid preset value, omitted: %s", presetStr)
|
||||
opts, rest := parseURLOptions(optsStr)
|
||||
|
||||
if len(rest) > 0 {
|
||||
log.Fatalf("Invalid preset value: %s", presetStr)
|
||||
}
|
||||
|
||||
p[name] = opts
|
||||
}
|
||||
|
||||
func checkPresets(p presets) {
|
||||
var po processingOptions
|
||||
|
||||
for name, opts := range p {
|
||||
if err := applyProcessingOptions(&po, opts); err != nil {
|
||||
log.Fatalf("Error in preset `%s`: %s", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -280,10 +280,8 @@ func applySharpenOption(po *processingOptions, args []string) error {
|
||||
func applyPresetOption(po *processingOptions, args []string) error {
|
||||
for _, preset := range args {
|
||||
if p, ok := conf.Presets[preset]; ok {
|
||||
for name, pargs := range p {
|
||||
if err := applyProcessingOption(po, name, pargs); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applyProcessingOptions(po, p); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("Unknown asset: %s", preset)
|
||||
@ -358,6 +356,18 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
|
||||
if err := applyPresetOption(po, args); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("Unknown processing option: %s", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyProcessingOptions(po *processingOptions, options urlOptions) error {
|
||||
for name, args := range options {
|
||||
if err := applyProcessingOption(po, name, args); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -422,10 +432,8 @@ func parsePathAdvanced(parts []string, acceptHeader string) (string, processingO
|
||||
|
||||
options, urlParts := parseURLOptions(parts)
|
||||
|
||||
for name, args := range options {
|
||||
if err := applyProcessingOption(&po, name, args); err != nil {
|
||||
return "", po, err
|
||||
}
|
||||
if err := applyProcessingOptions(&po, options); err != nil {
|
||||
return "", po, err
|
||||
}
|
||||
|
||||
url, extension, err := decodeURL(urlParts)
|
||||
|
Loading…
x
Reference in New Issue
Block a user