[MM-29174] fix: enable automatic emoji resize feature (#15657)
* fix: enable automatic emoji resize feature
* refactor writes by overriding existing buffer, write once, return once
* add testcases
* 🤔
* close file
* automatic formatting with goimports
* ensure image is resized keeping proportions
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ff42c97ab7
Коммит
047c76b55f
@@ -7,8 +7,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"image"
|
"image"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost-server/v5/utils/fileutils"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@@ -33,6 +37,22 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
th.RestoreDefaultRolePermissions(defaultRolePermissions)
|
th.RestoreDefaultRolePermissions(defaultRolePermissions)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// constants to be used along with checkEmojiFile
|
||||||
|
emojiWidth := app.MaxEmojiWidth
|
||||||
|
emojiHeight := app.MaxEmojiHeight * 2
|
||||||
|
// check that emoji gets resized correctly, respecting proportions, and is of expected type
|
||||||
|
checkEmojiFile := func(id, expectedImageType string) {
|
||||||
|
path, _ := fileutils.FindDir("data")
|
||||||
|
file, fileErr := os.Open(filepath.Join(path, "/emoji/"+id+"/image"))
|
||||||
|
require.NoError(t, fileErr)
|
||||||
|
defer file.Close()
|
||||||
|
config, imageType, err := image.DecodeConfig(file)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, expectedImageType, imageType)
|
||||||
|
require.Equal(t, emojiWidth/2, config.Width)
|
||||||
|
require.Equal(t, emojiHeight/2, config.Height)
|
||||||
|
}
|
||||||
|
|
||||||
emoji := &model.Emoji{
|
emoji := &model.Emoji{
|
||||||
CreatorId: th.BasicUser.Id,
|
CreatorId: th.BasicUser.Id,
|
||||||
Name: model.NewId(),
|
Name: model.NewId(),
|
||||||
@@ -42,11 +62,14 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
_, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
_, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
||||||
CheckNotImplementedStatus(t, resp)
|
CheckNotImplementedStatus(t, resp)
|
||||||
|
|
||||||
|
// enable emoji creation for next cases
|
||||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
|
||||||
|
|
||||||
// try to create a valid gif emoji when they're enabled
|
// try to create a valid gif emoji when they're enabled
|
||||||
newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
|
newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, emojiWidth, emojiHeight), "image.gif")
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
||||||
|
checkEmojiFile(newEmoji.Id, "gif")
|
||||||
|
|
||||||
// try to create an emoji with a duplicate name
|
// try to create an emoji with a duplicate name
|
||||||
emoji2 := &model.Emoji{
|
emoji2 := &model.Emoji{
|
||||||
@@ -63,9 +86,10 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
Name: model.NewId(),
|
Name: model.NewId(),
|
||||||
}
|
}
|
||||||
|
|
||||||
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 10, 10, 10), "image.gif")
|
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, emojiWidth, emojiHeight, 10), "image.gif")
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
||||||
|
checkEmojiFile(newEmoji.Id, "gif")
|
||||||
|
|
||||||
// try to create a valid jpeg emoji
|
// try to create a valid jpeg emoji
|
||||||
emoji = &model.Emoji{
|
emoji = &model.Emoji{
|
||||||
@@ -73,9 +97,10 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
Name: model.NewId(),
|
Name: model.NewId(),
|
||||||
}
|
}
|
||||||
|
|
||||||
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestJpeg(t, 10, 10), "image.gif")
|
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestJpeg(t, emojiWidth, emojiHeight), "image.jpeg")
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
||||||
|
checkEmojiFile(newEmoji.Id, "png") // emoji must be converted from jpeg to png
|
||||||
|
|
||||||
// try to create a valid png emoji
|
// try to create a valid png emoji
|
||||||
emoji = &model.Emoji{
|
emoji = &model.Emoji{
|
||||||
@@ -83,9 +108,10 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
Name: model.NewId(),
|
Name: model.NewId(),
|
||||||
}
|
}
|
||||||
|
|
||||||
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestPng(t, 10, 10), "image.gif")
|
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestPng(t, emojiWidth, emojiHeight), "image.png")
|
||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
|
||||||
|
checkEmojiFile(newEmoji.Id, "png")
|
||||||
|
|
||||||
// try to create an emoji that's too wide
|
// try to create an emoji that's too wide
|
||||||
emoji = &model.Emoji{
|
emoji = &model.Emoji{
|
||||||
|
|||||||
@@ -140,9 +140,7 @@ func (a *App) UploadEmojiImage(id string, imageData *multipart.FileHeader) *mode
|
|||||||
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "", http.StatusBadRequest)
|
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := a.WriteFile(newbuf, getEmojiImagePath(id)); err != nil {
|
buf = newbuf
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
img, _, err := image.Decode(bytes.NewReader(data))
|
img, _, err := image.Decode(bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -153,9 +151,7 @@ func (a *App) UploadEmojiImage(id string, imageData *multipart.FileHeader) *mode
|
|||||||
if err := png.Encode(newbuf, resized_image); err != nil {
|
if err := png.Encode(newbuf, resized_image); err != nil {
|
||||||
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "", http.StatusBadRequest)
|
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
if _, err := a.WriteFile(newbuf, getEmojiImagePath(id)); err != nil {
|
buf = newbuf
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user