MM-12007 Add max dimensions to emoji images (5.3) (#9407)
Этот коммит содержится в:
коммит произвёл
Carlos Tadeu Panato Junior
родитель
7d60bc8cf5
Коммит
fd21e53365
@@ -9,6 +9,7 @@ import (
|
|||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost-server/app"
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"github.com/mattermost/mattermost-server/model"
|
||||||
"github.com/mattermost/mattermost-server/utils"
|
"github.com/mattermost/mattermost-server/utils"
|
||||||
|
|
||||||
@@ -105,16 +106,26 @@ func TestCreateEmoji(t *testing.T) {
|
|||||||
t.Fatal("create with wrong name")
|
t.Fatal("create with wrong name")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try to create an emoji that's too wide
|
||||||
|
emoji = &model.Emoji{
|
||||||
|
CreatorId: th.BasicUser.Id,
|
||||||
|
Name: model.NewId(),
|
||||||
|
}
|
||||||
|
|
||||||
|
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, app.MaxEmojiOriginalWidth+1), "image.gif")
|
||||||
|
if resp.Error == nil {
|
||||||
|
t.Fatal("should fail - emoji is too wide")
|
||||||
|
}
|
||||||
|
|
||||||
// try to create an emoji that's too tall
|
// try to create an emoji that's too tall
|
||||||
emoji = &model.Emoji{
|
emoji = &model.Emoji{
|
||||||
CreatorId: th.BasicUser.Id,
|
CreatorId: th.BasicUser.Id,
|
||||||
Name: model.NewId(),
|
Name: model.NewId(),
|
||||||
}
|
}
|
||||||
|
|
||||||
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 1000), "image.gif")
|
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, app.MaxEmojiOriginalHeight+1, 10), "image.gif")
|
||||||
CheckNoError(t, resp)
|
if resp.Error == nil {
|
||||||
if newEmoji.Name != emoji.Name {
|
t.Fatal("should fail - emoji is too tall")
|
||||||
t.Fatal("create with wrong name")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to create an emoji that's too large
|
// try to create an emoji that's too large
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ const (
|
|||||||
MaxEmojiFileSize = 1 << 20 // 1 MB
|
MaxEmojiFileSize = 1 << 20 // 1 MB
|
||||||
MaxEmojiWidth = 128
|
MaxEmojiWidth = 128
|
||||||
MaxEmojiHeight = 128
|
MaxEmojiHeight = 128
|
||||||
|
MaxEmojiOriginalWidth = 1028
|
||||||
|
MaxEmojiOriginalHeight = 1028
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *App) CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartImageData *multipart.Form) (*model.Emoji, *model.AppError) {
|
func (a *App) CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartImageData *multipart.Form) (*model.Emoji, *model.AppError) {
|
||||||
@@ -85,6 +87,11 @@ func (a *App) UploadEmojiImage(id string, imageData *multipart.FileHeader) *mode
|
|||||||
// make sure the file is an image and is within the required dimensions
|
// make sure the file is an image and is within the required dimensions
|
||||||
if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil {
|
if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil {
|
||||||
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, "", http.StatusBadRequest)
|
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, "", http.StatusBadRequest)
|
||||||
|
} else if config.Width > MaxEmojiOriginalWidth || config.Height > MaxEmojiOriginalHeight {
|
||||||
|
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.too_large.app_error", map[string]interface{}{
|
||||||
|
"MaxWidth": MaxEmojiOriginalWidth,
|
||||||
|
"MaxHeight": MaxEmojiOriginalHeight,
|
||||||
|
}, "", http.StatusBadRequest)
|
||||||
} else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight {
|
} else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight {
|
||||||
data := buf.Bytes()
|
data := buf.Bytes()
|
||||||
newbuf := bytes.NewBuffer(nil)
|
newbuf := bytes.NewBuffer(nil)
|
||||||
|
|||||||
@@ -1008,6 +1008,10 @@
|
|||||||
"id": "api.emoji.upload.image.app_error",
|
"id": "api.emoji.upload.image.app_error",
|
||||||
"translation": "Unable to create emoji. File must be a PNG, JPEG, or GIF."
|
"translation": "Unable to create emoji. File must be a PNG, JPEG, or GIF."
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "api.emoji.upload.large_image.too_large.app_error",
|
||||||
|
"translation": "Unable to create emoji. Image must be smaller than {{.MaxWidth}} by {{.MaxHeight}}."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "api.emoji.upload.large_image.decode_error",
|
"id": "api.emoji.upload.large_image.decode_error",
|
||||||
"translation": "Unable to create emoji. An error occurred when trying to decode the image."
|
"translation": "Unable to create emoji. An error occurred when trying to decode the image."
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user