config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package fs
  2. import (
  3. "log/slog"
  4. "os"
  5. "github.com/imgproxy/imgproxy/v3/ensure"
  6. "github.com/imgproxy/imgproxy/v3/env"
  7. )
  8. var (
  9. IMGPROXY_LOCAL_FILESYSTEM_ROOT = env.Describe("IMGPROXY_LOCAL_FILESYSTEM_ROOT", "path")
  10. )
  11. // Config holds the configuration for local file system transport
  12. type Config struct {
  13. Root string // Root directory for the local file system transport
  14. }
  15. // NewDefaultConfig returns a new default configuration for local file system transport
  16. func NewDefaultConfig() Config {
  17. return Config{
  18. Root: "",
  19. }
  20. }
  21. // LoadConfigFromEnv loads configuration from the global config package
  22. func LoadConfigFromEnv(c *Config) (*Config, error) {
  23. c = ensure.Ensure(c, NewDefaultConfig)
  24. err := env.String(&c.Root, IMGPROXY_LOCAL_FILESYSTEM_ROOT)
  25. return c, err
  26. }
  27. // Validate checks if the configuration is valid
  28. func (c *Config) Validate() error {
  29. e := IMGPROXY_LOCAL_FILESYSTEM_ROOT
  30. if c.Root == "" {
  31. return e.ErrorEmpty()
  32. }
  33. stat, err := os.Stat(c.Root)
  34. if err != nil {
  35. return e.Errorf("cannot use local directory: %s", err)
  36. }
  37. if !stat.IsDir() {
  38. return e.Errorf("cannot use local directory: not a directory")
  39. }
  40. if c.Root == "/" {
  41. slog.Warn("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  42. }
  43. return nil
  44. }