diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-14 09:35:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-14 09:35:48 -0400 |
commit | ad0ed008fe54534fcc089f479df606ab921901a9 (patch) | |
tree | a5fda084fb7ea61941449b393a6084f0c049195c /model/client4.go | |
parent | d03367c56005470396d883d273323ecbd8d4f243 (diff) | |
download | chat-ad0ed008fe54534fcc089f479df606ab921901a9.tar.gz chat-ad0ed008fe54534fcc089f479df606ab921901a9.tar.bz2 chat-ad0ed008fe54534fcc089f479df606ab921901a9.zip |
Implement brand image endpoints for APIv4 (#5733)
* Implement brand image endpoints for APIv4
* Fix unit test
Diffstat (limited to 'model/client4.go')
-rw-r--r-- | model/client4.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/model/client4.go b/model/client4.go index 799fec802..df09174d5 100644 --- a/model/client4.go +++ b/model/client4.go @@ -190,6 +190,10 @@ func (c *Client4) GetLdapRoute() string { return fmt.Sprintf("/ldap") } +func (c *Client4) GetBrandRoute() string { + return fmt.Sprintf("/brand") +} + func (c *Client4) DoApiGet(url string, etag string) (*http.Response, *AppError) { return c.DoApiRequest(http.MethodGet, url, "", etag) } @@ -1498,3 +1502,49 @@ func (c *Client4) TestLdap() (bool, *Response) { return CheckStatusOK(r), BuildResponse(r) } } + +// Brand Section + +// GetBrandImage retrieves the previously uploaded brand image. +func (c *Client4) GetBrandImage() ([]byte, *Response) { + if r, err := c.DoApiGet(c.GetBrandRoute()+"/image", ""); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else if data, err := ioutil.ReadAll(r.Body); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: NewAppError("GetBrandImage", "model.client.read_file.app_error", nil, err.Error(), r.StatusCode)} + } else { + return data, BuildResponse(r) + } +} + +// UploadBrandImage sets the brand image for the system. +func (c *Client4) UploadBrandImage(data []byte) (bool, *Response) { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + if part, err := writer.CreateFormFile("image", "brand.png"); err != nil { + return false, &Response{Error: NewAppError("UploadBrandImage", "model.client.set_profile_user.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("UploadBrandImage", "model.client.set_profile_user.no_file.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + if err := writer.Close(); err != nil { + return false, &Response{Error: NewAppError("UploadBrandImage", "model.client.set_profile_user.writer.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + rq, _ := http.NewRequest("POST", c.ApiUrl+c.GetBrandRoute()+"/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 { + return false, &Response{StatusCode: http.StatusForbidden, Error: NewAppError(c.GetBrandRoute()+"/image", "model.client.connecting.app_error", nil, err.Error(), http.StatusForbidden)} + } else if rp.StatusCode >= 300 { + return false, &Response{StatusCode: rp.StatusCode, Error: AppErrorFromJson(rp.Body)} + } else { + defer closeBody(rp) + return CheckStatusOK(rp), BuildResponse(rp) + } +} |