From 491e46d390343fd14dfcf377df15789f2908d887 Mon Sep 17 00:00:00 2001 From: fxnm <47592197+fxnm@users.noreply.github.com> Date: Thu, 5 Dec 2024 19:16:01 +0100 Subject: [PATCH] [MM-61477] Fixed exif rotates images width and height is not correctly calculated (#29309) * [MM-61477] Fixed exif rotates images width and height is not correctly calculated * [MM-61477] Apply rotation ony to png or jepg * [MM-61477] Added test cases to verify correct rotation * fix: fixed wrong format * uff * fix: exif rotation is only possible on jpeg images --------- Co-authored-by: Mattermost Build --- server/channels/app/post_metadata.go | 11 ++++ server/channels/app/post_metadata_test.go | 64 +++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/server/channels/app/post_metadata.go b/server/channels/app/post_metadata.go index 87713a1eca..1471e79793 100644 --- a/server/channels/app/post_metadata.go +++ b/server/channels/app/post_metadata.go @@ -24,6 +24,7 @@ import ( "github.com/mattermost/mattermost/server/public/shared/markdown" "github.com/mattermost/mattermost/server/public/shared/mlog" "github.com/mattermost/mattermost/server/public/shared/request" + "github.com/mattermost/mattermost/server/v8/channels/app/imaging" "github.com/mattermost/mattermost/server/v8/channels/app/oembed" "github.com/mattermost/mattermost/server/v8/channels/app/platform" "github.com/mattermost/mattermost/server/v8/channels/utils/imgutils" @@ -927,6 +928,16 @@ func parseImages(body io.Reader) (*model.PostImage, error) { Format: format, } + if format == "jpeg" { + if imageOrientation, err := imaging.GetImageOrientation(io.MultiReader(buf, body)); err == nil && + (imageOrientation == imaging.RotatedCWMirrored || + imageOrientation == imaging.RotatedCCW || + imageOrientation == imaging.RotatedCCWMirrored || + imageOrientation == imaging.RotatedCW) { + image.Width, image.Height = image.Height, image.Width + } + } + if format == "gif" { // Decoding the config may have read some of the image data, so re-read the data that has already been read first frameCount, err := imgutils.CountGIFFrames(io.MultiReader(buf, body)) diff --git a/server/channels/app/post_metadata_test.go b/server/channels/app/post_metadata_test.go index 8267f413c5..0cd0b5a66c 100644 --- a/server/channels/app/post_metadata_test.go +++ b/server/channels/app/post_metadata_test.go @@ -2756,6 +2756,70 @@ func TestParseImages(t *testing.T) { Format: "png", }, }, + "jpg-1": { + FileName: "orientation_test_1.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-2": { + FileName: "orientation_test_2.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-3": { + FileName: "orientation_test_3.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-4": { + FileName: "orientation_test_4.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-5": { + FileName: "orientation_test_5.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-6": { + FileName: "orientation_test_6.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-7": { + FileName: "orientation_test_7.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, + "jpg-8": { + FileName: "orientation_test_8.jpeg", + Expected: &model.PostImage{ + Width: 2860, + Height: 1578, + Format: "jpeg", + }, + }, "animated gif": { FileName: "testgif.gif", Expected: &model.PostImage{