From 91fe8bb2c0d520f13269b2eadc2717a5ec4eea1c Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 17 Feb 2017 10:31:21 -0500 Subject: Implement upload and get file endpoints for APIv4 (#5396) * Implement POST /files endpoint for APIv4 * Implement GET /files/{file_id} endpoint for APIv4 --- app/file.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'app/file.go') 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 + } +} -- cgit v1.2.3-1-g7c22