checker.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package security
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/options"
  4. "github.com/imgproxy/imgproxy/v3/options/keys"
  5. )
  6. // Checker represents the security package instance
  7. type Checker struct {
  8. config *Config
  9. }
  10. // New creates a new Security instance
  11. func New(config *Config) (*Checker, error) {
  12. if err := config.Validate(); err != nil {
  13. return nil, err
  14. }
  15. return &Checker{
  16. config: config,
  17. }, nil
  18. }
  19. // NewOptions creates a new [security.Options] instance
  20. // filling it from [options.Options].
  21. // If opts is nil, it returns default [security.Options].
  22. func (s *Checker) NewOptions(opts *options.Options) (secops Options) {
  23. secops = s.config.DefaultOptions
  24. if opts == nil {
  25. return
  26. }
  27. secops.MaxSrcResolution = opts.GetInt(
  28. keys.MaxSrcResolution, secops.MaxSrcResolution,
  29. )
  30. secops.MaxSrcFileSize = opts.GetInt(
  31. keys.MaxSrcFileSize, secops.MaxSrcFileSize,
  32. )
  33. secops.MaxAnimationFrames = opts.GetInt(
  34. keys.MaxAnimationFrames, secops.MaxAnimationFrames,
  35. )
  36. secops.MaxAnimationFrameResolution = opts.GetInt(
  37. keys.MaxAnimationFrameResolution, secops.MaxAnimationFrameResolution,
  38. )
  39. secops.MaxResultDimension = opts.GetInt(
  40. keys.MaxResultDimension, secops.MaxResultDimension,
  41. )
  42. return
  43. }