github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	"image"
     9  	"image/draw"
    10  	"image/gif"
    11  	_ "image/jpeg"
    12  	"image/png"
    13  	"io"
    14  	"mime/multipart"
    15  	"net/http"
    16  
    17  	l4g "github.com/alecthomas/log4go"
    18  
    19  	"image/color/palette"
    20  
    21  	"github.com/disintegration/imaging"
    22  	"github.com/mattermost/mattermost-server/model"
    23  	"github.com/mattermost/mattermost-server/utils"
    24  )
    25  
    26  const (
    27  	MaxEmojiFileSize = 1000 * 1024 // 1 MB
    28  	MaxEmojiWidth    = 128
    29  	MaxEmojiHeight   = 128
    30  )
    31  
    32  func (a *App) CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartImageData *multipart.Form) (*model.Emoji, *model.AppError) {
    33  	// wipe the emoji id so that existing emojis can't get overwritten
    34  	emoji.Id = ""
    35  
    36  	// do our best to validate the emoji before committing anything to the DB so that we don't have to clean up
    37  	// orphaned files left over when validation fails later on
    38  	emoji.PreSave()
    39  	if err := emoji.IsValid(); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	if emoji.CreatorId != sessionUserId {
    44  		return nil, model.NewAppError("createEmoji", "api.emoji.create.other_user.app_error", nil, "", http.StatusForbidden)
    45  	}
    46  
    47  	if result := <-a.Srv.Store.Emoji().GetByName(emoji.Name); result.Err == nil && result.Data != nil {
    48  		return nil, model.NewAppError("createEmoji", "api.emoji.create.duplicate.app_error", nil, "", http.StatusBadRequest)
    49  	}
    50  
    51  	if imageData := multiPartImageData.File["image"]; len(imageData) == 0 {
    52  		err := model.NewAppError("Context", "api.context.invalid_body_param.app_error", map[string]interface{}{"Name": "createEmoji"}, "", http.StatusBadRequest)
    53  		return nil, err
    54  	} else if err := a.UploadEmojiImage(emoji.Id, imageData[0]); err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	if result := <-a.Srv.Store.Emoji().Save(emoji); result.Err != nil {
    59  		return nil, result.Err
    60  	} else {
    61  		message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_EMOJI_ADDED, "", "", "", nil)
    62  		message.Add("emoji", emoji.ToJson())
    63  		a.Publish(message)
    64  		return result.Data.(*model.Emoji), nil
    65  	}
    66  }
    67  
    68  func (a *App) GetEmojiList(page, perPage int, sort string) ([]*model.Emoji, *model.AppError) {
    69  	if result := <-a.Srv.Store.Emoji().GetList(page*perPage, perPage, sort); result.Err != nil {
    70  		return nil, result.Err
    71  	} else {
    72  		return result.Data.([]*model.Emoji), nil
    73  	}
    74  }
    75  
    76  func (a *App) UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError {
    77  	file, err := imageData.Open()
    78  	if err != nil {
    79  		return model.NewAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "", http.StatusBadRequest)
    80  	}
    81  	defer file.Close()
    82  
    83  	buf := bytes.NewBuffer(nil)
    84  	io.Copy(buf, file)
    85  
    86  	// make sure the file is an image and is within the required dimensions
    87  	if config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())); err != nil {
    88  		return model.NewAppError("uploadEmojiImage", "api.emoji.upload.image.app_error", nil, "", http.StatusBadRequest)
    89  	} else if config.Width > MaxEmojiWidth || config.Height > MaxEmojiHeight {
    90  		data := buf.Bytes()
    91  		newbuf := bytes.NewBuffer(nil)
    92  		if info, err := model.GetInfoForBytes(imageData.Filename, data); err != nil {
    93  			return err
    94  		} else if info.MimeType == "image/gif" {
    95  			if gif_data, err := gif.DecodeAll(bytes.NewReader(data)); err != nil {
    96  				return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_decode_error", nil, "", http.StatusBadRequest)
    97  			} else {
    98  				resized_gif := resizeEmojiGif(gif_data)
    99  				if err := gif.EncodeAll(newbuf, resized_gif); err != nil {
   100  					return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "", http.StatusBadRequest)
   101  				}
   102  				if err := a.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil {
   103  					return err
   104  				}
   105  			}
   106  		} else {
   107  			if img, _, err := image.Decode(bytes.NewReader(data)); err != nil {
   108  				return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.decode_error", nil, "", http.StatusBadRequest)
   109  			} else {
   110  				resized_image := resizeEmoji(img, config.Width, config.Height)
   111  				if err := png.Encode(newbuf, resized_image); err != nil {
   112  					return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "", http.StatusBadRequest)
   113  				}
   114  				if err := a.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil {
   115  					return err
   116  				}
   117  			}
   118  		}
   119  	}
   120  
   121  	return a.WriteFile(buf.Bytes(), getEmojiImagePath(id))
   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  		l4g.Error("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  		l4g.Warn(utils.T("api.emoji.delete.delete_reactions.app_error"), emojiName)
   252  		l4g.Warn(result.Err)
   253  	}
   254  }