config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package optionsparser
  2. import (
  3. "errors"
  4. "slices"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/ensure"
  7. )
  8. // URLReplacement represents a URL replacement configuration
  9. type URLReplacement = config.URLReplacement
  10. // Config represents the configuration for options processing
  11. type Config struct {
  12. // Presets configuration
  13. Presets []string // Available presets
  14. OnlyPresets bool // Whether to allow only presets
  15. // Security and validation
  16. AllowedProcessingOptions []string // List of allowed processing options
  17. AllowSecurityOptions bool // Whether to allow security options in URLs
  18. // Format preference and enforcement
  19. AutoWebp bool // Whether to automatically serve WebP when supported
  20. EnforceWebp bool // Whether to enforce WebP format
  21. AutoAvif bool // Whether to automatically serve AVIF when supported
  22. EnforceAvif bool // Whether to enforce AVIF format
  23. AutoJxl bool // Whether to automatically serve JXL when supported
  24. EnforceJxl bool // Whether to enforce JXL format
  25. // Client hints
  26. EnableClientHints bool // Whether to enable client hints support
  27. // URL processing
  28. ArgumentsSeparator string // Separator for URL arguments
  29. BaseURL string // Base URL for relative URLs
  30. URLReplacements []URLReplacement // URL replacement rules
  31. Base64URLIncludesFilename bool // Whether base64 URLs include filename
  32. }
  33. // NewDefaultConfig creates a new default configuration for options processing
  34. func NewDefaultConfig() Config {
  35. return Config{
  36. // Presets configuration
  37. OnlyPresets: false,
  38. // Security and validation
  39. AllowSecurityOptions: false,
  40. // Format preference and enforcement (copied from global config defaults)
  41. AutoWebp: false,
  42. EnforceWebp: false,
  43. AutoAvif: false,
  44. EnforceAvif: false,
  45. AutoJxl: false,
  46. EnforceJxl: false,
  47. // Client hints
  48. EnableClientHints: false,
  49. // URL processing (copied from global config defaults)
  50. ArgumentsSeparator: ":",
  51. BaseURL: "",
  52. Base64URLIncludesFilename: false,
  53. }
  54. }
  55. // LoadConfigFromEnv loads configuration from global config variables
  56. func LoadConfigFromEnv(c *Config) (*Config, error) {
  57. c = ensure.Ensure(c, NewDefaultConfig)
  58. // Presets configuration
  59. c.Presets = slices.Clone(config.Presets)
  60. c.OnlyPresets = config.OnlyPresets
  61. // Security and validation
  62. c.AllowedProcessingOptions = slices.Clone(config.AllowedProcessingOptions)
  63. c.AllowSecurityOptions = config.AllowSecurityOptions
  64. // Format preference and enforcement
  65. c.AutoWebp = config.AutoWebp
  66. c.EnforceWebp = config.EnforceWebp
  67. c.AutoAvif = config.AutoAvif
  68. c.EnforceAvif = config.EnforceAvif
  69. c.AutoJxl = config.AutoJxl
  70. c.EnforceJxl = config.EnforceJxl
  71. // Client hints
  72. c.EnableClientHints = config.EnableClientHints
  73. // URL processing
  74. c.ArgumentsSeparator = config.ArgumentsSeparator
  75. c.BaseURL = config.BaseURL
  76. c.URLReplacements = slices.Clone(config.URLReplacements)
  77. c.Base64URLIncludesFilename = config.Base64URLIncludesFilename
  78. return c, nil
  79. }
  80. // Validate validates the configuration values
  81. func (c *Config) Validate() error {
  82. // Arguments separator validation
  83. if c.ArgumentsSeparator == "" {
  84. return errors.New("arguments separator cannot be empty")
  85. }
  86. return nil
  87. }