From 0f8238594e7179d8fdf83367e21821e87f534e07 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 17 Nov 2020 12:33:42 +0100 Subject: [PATCH] Ensure that if method=scale the returned thumbnail is no bytesize-larger than the original image --- api/r0/thumbnail.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/api/r0/thumbnail.go b/api/r0/thumbnail.go index 779cf635..f8fe7bf7 100644 --- a/api/r0/thumbnail.go +++ b/api/r0/thumbnail.go @@ -10,6 +10,7 @@ import ( "github.com/turt2live/matrix-media-repo/common" "github.com/turt2live/matrix-media-repo/common/rcontext" "github.com/turt2live/matrix-media-repo/controllers/thumbnail_controller" + "github.com/turt2live/matrix-media-repo/controllers/download_controller" ) func ThumbnailMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInfo) interface{} { @@ -94,6 +95,20 @@ func ThumbnailMedia(r *http.Request, rctx rcontext.RequestContext, user api.User return api.InternalServerError("Unexpected Error") } + if method == "scale" { + // now we fetch the original file to check if that one is smaller + streamedMedia, err := download_controller.GetMedia(server, mediaId, downloadRemote, false, rctx) + if err == nil && streamedThumbnail.Thumbnail.SizeBytes > streamedMedia.SizeBytes { + // the media is smaller than the thumbnail, return that instead + return &DownloadMediaResponse{ + ContentType: streamedMedia.ContentType, + Filename: streamedMedia.UploadName, + SizeBytes: streamedMedia.SizeBytes, + Data: streamedMedia.Stream, + } + } + } + return &DownloadMediaResponse{ ContentType: streamedThumbnail.Thumbnail.ContentType, SizeBytes: streamedThumbnail.Thumbnail.SizeBytes,