diff options
author | Christian Hoff <hoff.christian@posteo.de> | 2018-03-01 20:11:44 +0100 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2018-03-01 19:11:44 +0000 |
commit | 2b3b6051d265edf131d006b2eb14f55284faf1e5 (patch) | |
tree | 2bf50fd4c6ff09daf897f818aaef0c357b67ca79 /model/client4.go | |
parent | 51c7198d53a2fbc4c7d47b3eb308781b69bf4b51 (diff) | |
download | chat-2b3b6051d265edf131d006b2eb14f55284faf1e5.tar.gz chat-2b3b6051d265edf131d006b2eb14f55284faf1e5.tar.bz2 chat-2b3b6051d265edf131d006b2eb14f55284faf1e5.zip |
PLT-7567: Integration of Team Icons (#8284)
* PLT-7567: Integration of Team Icons
* PLT-7567: Read replica workaround, upgrade logic moved, more concrete i18n key
* PLT-7567: Read replica workaround, corrections
* PLT-7567: upgrade correction
Diffstat (limited to 'model/client4.go')
-rw-r--r-- | model/client4.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/model/client4.go b/model/client4.go index c1587f882..1d71d7b3e 100644 --- a/model/client4.go +++ b/model/client4.go @@ -3318,3 +3318,56 @@ func (c *Client4) DeactivatePlugin(id string) (bool, *Response) { return CheckStatusOK(r), BuildResponse(r) } } + +// SetTeamIcon sets team icon of the team +func (c *Client4) SetTeamIcon(teamId string, data []byte) (bool, *Response) { + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + if part, err := writer.CreateFormFile("image", "teamIcon.png"); err != nil { + return false, &Response{Error: NewAppError("SetTeamIcon", "model.client.set_team_icon.no_file.app_error", nil, err.Error(), http.StatusBadRequest)} + } else if _, err = io.Copy(part, bytes.NewBuffer(data)); err != nil { + return false, &Response{Error: NewAppError("SetTeamIcon", "model.client.set_team_icon.no_file.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + if err := writer.Close(); err != nil { + return false, &Response{Error: NewAppError("SetTeamIcon", "model.client.set_team_icon.writer.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + rq, _ := http.NewRequest("POST", c.ApiUrl+c.GetTeamRoute(teamId)+"/image", bytes.NewReader(body.Bytes())) + rq.Header.Set("Content-Type", writer.FormDataContentType()) + rq.Close = true + + if len(c.AuthToken) > 0 { + rq.Header.Set(HEADER_AUTH, c.AuthType+" "+c.AuthToken) + } + + if rp, err := c.HttpClient.Do(rq); err != nil || rp == nil { + // set to http.StatusForbidden(403) + return false, &Response{StatusCode: http.StatusForbidden, Error: NewAppError(c.GetTeamRoute(teamId)+"/image", "model.client.connecting.app_error", nil, err.Error(), 403)} + } else { + defer closeBody(rp) + + if rp.StatusCode >= 300 { + return false, BuildErrorResponse(rp, AppErrorFromJson(rp.Body)) + } else { + return CheckStatusOK(rp), BuildResponse(rp) + } + } +} + +// GetTeamIcon gets the team icon of the team +func (c *Client4) GetTeamIcon(teamId, etag string) ([]byte, *Response) { + if r, err := c.DoApiGet(c.GetTeamRoute(teamId)+"/image", etag); err != nil { + return nil, BuildErrorResponse(r, err) + } else { + defer closeBody(r) + + if data, err := ioutil.ReadAll(r.Body); err != nil { + return nil, BuildErrorResponse(r, NewAppError("GetTeamIcon", "model.client.get_team_icon.app_error", nil, err.Error(), r.StatusCode)) + } else { + return data, BuildResponse(r) + } + } +} |