MM-16872 - Extend Plugin API to set LHS bot icon (#11601)

* MM-16872 - Extend Plugin API to set LHS bot icon

* MM-16872 - Using ReadSeeker as opposed to Reader for reading svg image file

* MM-16872 - PR feedback

* MM-16872 - Using userId rather than bot.UserId

* MM-16872 - Minor stylistic changes

* MM-16872 - Removing DriverName check
Этот коммит содержится в:
Ali Farooq
2019-07-11 12:00:12 -04:00
коммит произвёл GitHub
родитель 76f4fccf8a
Коммит 5ed40a48c8
10 изменённых файлов: 388 добавлений и 57 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package app
import (
"fmt"
"io"
"mime/multipart"
"net/http"
@@ -197,19 +198,25 @@ func (a *App) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) {
return a.Srv.Store.Bot().Save(model.BotFromUser(user))
}
// SetBotIconImage sets LHS icon for a bot.
func (a *App) SetBotIconImage(botUserId string, imageData *multipart.FileHeader) *model.AppError {
if len(*a.Config().FileSettings.DriverName) == 0 {
return model.NewAppError("SetBotIconImage", "api.bot.icon_image.storage.app_error", nil, "", http.StatusNotImplemented)
}
// SetBotIconImageFromMultiPartFile sets LHS icon for a bot.
func (a *App) SetBotIconImageFromMultiPartFile(botUserId string, imageData *multipart.FileHeader) *model.AppError {
file, err := imageData.Open()
if err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.open.app_error", nil, err.Error(), http.StatusBadRequest)
}
defer file.Close()
if _, err = parseSVG(file); err != nil {
file.Seek(0, 0)
return a.SetBotIconImage(botUserId, file)
}
// SetBotIconImage sets LHS icon for a bot.
func (a *App) SetBotIconImage(botUserId string, file io.ReadSeeker) *model.AppError {
if _, err := a.GetBot(botUserId, true); err != nil {
return err
}
if _, err := parseSVG(file); err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.parse.app_error", nil, err.Error(), http.StatusBadRequest)
}
@@ -229,8 +236,8 @@ func (a *App) SetBotIconImage(botUserId string, imageData *multipart.FileHeader)
// DeleteBotIconImage deletes LHS icon for a bot.
func (a *App) DeleteBotIconImage(botUserId string) *model.AppError {
if len(*a.Config().FileSettings.DriverName) == 0 {
return model.NewAppError("DeleteBotIconImage", "api.bot.icon_image.storage.app_error", nil, "", http.StatusNotImplemented)
if _, err := a.GetBot(botUserId, true); err != nil {
return err
}
// Delete icon
@@ -247,17 +254,17 @@ func (a *App) DeleteBotIconImage(botUserId string) *model.AppError {
}
// GetBotIconImage retrieves LHS icon for a bot.
func (a *App) GetBotIconImage(botUserId string) ([]byte, bool, *model.AppError) {
if len(*a.Config().FileSettings.DriverName) == 0 {
return nil, false, model.NewAppError("GetBotIconImage", "api.bot.icon_image.storage.app_error", nil, "", http.StatusNotImplemented)
func (a *App) GetBotIconImage(botUserId string) ([]byte, *model.AppError) {
if _, err := a.GetBot(botUserId, true); err != nil {
return nil, err
}
data, err := a.ReadFile(getBotIconPath(botUserId))
if err != nil {
return nil, false, model.NewAppError("GetBotIconImage", "api.bot.get_bot_icon_image.read.app_error", nil, err.Error(), http.StatusNotFound)
return nil, model.NewAppError("GetBotIconImage", "api.bot.get_bot_icon_image.read.app_error", nil, err.Error(), http.StatusNotFound)
}
return data, false, nil
return data, nil
}
func getBotIconPath(botUserId string) string {