From cfbf8d724cd326e835dfcb01e7224397c46037d3 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 30 Mar 2022 13:03:44 +0200 Subject: [PATCH] Return with and heigh of the generated images (#382) * Return with and heigh of the generated images Use case: When using the fit image transformation, it is helpful to know the size of the resulting image without having to either read the image locally or do another request to the info endpoint. Used in: https://github.com/nextcloud/server/pull/24166 Signed-off-by: Carl Schwan * Make mimetype support always return true * Add command line option to enable this feature Signed-off-by: Carl Schwan * refactor: remove deprecated X- prefix in response headers Co-authored-by: Tom --- controllers.go | 7 +++++++ imaginary.go | 3 +++ server.go | 1 + 3 files changed, 11 insertions(+) diff --git a/controllers.go b/controllers.go index 7efd917..98825bf 100644 --- a/controllers.go +++ b/controllers.go @@ -133,6 +133,13 @@ func imageHandler(w http.ResponseWriter, r *http.Request, buf []byte, operation // Expose Content-Length response header w.Header().Set("Content-Length", strconv.Itoa(len(image.Body))) w.Header().Set("Content-Type", image.Mime) + if image.Mime != "application/json" && o.ReturnSize { + meta, err := bimg.Metadata(image.Body) + if err == nil { + w.Header().Set("Image-Width", strconv.Itoa(meta.Size.Width)) + w.Header().Set("Image-Height", strconv.Itoa(meta.Size.Height)) + } + } if vary != "" { w.Header().Set("Vary", vary) } diff --git a/imaginary.go b/imaginary.go index 75ecec5..339cd00 100644 --- a/imaginary.go +++ b/imaginary.go @@ -50,6 +50,7 @@ var ( aMRelease = flag.Int("mrelease", 30, "OS memory release interval in seconds") aCpus = flag.Int("cpus", runtime.GOMAXPROCS(-1), "Number of cpu cores to use") aLogLevel = flag.String("log-level", "info", "Define log level for http-server. E.g: info,warning,error") + aReturnSize = flag.Bool("return-size", false, "Return the image size in the HTTP headers") ) const usage = `imaginary %s @@ -106,6 +107,7 @@ Options: (default for current machine is %d cores) -log-level Set log level for http-server. E.g: info,warning,error [default: info]. Or can use the environment variable GOLANG_LOG=info. + -return-size Return the image size with X-Width and X-Height HTTP header. [default: disabled]. ` type URLSignature struct { @@ -157,6 +159,7 @@ func main() { AllowedOrigins: parseOrigins(*aAllowedOrigins), MaxAllowedSize: *aMaxAllowedSize, LogLevel: getLogLevel(*aLogLevel), + ReturnSize: *aReturnSize, } // Show warning if gzip flag is passed diff --git a/server.go b/server.go index e99c1f5..c56bf14 100644 --- a/server.go +++ b/server.go @@ -43,6 +43,7 @@ type ServerOptions struct { Endpoints Endpoints AllowedOrigins []*url.URL LogLevel string + ReturnSize bool } // Endpoints represents a list of endpoint names to disable.