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 <carl@carlschwan.eu>

* Make mimetype support always return true

* Add command line option to enable this feature

Signed-off-by: Carl Schwan <carl@carlschwan.eu>

* refactor: remove deprecated X- prefix in response headers

Co-authored-by: Tom <tomas@aparicio.me>
This commit is contained in:
Carl Schwan
2022-03-30 13:03:44 +02:00
committed by GitHub
parent 00f696fe3a
commit cfbf8d724c
3 changed files with 11 additions and 0 deletions
+7
View File
@@ -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)
}
+3
View File
@@ -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
+1
View File
@@ -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.