From f7e16450a214aa3f5db5b864899b67b39fb84a87 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 8 Jun 2017 13:07:41 +0200 Subject: [PATCH] mediaapi/thumbnailer: Check if request is larger than original --- .../mediaapi/thumbnailer/thumbnailer_bimg.go | 13 +++++++++++++ .../mediaapi/thumbnailer/thumbnailer_nfnt.go | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_bimg.go b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_bimg.go index d8400a641..41edce7cb 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_bimg.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_bimg.go @@ -81,6 +81,11 @@ func createThumbnail(src types.Path, img *bimg.Image, config types.ThumbnailSize "ResizeMethod": config.ResizeMethod, }) + // Check if request is larger than original + if isLargerThanOriginal(config, img) { + return false, nil + } + dst := GetThumbnailPath(src, config) // Note: getActiveThumbnailGeneration uses mutexes and conditions from activeThumbnailGeneration @@ -153,6 +158,14 @@ func createThumbnail(src types.Path, img *bimg.Image, config types.ThumbnailSize return false, nil } +func isLargerThanOriginal(config types.ThumbnailSize, img *bimg.Image) bool { + imgSize, err := img.Size() + if err == nil && config.Width >= imgSize.Width && config.Height >= imgSize.Height { + return true + } + return false +} + // resize scales an image to fit within the provided width and height // If the source aspect ratio is different to the target dimensions, one edge will be smaller than requested // If crop is set to true, the image will be scaled to fill the width and height with any excess being cropped off diff --git a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go index 476e1cb35..0b75ea52a 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go @@ -113,6 +113,11 @@ func createThumbnail(src types.Path, img image.Image, config types.ThumbnailSize "ResizeMethod": config.ResizeMethod, }) + // Check if request is larger than original + if config.Width >= img.Bounds().Dx() && config.Height >= img.Bounds().Dy() { + return false, nil + } + dst := GetThumbnailPath(src, config) // Note: getActiveThumbnailGeneration uses mutexes and conditions from activeThumbnailGeneration