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