MM-14030 Add format and frame count to image metadata (#10757)
* MM-14030 Add initial CountFrames * MM-14030 Add format and frame count to image metadata * Fix tests and stop using image dimensions from OpenGraph * Fix copyright header * Move license to NOTICE.txt
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
9a9d5d4081
Коммит
e50b642e43
80
utils/imgutils/gif_test.go
Обычный файл
80
utils/imgutils/gif_test.go
Обычный файл
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package imgutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/utils/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCountFrames(t *testing.T) {
|
||||
header := []byte{
|
||||
'G', 'I', 'F', '8', '9', 'a', // header
|
||||
1, 0, 1, 0, // width and height of 1 by 1
|
||||
128, 0, 0, // other header information
|
||||
0, 0, 0, 1, 1, 1, // color table
|
||||
}
|
||||
frame := []byte{
|
||||
0x2c, // block introducer
|
||||
0, 0, 0, 0, 1, 0, 1, 0, // position and dimensions of the frame
|
||||
0, // other frame information
|
||||
0x2, 0x2, 0x4c, 0x1, 0, // encoded pixel data
|
||||
}
|
||||
trailer := []byte{0x3b}
|
||||
|
||||
t.Run("should count the frames of a static gif", func(t *testing.T) {
|
||||
var b []byte
|
||||
b = append(b, header...)
|
||||
b = append(b, frame...)
|
||||
b = append(b, trailer...)
|
||||
|
||||
count, err := CountFrames(bytes.NewReader(b))
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, count)
|
||||
})
|
||||
|
||||
t.Run("should count the frames of an animated gif", func(t *testing.T) {
|
||||
var b []byte
|
||||
b = append(b, header...)
|
||||
for i := 0; i < 100; i++ {
|
||||
b = append(b, frame...)
|
||||
}
|
||||
b = append(b, trailer...)
|
||||
|
||||
count, err := CountFrames(bytes.NewReader(b))
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 100, count)
|
||||
})
|
||||
|
||||
t.Run("should count the frames of an actual animated gif", func(t *testing.T) {
|
||||
b, err := testutils.ReadTestFile("testgif.gif")
|
||||
require.Nil(t, err)
|
||||
|
||||
count, err := CountFrames(bytes.NewReader(b))
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 4, count)
|
||||
})
|
||||
|
||||
t.Run("should return an error for a non-gif image", func(t *testing.T) {
|
||||
b, err := testutils.ReadTestFile("test.png")
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = CountFrames(bytes.NewReader(b))
|
||||
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
|
||||
t.Run("should return an error for garbage data", func(t *testing.T) {
|
||||
_, err := CountFrames(bytes.NewReader([]byte("garbage data")))
|
||||
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
}
|
||||
Ссылка в новой задаче
Block a user