From af434077ef3a971494fb0bc69ec9617c3b365abd Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 14:30:09 -0600 Subject: Implement image macro for rendering images --- srv/src/http/assets.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'srv/src/http/assets.go') diff --git a/srv/src/http/assets.go b/srv/src/http/assets.go index f782c69..aacef96 100644 --- a/srv/src/http/assets.go +++ b/srv/src/http/assets.go @@ -17,6 +17,15 @@ import ( "golang.org/x/image/draw" ) +func isImgResizable(id string) bool { + switch strings.ToLower(filepath.Ext(id)) { + case ".jpg", ".jpeg", ".png": + return true + default: + return false + } +} + func resizeImage(out io.Writer, in io.Reader, maxWidth float64) error { img, format, err := image.Decode(in) @@ -123,24 +132,21 @@ func (a *api) getPostAssetHandler() http.Handler { return } - switch ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(id), ".")); ext { - case "jpg", "jpeg", "png": - - if err := resizeImage(rw, buf, float64(maxWidth)); err != nil { - apiutil.InternalServerError( - rw, r, - fmt.Errorf( - "resizing image with id %q to size %d: %w", - id, maxWidth, err, - ), - ) - } - - default: - apiutil.BadRequest(rw, r, fmt.Errorf("cannot resize file with extension %q", ext)) + if !isImgResizable(id) { + apiutil.BadRequest(rw, r, fmt.Errorf("cannot resize file %q", id)) return } + if err := resizeImage(rw, buf, float64(maxWidth)); err != nil { + apiutil.InternalServerError( + rw, r, + fmt.Errorf( + "resizing image with id %q to size %d: %w", + id, maxWidth, err, + ), + ) + } + }) } -- cgit v1.2.3