summaryrefslogtreecommitdiffstats
path: root/app/file.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-02-17 10:31:21 -0500
committerGitHub <noreply@github.com>2017-02-17 10:31:21 -0500
commit91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c (patch)
tree088fc7015274975c4f1494a5b5afe72af84b6966 /app/file.go
parent4e7dbc3bb0e93bafa684594b19c5648dc030ee17 (diff)
downloadchat-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 'app/file.go')
-rw-r--r--app/file.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/app/file.go b/app/file.go
index 095e4d032..2ac3c398a 100644
--- a/app/file.go
+++ b/app/file.go
@@ -15,6 +15,7 @@ import (
"image/jpeg"
"io"
"io/ioutil"
+ "mime/multipart"
"net/http"
"net/url"
"os"
@@ -370,6 +371,54 @@ func GeneratePublicLinkHash(fileId, salt string) string {
return base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
}
+func UploadFiles(teamId string, channelId string, userId string, fileHeaders []*multipart.FileHeader, clientIds []string) (*model.FileUploadResponse, *model.AppError) {
+ if len(utils.Cfg.FileSettings.DriverName) == 0 {
+ return nil, model.NewAppError("uploadFile", "api.file.upload_file.storage.app_error", nil, "", http.StatusNotImplemented)
+ }
+
+ resStruct := &model.FileUploadResponse{
+ FileInfos: []*model.FileInfo{},
+ ClientIds: []string{},
+ }
+
+ previewPathList := []string{}
+ thumbnailPathList := []string{}
+ imageDataList := [][]byte{}
+
+ for i, fileHeader := range fileHeaders {
+ file, fileErr := fileHeader.Open()
+ defer file.Close()
+ if fileErr != nil {
+ return nil, model.NewAppError("UploadFiles", "api.file.upload_file.bad_parse.app_error", nil, fileErr.Error(), http.StatusBadRequest)
+ }
+
+ buf := bytes.NewBuffer(nil)
+ io.Copy(buf, file)
+ data := buf.Bytes()
+
+ info, err := DoUploadFile(teamId, channelId, userId, fileHeader.Filename, data)
+ if err != nil {
+ return nil, err
+ }
+
+ 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(clientIds) > 0 {
+ resStruct.ClientIds = append(resStruct.ClientIds, clientIds[i])
+ }
+ }
+
+ HandleImages(previewPathList, thumbnailPathList, imageDataList)
+
+ return resStruct, nil
+}
+
func DoUploadFile(teamId string, channelId string, userId string, rawFilename string, data []byte) (*model.FileInfo, *model.AppError) {
filename := filepath.Base(rawFilename)
@@ -527,3 +576,11 @@ func generatePreviewImage(img image.Image, previewPath string, width int) {
return
}
}
+
+func GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
+ if result := <-Srv.Store.FileInfo().Get(fileId); result.Err != nil {
+ return nil, result.Err
+ } else {
+ return result.Data.(*model.FileInfo), nil
+ }
+}