Implement upload and get file endpoints for APIv4 (#5396)

* Implement POST /files endpoint for APIv4

* Implement GET /files/{file_id} endpoint for APIv4
Этот коммит содержится в:
Joram Wilander
2017-02-17 10:31:21 -05:00
коммит произвёл GitHub
родитель 4e7dbc3bb0
Коммит 91fe8bb2c0
11 изменённых файлов: 500 добавлений и 63 удалений

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

@@ -4,7 +4,11 @@
package model
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
)
@@ -112,6 +116,14 @@ func (c *Client4) GetPostRoute(postId string) string {
return fmt.Sprintf(c.GetPostsRoute()+"/%v", postId)
}
func (c *Client4) GetFilesRoute() string {
return fmt.Sprintf("/files")
}
func (c *Client4) GetFileRoute(fileId string) string {
return fmt.Sprintf(c.GetFilesRoute()+"/%v", fileId)
}
func (c *Client4) GetSystemRoute() string {
return fmt.Sprintf("/system")
}
@@ -156,6 +168,25 @@ func (c *Client4) DoApiRequest(method, url, data, etag string) (*http.Response,
}
}
func (c *Client4) DoUploadFile(url string, data []byte, contentType string) (*FileUploadResponse, *Response) {
rq, _ := http.NewRequest("POST", c.ApiUrl+url, bytes.NewReader(data))
rq.Header.Set("Content-Type", contentType)
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 nil, &Response{Error: NewAppError(url, "model.client.connecting.app_error", nil, err.Error(), 0)}
} else if rp.StatusCode >= 300 {
return nil, &Response{StatusCode: rp.StatusCode, Error: AppErrorFromJson(rp.Body)}
} else {
defer closeBody(rp)
return FileUploadResponseFromJson(rp.Body), BuildResponse(rp)
}
}
// CheckStatusOK is a convenience function for checking the standard OK response
// from the web service.
func CheckStatusOK(r *http.Response) bool {
@@ -595,6 +626,46 @@ func (c *Client4) GetPostsForChannel(channelId string, page, perPage int, etag s
}
}
// File Section
// UploadFile will upload a file to a channel, to be later attached to a post.
func (c *Client4) UploadFile(data []byte, channelId string, filename string) (*FileUploadResponse, *Response) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
if part, err := writer.CreateFormFile("files", filename); err != nil {
return nil, &Response{Error: NewAppError("UploadPostAttachment", "model.client.upload_post_attachment.file.app_error", nil, err.Error(), http.StatusBadRequest)}
} else if _, err = io.Copy(part, bytes.NewBuffer(data)); err != nil {
return nil, &Response{Error: NewAppError("UploadPostAttachment", "model.client.upload_post_attachment.file.app_error", nil, err.Error(), http.StatusBadRequest)}
}
if part, err := writer.CreateFormField("channel_id"); err != nil {
return nil, &Response{Error: NewAppError("UploadPostAttachment", "model.client.upload_post_attachment.channel_id.app_error", nil, err.Error(), http.StatusBadRequest)}
} else if _, err = io.Copy(part, strings.NewReader(channelId)); err != nil {
return nil, &Response{Error: NewAppError("UploadPostAttachment", "model.client.upload_post_attachment.channel_id.app_error", nil, err.Error(), http.StatusBadRequest)}
}
if err := writer.Close(); err != nil {
return nil, &Response{Error: NewAppError("UploadPostAttachment", "model.client.upload_post_attachment.writer.app_error", nil, err.Error(), http.StatusBadRequest)}
}
return c.DoUploadFile(c.GetFilesRoute(), body.Bytes(), writer.FormDataContentType())
}
// GetFile gets the bytes for a file by id.
func (c *Client4) GetFile(fileId string) ([]byte, *Response) {
if r, err := c.DoApiGet(c.GetFileRoute(fileId), ""); 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("GetFile", "model.client.read_file.app_error", nil, err.Error(), r.StatusCode)}
} else {
return data, BuildResponse(r)
}
}
// General Section
// GetPing will ping the server and to see if it is up and running.
func (c *Client4) GetPing() (bool, *Response) {
if r, err := c.DoApiGet(c.GetSystemRoute()+"/ping", ""); err != nil {
return false, &Response{StatusCode: r.StatusCode, Error: err}
@@ -603,6 +674,3 @@ func (c *Client4) GetPing() (bool, *Response) {
return CheckStatusOK(r), BuildResponse(r)
}
}
// Files Section
// to be filled in..