config.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package processing
  2. import (
  3. "errors"
  4. "github.com/imgproxy/imgproxy/v3/config"
  5. "github.com/imgproxy/imgproxy/v3/ensure"
  6. "github.com/imgproxy/imgproxy/v3/imagetype"
  7. "github.com/imgproxy/imgproxy/v3/vips"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. // Config holds processing-related configuration.
  11. type Config struct {
  12. PreferredFormats []imagetype.Type
  13. WatermarkOpacity float64
  14. DisableShrinkOnLoad bool
  15. }
  16. // NewConfig creates a new Config instance with the given parameters.
  17. func NewDefaultConfig() Config {
  18. return Config{
  19. WatermarkOpacity: 1,
  20. }
  21. }
  22. // NewConfig creates a new Config instance with the given parameters.
  23. func LoadConfigFromEnv(c *Config) (*Config, error) {
  24. c = ensure.Ensure(c, NewDefaultConfig)
  25. c.PreferredFormats = config.PreferredFormats
  26. return c, nil
  27. }
  28. // Validate checks if the configuration is valid
  29. func (c *Config) Validate() error {
  30. filtered := c.PreferredFormats[:0]
  31. for _, t := range c.PreferredFormats {
  32. if !vips.SupportsSave(t) {
  33. log.Warnf("%s can't be a preferred format as it's saving is not supported", t)
  34. } else {
  35. filtered = append(filtered, t)
  36. }
  37. }
  38. if len(filtered) == 0 {
  39. return errors.New("no supported preferred formats specified")
  40. }
  41. c.PreferredFormats = filtered
  42. if c.WatermarkOpacity <= 0 {
  43. return errors.New("watermark opacity should be greater than 0")
  44. } else if c.WatermarkOpacity > 1 {
  45. return errors.New("watermark opacity should be less than or equal to 1")
  46. }
  47. return nil
  48. }