1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-04-17 07:07:11 +02:00

Check presets options on start; Exit on preset error

This commit is contained in:
DarthSim 2018-09-12 19:46:43 +06:00
parent 11acf1f96a
commit 017724d20d
3 changed files with 50 additions and 21 deletions

View File

@ -75,7 +75,7 @@ func hexFileConfig(b *[]byte, filepath string) {
*b = dst[:n] *b = dst[:n]
} }
func presetEnvConfig(p *presets, name string) { func presetEnvConfig(p presets, name string) {
if env := os.Getenv(name); len(env) > 0 { if env := os.Getenv(name); len(env) > 0 {
presetStrings := strings.Split(env, ",") 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 { if len(filepath) == 0 {
return return
} }
@ -99,6 +99,10 @@ func presetFileConfig(p *presets, filepath string) {
for scanner.Scan() { for scanner.Scan() {
parsePreset(p, scanner.Text()) parsePreset(p, scanner.Text())
} }
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read presets file: %s", err)
}
} }
type config struct { type config struct {
@ -215,8 +219,8 @@ func init() {
strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL") strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
conf.Presets = make(presets) conf.Presets = make(presets)
presetEnvConfig(&conf.Presets, "IMGPROXY_PRESETS") presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS")
presetFileConfig(&conf.Presets, *presetsPath) presetFileConfig(conf.Presets, *presetsPath)
if len(conf.Key) == 0 { if len(conf.Key) == 0 {
log.Fatalln("Key is not defined") log.Fatalln("Key is not defined")
@ -295,6 +299,8 @@ func init() {
} }
} }
checkPresets(conf.Presets)
initVips() initVips()
initDownloading() initDownloading()
} }

View File

@ -1,10 +1,13 @@
package main package main
import "strings" import (
"log"
"strings"
)
type presets map[string]urlOptions type presets map[string]urlOptions
func parsePreset(p *presets, presetStr string) { func parsePreset(p presets, presetStr string) {
presetStr = strings.Trim(presetStr, " ") presetStr = strings.Trim(presetStr, " ")
if len(presetStr) == 0 || strings.HasPrefix(presetStr, "#") { if len(presetStr) == 0 || strings.HasPrefix(presetStr, "#") {
@ -14,27 +17,39 @@ func parsePreset(p *presets, presetStr string) {
parts := strings.Split(presetStr, "=") parts := strings.Split(presetStr, "=")
if len(parts) != 2 { if len(parts) != 2 {
warning("Invalid preset string, omitted: %s", presetStr) log.Fatalf("Invalid preset string: %s", presetStr)
return return
} }
name := strings.Trim(parts[0], " ") name := strings.Trim(parts[0], " ")
if len(name) == 0 { if len(name) == 0 {
warning("Empty preset name, omitted: %s", presetStr) log.Fatalf("Empty preset name: %s", presetStr)
return return
} }
value := strings.Trim(parts[1], " ") value := strings.Trim(parts[1], " ")
if len(value) == 0 { if len(value) == 0 {
warning("Empty preset value, omitted: %s", presetStr) log.Fatalf("Empty preset value: %s", presetStr)
return return
} }
optsStr := strings.Split(value, "/") optsStr := strings.Split(value, "/")
if opts, rest := parseURLOptions(optsStr); len(rest) == 0 { opts, rest := parseURLOptions(optsStr)
(*p)[name] = opts
} else { if len(rest) > 0 {
warning("Invalid preset value, omitted: %s", presetStr) 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)
}
} }
} }

View File

@ -280,10 +280,8 @@ func applySharpenOption(po *processingOptions, args []string) error {
func applyPresetOption(po *processingOptions, args []string) error { func applyPresetOption(po *processingOptions, args []string) error {
for _, preset := range args { for _, preset := range args {
if p, ok := conf.Presets[preset]; ok { if p, ok := conf.Presets[preset]; ok {
for name, pargs := range p { if err := applyProcessingOptions(po, p); err != nil {
if err := applyProcessingOption(po, name, pargs); err != nil { return err
return err
}
} }
} else { } else {
return fmt.Errorf("Unknown asset: %s", preset) 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 { if err := applyPresetOption(po, args); err != nil {
return err 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 return nil
@ -422,10 +432,8 @@ func parsePathAdvanced(parts []string, acceptHeader string) (string, processingO
options, urlParts := parseURLOptions(parts) options, urlParts := parseURLOptions(parts)
for name, args := range options { if err := applyProcessingOptions(&po, options); err != nil {
if err := applyProcessingOption(&po, name, args); err != nil { return "", po, err
return "", po, err
}
} }
url, extension, err := decodeURL(urlParts) url, extension, err := decodeURL(urlParts)