github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/app/emoji.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "bytes" 8 "fmt" 9 "image" 10 "image/draw" 11 "image/gif" 12 _ "image/jpeg" 13 "image/png" 14 "io" 15 "mime/multipart" 16 "net/http" 17 18 "image/color/palette" 19 20 "github.com/disintegration/imaging" 21 "github.com/mattermost/mattermost-server/mlog" 22 "github.com/mattermost/mattermost-server/model" 23 ) 24 25 const ( 26 MaxEmojiFileSize = 1 << 20 // 1 MB 27 MaxEmojiWidth = 128 28 MaxEmojiHeight = 128 29 ) 30 31 func (a *App) CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartImageData *multipart.Form) (*model.Emoji, *model.AppError) { 32 // wipe the emoji id so that existing emojis can't get overwritten 33 emoji.Id = "" 34 35 // do our best to validate the emoji before committing anything to the DB so that we don't have to clean up 36 // orphaned files left over when validation fails later on 37 emoji.PreSave() 38 if err := emoji.IsValid(); err != nil { 39 return nil, err 40 } 41 42 if emoji.CreatorId != sessionUserId { 43 return nil, model.NewAppError("createEmoji", "api.emoji.create.other_user.app_error", nil, "", http.StatusForbidden) 44 } 45 46 if result := <-a.Srv.Store.Emoji().GetByName(emoji.Name); result.Err == nil && result.Data != nil { 47 return nil, model.NewAppError("createEmoji", "api.emoji.create.duplicate.app_error", nil, "", http.StatusBadRequest) 48 } 49 50 if imageData := multiPartImageData.File["image"]; len(imageData) == 0 { 51 err := model.NewAppError("Context", "api.context.invalid_body_param.app_error", map[string]interface{}{"Name": "createEmoji"}, "", http.StatusBadRequest) 52 return nil, err 53 } else if err := a.UploadEmojiImage(emoji.Id, imageData[0]); err != nil { 54 return nil, err 55 } 56 57 if result := <-a.Srv.Store.Emoji().Save(emoji); result.Err != nil { 58 return nil, result.Err 59 } else { 60 message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_EMOJI_ADDED, "", "", "", nil) 61 message.Add("emoji", emoji.ToJson()) 62 a.Publish(message) 63 return result.Data.(*model.Emoji), nil 64 } 65 } 66 67 func (a *App) GetEmojiList(page, perPage int, sort string) ([]*model.Emoji, *model.AppError) { 68 if result := <-a.Srv.Store.Emoji().GetList(page*perPage, perPage, sort); result.Err != nil { 69 return nil, result.Err 70 } else { 71 return result.Data.([]*model.Emoji), nil 72 } 73 } 74 75 func (a *App) UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError { 76 file, err := imageData.Open() 77 if err != nil { 78 return model.NewAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "", http.StatusBadRequest) 79 } 80 defer file.Close() 81 82 buf := bytes.NewBuffer(nil) 83 io.Copy(buf, file) 84 85 // make sure the file is an image and is within the required dimensions 86 if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil { 87 return model.NewAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, "", http.StatusBadRequest) 88 } else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight { 89 data := buf.Bytes() 90 newbuf := bytes.NewBuffer(nil) 91 if info, err := model.GetInfoForBytes(imageData.Filename, data); err != nil { 92 return err 93 } else if info.MimeType == "image/gif" { 94 if gif_data, err := gif.DecodeAll(bytes.NewReader(data)); err != nil { 95 return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_decode_error", nil, "", http.StatusBadRequest) 96 } else { 97 resized_gif := resizeEmojiGif(gif_data) 98 if err := gif.EncodeAll(newbuf, resized_gif); err != nil { 99 return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "", http.StatusBadRequest) 100 } 101 if _, err := a.WriteFile(newbuf, getEmojiImagePath(id)); err != nil { 102 return err 103 } 104 } 105 } else { 106 if img, _, err := image.Decode(bytes.NewReader(data)); err != nil { 107 return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.decode_error", nil, "", http.StatusBadRequest) 108 } else { 109 resized_image := resizeEmoji(img, config.Width, config.Height) 110 if err := png.Encode(newbuf, resized_image); err != nil { 111 return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "", http.StatusBadRequest) 112 } 113 if _, err := a.WriteFile(newbuf, getEmojiImagePath(id)); err != nil { 114 return err 115 } 116 } 117 } 118 } 119 120 _, appErr := a.WriteFile(buf, getEmojiImagePath(id)) 121 return appErr 122 } 123 124 func (a *App) DeleteEmoji(emoji *model.Emoji) *model.AppError { 125 if err := (<-a.Srv.Store.Emoji().Delete(emoji.Id, model.GetMillis())).Err; err != nil { 126 return err 127 } 128 129 a.deleteEmojiImage(emoji.Id) 130 a.deleteReactionsForEmoji(emoji.Name) 131 return nil 132 } 133 134 func (a *App) GetEmoji(emojiId string) (*model.Emoji, *model.AppError) { 135 if !*a.Config().ServiceSettings.EnableCustomEmoji { 136 return nil, model.NewAppError("GetEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented) 137 } 138 139 if len(*a.Config().FileSettings.DriverName) == 0 { 140 return nil, model.NewAppError("GetEmoji", "api.emoji.storage.app_error", nil, "", http.StatusNotImplemented) 141 } 142 143 if result := <-a.Srv.Store.Emoji().Get(emojiId, false); result.Err != nil { 144 return nil, result.Err 145 } else { 146 return result.Data.(*model.Emoji), nil 147 } 148 } 149 150 func (a *App) GetEmojiByName(emojiName string) (*model.Emoji, *model.AppError) { 151 if !*a.Config().ServiceSettings.EnableCustomEmoji { 152 return nil, model.NewAppError("GetEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented) 153 } 154 155 if len(*a.Config().FileSettings.DriverName) == 0 { 156 return nil, model.NewAppError("GetEmoji", "api.emoji.storage.app_error", nil, "", http.StatusNotImplemented) 157 } 158 159 if result := <-a.Srv.Store.Emoji().GetByName(emojiName); result.Err != nil { 160 return nil, result.Err 161 } else { 162 return result.Data.(*model.Emoji), nil 163 } 164 } 165 166 func (a *App) GetEmojiImage(emojiId string) (imageByte []byte, imageType string, err *model.AppError) { 167 if result := <-a.Srv.Store.Emoji().Get(emojiId, true); result.Err != nil { 168 return nil, "", result.Err 169 } else { 170 var img []byte 171 172 if data, err := a.ReadFile(getEmojiImagePath(emojiId)); err != nil { 173 return nil, "", model.NewAppError("getEmojiImage", "api.emoji.get_image.read.app_error", nil, err.Error(), http.StatusNotFound) 174 } else { 175 img = data 176 } 177 178 _, imageType, err := image.DecodeConfig(bytes.NewReader(img)) 179 if err != nil { 180 return nil, "", model.NewAppError("getEmojiImage", "api.emoji.get_image.decode.app_error", nil, err.Error(), http.StatusInternalServerError) 181 } 182 183 return img, imageType, nil 184 } 185 } 186 187 func (a *App) SearchEmoji(name string, prefixOnly bool, limit int) ([]*model.Emoji, *model.AppError) { 188 if !*a.Config().ServiceSettings.EnableCustomEmoji { 189 return nil, model.NewAppError("SearchEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented) 190 } 191 192 if result := <-a.Srv.Store.Emoji().Search(name, prefixOnly, limit); result.Err != nil { 193 return nil, result.Err 194 } else { 195 return result.Data.([]*model.Emoji), nil 196 } 197 } 198 199 func resizeEmojiGif(gifImg *gif.GIF) *gif.GIF { 200 // Create a new RGBA image to hold the incremental frames. 201 firstFrame := gifImg.Image[0].Bounds() 202 b := image.Rect(0, 0, firstFrame.Dx(), firstFrame.Dy()) 203 img := image.NewRGBA(b) 204 205 resizedImage := image.Image(nil) 206 // Resize each frame. 207 for index, frame := range gifImg.Image { 208 bounds := frame.Bounds() 209 draw.Draw(img, bounds, frame, bounds.Min, draw.Over) 210 resizedImage = resizeEmoji(img, firstFrame.Dx(), firstFrame.Dy()) 211 gifImg.Image[index] = imageToPaletted(resizedImage) 212 } 213 // Set new gif width and height 214 gifImg.Config.Width = resizedImage.Bounds().Dx() 215 gifImg.Config.Height = resizedImage.Bounds().Dy() 216 return gifImg 217 } 218 219 func getEmojiImagePath(id string) string { 220 return "emoji/" + id + "/image" 221 } 222 223 func resizeEmoji(img image.Image, width int, height int) image.Image { 224 emojiWidth := float64(width) 225 emojiHeight := float64(height) 226 227 var emoji image.Image 228 if emojiHeight <= MaxEmojiHeight && emojiWidth <= MaxEmojiWidth { 229 emoji = img 230 } else { 231 emoji = imaging.Fit(img, MaxEmojiWidth, MaxEmojiHeight, imaging.Lanczos) 232 } 233 return emoji 234 } 235 236 func imageToPaletted(img image.Image) *image.Paletted { 237 b := img.Bounds() 238 pm := image.NewPaletted(b, palette.Plan9) 239 draw.FloydSteinberg.Draw(pm, b, img, image.ZP) 240 return pm 241 } 242 243 func (a *App) deleteEmojiImage(id string) { 244 if err := a.MoveFile(getEmojiImagePath(id), "emoji/"+id+"/image_deleted"); err != nil { 245 mlog.Error(fmt.Sprintf("Failed to rename image when deleting emoji %v", id)) 246 } 247 } 248 249 func (a *App) deleteReactionsForEmoji(emojiName string) { 250 if result := <-a.Srv.Store.Reaction().DeleteAllWithEmojiName(emojiName); result.Err != nil { 251 mlog.Warn(fmt.Sprintf("Unable to delete reactions when deleting emoji with emoji name %v", emojiName)) 252 mlog.Warn(fmt.Sprint(result.Err)) 253 } 254 }