From 850498f8177c7ab53813a00ee82e4d815b00c0e0 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Fri, 29 Dec 2017 22:59:20 +0300 Subject: [PATCH] Add option for reading images from local filesystem --- README.md | 1 + config.go | 19 +++++++++++++++++++ download.go | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 3a4038e0..b170afcb 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ $ xxd -g 2 -l 64 -p /dev/random | tr -d '\n' * `IMGPROXY_CONCURRENCY` — the maximum number of image requests to be processed simultaneously. Default: double number of CPU cores; * `IMGPROXY_MAX_CLIENTS` — the maximum number of simultaneous active connections. Default: `IMGPROXY_CONCURRENCY * 5`; * `IMGPROXY_TTL` — duration in seconds sent in `Expires` and `Cache-Control: max-age` headers. Default: `3600` (1 hour); +* `IMGPROXY_LOCAL_FILESYSTEM_ROOT` — root directory path for serving images from local filesystem via `local:///image.ext`. Default: disabled #### Security diff --git a/config.go b/config.go index c6750234..8cfa8e69 100644 --- a/config.go +++ b/config.go @@ -85,6 +85,8 @@ type config struct { Salt []byte Secret string + + LocalFileSystemRoot string } var conf = config{ @@ -98,6 +100,7 @@ var conf = config{ MaxSrcResolution: 16800000, Quality: 80, GZipCompression: 5, + LocalFileSystemRoot: "", } func init() { @@ -132,6 +135,8 @@ func init() { strEnvConfig(&conf.Secret, "IMGPROXY_SECRET") + strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT") + if len(conf.Key) == 0 { log.Fatalln("Key is not defined") } @@ -187,6 +192,20 @@ func init() { log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression) } + if conf.LocalFileSystemRoot != "" { + stat, err := os.Stat(conf.LocalFileSystemRoot) + if err != nil { + log.Fatalf("Cannot use local directory: %s", err) + } else { + if !stat.IsDir() { + log.Fatalf("Cannot use local directory: not a directory") + } + } + if conf.LocalFileSystemRoot == "/" { + log.Print("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe") + } + } + initVips() initDownloading() } diff --git a/download.go b/download.go index 1c12e6e7..e6463aa1 100644 --- a/download.go +++ b/download.go @@ -56,8 +56,13 @@ func (r *netReader) GrowBuf(s int) { } func initDownloading() { + transport := &http.Transport{} + if conf.LocalFileSystemRoot != "" { + transport.RegisterProtocol("local", http.NewFileTransport(http.Dir(conf.LocalFileSystemRoot))) + } downloadClient = &http.Client{ Timeout: time.Duration(conf.DownloadTimeout) * time.Second, + Transport: transport, } }