Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that if method=scale the returned thumbnail is no bytesize-larger than the original image #288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/r0/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down Expand Up @@ -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,
Expand Down