diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-02-17 10:31:21 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-17 10:31:21 -0500 |
commit | 91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c (patch) | |
tree | 088fc7015274975c4f1494a5b5afe72af84b6966 /api/file.go | |
parent | 4e7dbc3bb0e93bafa684594b19c5648dc030ee17 (diff) | |
download | chat-91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c.tar.gz chat-91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c.tar.bz2 chat-91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c.zip |
Implement upload and get file endpoints for APIv4 (#5396)
* Implement POST /files endpoint for APIv4
* Implement GET /files/{file_id} endpoint for APIv4
Diffstat (limited to 'api/file.go')
-rw-r--r-- | api/file.go | 62 |
1 files changed, 7 insertions, 55 deletions
diff --git a/api/file.go b/api/file.go index 56a209d36..afc0ddbd8 100644 --- a/api/file.go +++ b/api/file.go @@ -4,9 +4,6 @@ package api import ( - "bytes" - _ "image/gif" - "io" "net/http" "net/url" "strconv" @@ -16,7 +13,6 @@ import ( "github.com/mattermost/platform/app" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" - _ "golang.org/x/image/bmp" ) func InitFile() { @@ -35,12 +31,6 @@ func InitFile() { } func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { - if len(utils.Cfg.FileSettings.DriverName) == 0 { - c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.storage.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented - return - } - if r.ContentLength > *utils.Cfg.FileSettings.MaxFileSize { c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.too_large.app_error", nil, "") c.Err.StatusCode = http.StatusRequestEntityTooLarge @@ -70,48 +60,12 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { return } - resStruct := &model.FileUploadResponse{ - FileInfos: []*model.FileInfo{}, - ClientIds: []string{}, - } - - previewPathList := []string{} - thumbnailPathList := []string{} - imageDataList := [][]byte{} - - for i, fileHeader := range m.File["files"] { - file, fileErr := fileHeader.Open() - defer file.Close() - if fileErr != nil { - http.Error(w, fileErr.Error(), http.StatusInternalServerError) - return - } - - buf := bytes.NewBuffer(nil) - io.Copy(buf, file) - data := buf.Bytes() - - info, err := app.DoUploadFile(c.TeamId, channelId, c.Session.UserId, fileHeader.Filename, data) - if err != nil { - c.Err = err - return - } - - if info.PreviewPath != "" || info.ThumbnailPath != "" { - previewPathList = append(previewPathList, info.PreviewPath) - thumbnailPathList = append(thumbnailPathList, info.ThumbnailPath) - imageDataList = append(imageDataList, data) - } - - resStruct.FileInfos = append(resStruct.FileInfos, info) - - if len(m.Value["client_ids"]) > 0 { - resStruct.ClientIds = append(resStruct.ClientIds, m.Value["client_ids"][i]) - } + resStruct, err := app.UploadFiles(c.TeamId, channelId, c.Session.UserId, m.File["files"], m.Value["client_ids"]) + if err != nil { + c.Err = err + return } - app.HandleImages(previewPathList, thumbnailPathList, imageDataList) - w.Write([]byte(resStruct.ToJson())) } @@ -239,11 +193,9 @@ func getFileInfoForRequest(c *Context, r *http.Request, requireFileVisible bool) return nil, NewInvalidParamError("getFileInfoForRequest", "file_id") } - var info *model.FileInfo - if result := <-app.Srv.Store.FileInfo().Get(fileId); result.Err != nil { - return nil, result.Err - } else { - info = result.Data.(*model.FileInfo) + info, err := app.GetFileInfo(fileId) + if err != nil { + return nil, err } // only let users access files visible in a channel, unless they're the one who uploaded the file |