[MM-36318] Set bot profile image and icon only if they have changed (#18008)

* Set bot profile image and icon only if they have changed

* Move equality check to the app layer

* Use ioutil.ReadAll

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Claudio Costa
2021-08-11 09:37:52 +02:00
коммит произвёл GitHub
родитель 2982cc6f4e
Коммит 868b8d91db
5 изменённых файлов: 42 добавлений и 12 удалений

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

@@ -4,12 +4,15 @@
package app
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"path/filepath"
"github.com/mattermost/mattermost-server/v6/app/imaging"
"github.com/mattermost/mattermost-server/v6/app/request"
@@ -592,9 +595,18 @@ func (a *App) SetBotIconImage(botUserId string, file io.ReadSeeker) *model.AppEr
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.parse.app_error", nil, err.Error(), http.StatusBadRequest)
}
// Set icon
file.Seek(0, 0)
if _, err = a.WriteFile(file, getBotIconPath(botUserId)); err != nil {
data, readErr := ioutil.ReadAll(file)
if readErr != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.read.app_error", nil, readErr.Error(), http.StatusInternalServerError)
}
if storedData, readFileErr := a.ReadFile(getBotIconPath(botUserId)); readFileErr == nil && bytes.Equal(storedData, data) {
return nil
}
// Set icon
if _, err = a.WriteFile(bytes.NewReader(data), getBotIconPath(botUserId)); err != nil {
return model.NewAppError("SetBotIconImage", "api.bot.set_bot_icon_image.app_error", nil, err.Error(), http.StatusInternalServerError)
}
@@ -666,5 +678,5 @@ func (a *App) GetBotIconImage(botUserId string) ([]byte, *model.AppError) {
}
func getBotIconPath(botUserId string) string {
return fmt.Sprintf("bots/%v/icon.svg", botUserId)
return filepath.Join("bots", botUserId, "icon.svg")
}