Implement brand image endpoints for APIv4 (#5733)

* Implement brand image endpoints for APIv4

* Fix unit test
Этот коммит содержится в:
Joram Wilander
2017-03-14 09:35:48 -04:00
коммит произвёл GitHub
родитель d03367c560
Коммит ad0ed008fe
7 изменённых файлов: 204 добавлений и 15 удалений

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

@@ -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)
}
}