Browse Source

Simplify WebP preset config

DarthSim 6 days ago
parent
commit
92a281f834
4 changed files with 71 additions and 27 deletions
  1. 5 3
      config/config.go
  2. 12 0
      config/configurators/configurators.go
  3. 41 0
      config/webp_preset.go
  4. 13 24
      vips/vips.go

+ 5 - 3
config/config.go

@@ -56,7 +56,7 @@ var (
 	AvifSpeed             int
 	AvifSpeed             int
 	JxlEffort             int
 	JxlEffort             int
 	WebpEffort            int
 	WebpEffort            int
-	WebpPreset            string
+	WebpPreset            WebpPresetKind
 	Quality               int
 	Quality               int
 	FormatQuality         map[imagetype.Type]int
 	FormatQuality         map[imagetype.Type]int
 	StripMetadata         bool
 	StripMetadata         bool
@@ -263,7 +263,7 @@ func Reset() {
 	AvifSpeed = 8
 	AvifSpeed = 8
 	JxlEffort = 4
 	JxlEffort = 4
 	WebpEffort = 4
 	WebpEffort = 4
-	WebpPreset = "default"
+	WebpPreset = WebpPresetDefault
 	Quality = 80
 	Quality = 80
 	FormatQuality = map[imagetype.Type]int{
 	FormatQuality = map[imagetype.Type]int{
 		imagetype.WEBP: 79,
 		imagetype.WEBP: 79,
@@ -496,7 +496,9 @@ func Configure() error {
 	configurators.Int(&AvifSpeed, "IMGPROXY_AVIF_SPEED")
 	configurators.Int(&AvifSpeed, "IMGPROXY_AVIF_SPEED")
 	configurators.Int(&JxlEffort, "IMGPROXY_JXL_EFFORT")
 	configurators.Int(&JxlEffort, "IMGPROXY_JXL_EFFORT")
 	configurators.Int(&WebpEffort, "IMGPROXY_WEBP_EFFORT")
 	configurators.Int(&WebpEffort, "IMGPROXY_WEBP_EFFORT")
-	configurators.String(&WebpPreset, "IMGPROXY_WEBP_PRESET")
+	if err := configurators.FromMap(&WebpPreset, "IMGPROXY_WEBP_PRESET", WebpPresets); err != nil {
+		return err
+	}
 	configurators.Int(&Quality, "IMGPROXY_QUALITY")
 	configurators.Int(&Quality, "IMGPROXY_QUALITY")
 	if err := configurators.ImageTypesQuality(FormatQuality, "IMGPROXY_FORMAT_QUALITY"); err != nil {
 	if err := configurators.ImageTypesQuality(FormatQuality, "IMGPROXY_FORMAT_QUALITY"); err != nil {
 		return err
 		return err

+ 12 - 0
config/configurators/configurators.go

@@ -289,3 +289,15 @@ func RegexpFromPattern(pattern string) *regexp.Regexp {
 	// It is safe to use regexp.MustCompile since the expression is always valid
 	// It is safe to use regexp.MustCompile since the expression is always valid
 	return regexp.MustCompile(result.String())
 	return regexp.MustCompile(result.String())
 }
 }
+
+func FromMap[T any](v *T, name string, m map[string]T) error {
+	if env := os.Getenv(name); len(env) > 0 {
+		if val, ok := m[env]; ok {
+			*v = val
+		} else {
+			return fmt.Errorf("Invalid %s value: %s", name, env)
+		}
+	}
+
+	return nil
+}

+ 41 - 0
config/webp_preset.go

@@ -0,0 +1,41 @@
+package config
+
+import "fmt"
+
+type WebpPresetKind int
+
+const (
+	WebpPresetDefault WebpPresetKind = iota
+	WebpPresetPhoto
+	WebpPresetPicture
+	WebpPresetDrawing
+	WebpPresetIcon
+	WebpPresetText
+)
+
+var WebpPresets = map[string]WebpPresetKind{
+	"default": WebpPresetDefault,
+	"photo":   WebpPresetPhoto,
+	"picture": WebpPresetPicture,
+	"drawing": WebpPresetDrawing,
+	"icon":    WebpPresetIcon,
+	"text":    WebpPresetText,
+}
+
+func (wp WebpPresetKind) String() string {
+	for k, v := range WebpPresets {
+		if v == wp {
+			return k
+		}
+	}
+	return ""
+}
+
+func (wp WebpPresetKind) MarshalJSON() ([]byte, error) {
+	for k, v := range WebpPresets {
+		if v == wp {
+			return []byte(fmt.Sprintf("%q", k)), nil
+		}
+	}
+	return []byte("null"), nil
+}

+ 13 - 24
vips/vips.go

@@ -33,30 +33,10 @@ import (
 	"github.com/imgproxy/imgproxy/v3/metrics/prometheus"
 	"github.com/imgproxy/imgproxy/v3/metrics/prometheus"
 )
 )
 
 
-const (
-	webpPresetDefault = "default"
-	webpPresetPhoto   = "photo"
-	webpPresetPicture = "picture"
-	webpPresetDrawing = "drawing"
-	webpPresetIcon    = "icon"
-	webpPresetText    = "text"
-)
-
 type Image struct {
 type Image struct {
 	VipsImage *C.VipsImage
 	VipsImage *C.VipsImage
 }
 }
 
 
-var (
-	cWebpPreset = map[string]C.VipsForeignWebpPreset{
-		webpPresetDefault: C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT,
-		webpPresetPhoto:   C.VIPS_FOREIGN_WEBP_PRESET_PHOTO,
-		webpPresetPicture: C.VIPS_FOREIGN_WEBP_PRESET_PICTURE,
-		webpPresetDrawing: C.VIPS_FOREIGN_WEBP_PRESET_DRAWING,
-		webpPresetIcon:    C.VIPS_FOREIGN_WEBP_PRESET_ICON,
-		webpPresetText:    C.VIPS_FOREIGN_WEBP_PRESET_TEXT,
-	}
-)
-
 var (
 var (
 	typeSupportLoad sync.Map
 	typeSupportLoad sync.Map
 	typeSupportSave sync.Map
 	typeSupportSave sync.Map
@@ -127,10 +107,19 @@ func Init() error {
 	vipsConf.PngUnlimited = gbool(config.PngUnlimited)
 	vipsConf.PngUnlimited = gbool(config.PngUnlimited)
 	vipsConf.SvgUnlimited = gbool(config.SvgUnlimited)
 	vipsConf.SvgUnlimited = gbool(config.SvgUnlimited)
 
 
-	if p, ok := cWebpPreset[config.WebpPreset]; ok {
-		vipsConf.WebpPreset = p
-	} else {
-		return newVipsErrorf("invalid libwebp preset: %s", config.WebpPreset)
+	switch config.WebpPreset {
+	case config.WebpPresetPhoto:
+		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_PHOTO
+	case config.WebpPresetPicture:
+		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_PICTURE
+	case config.WebpPresetDrawing:
+		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_DRAWING
+	case config.WebpPresetIcon:
+		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_ICON
+	case config.WebpPresetText:
+		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_TEXT
+	default:
+		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT
 	}
 	}
 
 
 	prometheus.AddGaugeFunc(
 	prometheus.AddGaugeFunc(