From b5d91cea272d6476cf20170e6bd30a33f3fb578a Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Fri, 11 Feb 2022 14:21:18 +0100 Subject: [PATCH 1/4] 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 --- controllers.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/controllers.go b/controllers.go index 7efd917a..71c70492 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" { + meta, err := bimg.Metadata(image.Body) + if err == nil { + w.Header().Set("X-Image-Width", strconv.Itoa(meta.Size.Width)) + w.Header().Set("X-Image-Height", strconv.Itoa(meta.Size.Height)) + } + } if vary != "" { w.Header().Set("Vary", vary) } From 270eeeb6f1eda961a01477d06748ee52d42f25c3 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 22 Feb 2022 11:47:39 +0100 Subject: [PATCH 2/4] Make mimetype support always return true --- type.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/type.go b/type.go index 6230ca8a..98fa182c 100644 --- a/type.go +++ b/type.go @@ -27,7 +27,8 @@ func IsImageMimeTypeSupported(mime string) bool { format = "svg" } - return bimg.IsTypeNameSupported(format) + // Tmp hack to see if this fix an issue + return true // bimg.IsTypeNameSupported(format) } // ImageType returns the image type based on the given image type alias. From 9e4c26e1ccd454263e558f3590e328dece5bbaf6 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Mon, 14 Mar 2022 16:31:05 +0100 Subject: [PATCH 3/4] Add command line option to enable this feature Signed-off-by: Carl Schwan --- controllers.go | 2 +- imaginary.go | 3 +++ server.go | 1 + type.go | 3 +-- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/controllers.go b/controllers.go index 71c70492..5b933aae 100644 --- a/controllers.go +++ b/controllers.go @@ -133,7 +133,7 @@ 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" { + if image.Mime != "application/json" && o.ReturnSize { meta, err := bimg.Metadata(image.Body) if err == nil { w.Header().Set("X-Image-Width", strconv.Itoa(meta.Size.Width)) diff --git a/imaginary.go b/imaginary.go index 75ecec56..339cd004 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 e99c1f53..c56bf149 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. diff --git a/type.go b/type.go index 98fa182c..6230ca8a 100644 --- a/type.go +++ b/type.go @@ -27,8 +27,7 @@ func IsImageMimeTypeSupported(mime string) bool { format = "svg" } - // Tmp hack to see if this fix an issue - return true // bimg.IsTypeNameSupported(format) + return bimg.IsTypeNameSupported(format) } // ImageType returns the image type based on the given image type alias. From 234d1fecb7e0bc49f51f6f2152ba77c1d1388fa6 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 30 Mar 2022 13:03:26 +0200 Subject: [PATCH 4/4] refactor: remove deprecated X- prefix in response headers --- controllers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers.go b/controllers.go index 5b933aae..98825bfa 100644 --- a/controllers.go +++ b/controllers.go @@ -136,8 +136,8 @@ func imageHandler(w http.ResponseWriter, r *http.Request, buf []byte, operation if image.Mime != "application/json" && o.ReturnSize { meta, err := bimg.Metadata(image.Body) if err == nil { - w.Header().Set("X-Image-Width", strconv.Itoa(meta.Size.Width)) - w.Header().Set("X-Image-Height", strconv.Itoa(meta.Size.Height)) + w.Header().Set("Image-Width", strconv.Itoa(meta.Size.Width)) + w.Header().Set("Image-Height", strconv.Itoa(meta.Size.Height)) } } if vary != "" {