config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package fs
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "github.com/imgproxy/imgproxy/v3/config"
  7. "github.com/imgproxy/imgproxy/v3/ensure"
  8. )
  9. // Config holds the configuration for local file system transport
  10. type Config struct {
  11. Root string // Root directory for the local file system transport
  12. }
  13. // NewDefaultConfig returns a new default configuration for local file system transport
  14. func NewDefaultConfig() Config {
  15. return Config{
  16. Root: "",
  17. }
  18. }
  19. // LoadConfigFromEnv loads configuration from the global config package
  20. func LoadConfigFromEnv(c *Config) (*Config, error) {
  21. c = ensure.Ensure(c, NewDefaultConfig)
  22. c.Root = config.LocalFileSystemRoot
  23. return c, nil
  24. }
  25. // Validate checks if the configuration is valid
  26. func (c *Config) Validate() error {
  27. if c.Root == "" {
  28. return errors.New("local file system root shold not be blank")
  29. }
  30. stat, err := os.Stat(c.Root)
  31. if err != nil {
  32. return fmt.Errorf("cannot use local directory: %s", err)
  33. }
  34. if !stat.IsDir() {
  35. return fmt.Errorf("cannot use local directory: not a directory")
  36. }
  37. if c.Root == "/" {
  38. // Warning: exposing root is unsafe
  39. // TODO: Move this somewhere to the instance checks (?)
  40. fmt.Println("Warning: Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  41. }
  42. return nil
  43. }