Add GetTeamIcon plugin api method (#9837)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
b29f1cb844
Коммит
7a8d7e53cc
@@ -453,6 +453,19 @@ func (api *PluginAPI) GetEmojiImage(emojiId string) ([]byte, string, *model.AppE
|
||||
return api.app.GetEmojiImage(emojiId)
|
||||
}
|
||||
|
||||
func (api *PluginAPI) GetTeamIcon(teamId string) ([]byte, *model.AppError) {
|
||||
team, err := api.app.GetTeam(teamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := api.app.GetTeamIcon(team)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Plugin Section
|
||||
|
||||
func (api *PluginAPI) GetPlugins() ([]*model.Manifest, *model.AppError) {
|
||||
|
||||
@@ -311,3 +311,34 @@ func TestPluginAPIGetPlugins(t *testing.T) {
|
||||
assert.NotEmpty(t, plugins)
|
||||
assert.Equal(t, pluginManifests, plugins)
|
||||
}
|
||||
|
||||
func TestPluginAPIGetTeamIcon(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
api := th.SetupPluginAPI()
|
||||
|
||||
// Create an 128 x 128 image
|
||||
img := image.NewRGBA(image.Rect(0, 0, 128, 128))
|
||||
// Draw a red dot at (2, 3)
|
||||
img.Set(2, 3, color.RGBA{255, 0, 0, 255})
|
||||
buf := new(bytes.Buffer)
|
||||
err := png.Encode(buf, img)
|
||||
require.Nil(t, err)
|
||||
dataBytes := buf.Bytes()
|
||||
fileReader := bytes.NewReader(dataBytes)
|
||||
|
||||
// Set the Team Icon
|
||||
err = th.App.SetTeamIconFromFile(th.BasicTeam, fileReader)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Get the team icon to check
|
||||
imageProfile, err := api.GetTeamIcon(th.BasicTeam.Id)
|
||||
require.Nil(t, err)
|
||||
require.NotEmpty(t, imageProfile)
|
||||
|
||||
colorful := color.NRGBA{255, 0, 0, 255}
|
||||
byteReader := bytes.NewReader(imageProfile)
|
||||
img2, _, err2 := image.Decode(byteReader)
|
||||
require.Nil(t, err2)
|
||||
require.Equal(t, img2.At(2, 3), colorful)
|
||||
}
|
||||
|
||||
15
app/team.go
15
app/team.go
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -1093,10 +1094,10 @@ func (a *App) SetTeamIcon(teamId string, imageData *multipart.FileHeader) *model
|
||||
return model.NewAppError("SetTeamIcon", "api.team.set_team_icon.open.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
defer file.Close()
|
||||
return a.SetTeamIconFromFile(teamId, file)
|
||||
return a.SetTeamIconFromMultiPartFile(teamId, file)
|
||||
}
|
||||
|
||||
func (a *App) SetTeamIconFromFile(teamId string, file multipart.File) *model.AppError {
|
||||
func (a *App) SetTeamIconFromMultiPartFile(teamId string, file multipart.File) *model.AppError {
|
||||
team, getTeamErr := a.GetTeam(teamId)
|
||||
|
||||
if getTeamErr != nil {
|
||||
@@ -1118,14 +1119,16 @@ func (a *App) SetTeamIconFromFile(teamId string, file multipart.File) *model.App
|
||||
|
||||
file.Seek(0, 0)
|
||||
|
||||
return a.SetTeamIconFromFile(team, file)
|
||||
}
|
||||
|
||||
func (a *App) SetTeamIconFromFile(team *model.Team, file io.Reader) *model.AppError {
|
||||
// Decode image into Image object
|
||||
img, _, err := image.Decode(file)
|
||||
if err != nil {
|
||||
return model.NewAppError("SetTeamIcon", "api.team.set_team_icon.decode.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
file.Seek(0, 0)
|
||||
|
||||
orientation, _ := getImageOrientation(file)
|
||||
img = makeImageUpright(img, orientation)
|
||||
|
||||
@@ -1139,7 +1142,7 @@ func (a *App) SetTeamIconFromFile(teamId string, file multipart.File) *model.App
|
||||
return model.NewAppError("SetTeamIcon", "api.team.set_team_icon.encode.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
path := "teams/" + teamId + "/teamIcon.png"
|
||||
path := "teams/" + team.Id + "/teamIcon.png"
|
||||
|
||||
if _, err := a.WriteFile(buf, path); err != nil {
|
||||
return model.NewAppError("SetTeamIcon", "api.team.set_team_icon.write_file.app_error", nil, "", http.StatusInternalServerError)
|
||||
@@ -1147,7 +1150,7 @@ func (a *App) SetTeamIconFromFile(teamId string, file multipart.File) *model.App
|
||||
|
||||
curTime := model.GetMillis()
|
||||
|
||||
if result := <-a.Srv.Store.Team().UpdateLastTeamIconUpdate(teamId, curTime); result.Err != nil {
|
||||
if result := <-a.Srv.Store.Team().UpdateLastTeamIconUpdate(team.Id, curTime); result.Err != nil {
|
||||
return model.NewAppError("SetTeamIcon", "api.team.team_icon.update.app_error", nil, result.Err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user