[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 <build@mattermost.com>
Этот коммит содержится в:
fxnm
2024-12-05 19:16:01 +01:00
коммит произвёл GitHub
родитель bc6f162d20
Коммит 491e46d390
2 изменённых файлов: 75 добавлений и 0 удалений

Просмотреть файл

@@ -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))

Просмотреть файл

@@ -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{