Add GetFile API in app and plugin (#9820)

Этот коммит содержится в:
Vaibhav Thakkar
2018-12-13 15:16:42 +05:30
коммит произвёл Hanzei
родитель 0541e765ad
Коммит dbd3801271
6 изменённых файлов: 103 добавлений и 0 удалений

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

@@ -640,6 +640,20 @@ func (a *App) GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
return result.Data.(*model.FileInfo), nil
}
func (a *App) GetFile(fileId string) ([]byte, *model.AppError) {
info, err := a.GetFileInfo(fileId)
if err != nil {
return nil, err
}
data, err := a.ReadFile(info.Path)
if err != nil {
return nil, err
}
return data, nil
}
func (a *App) CopyFileInfos(userId string, fileIds []string) ([]string, *model.AppError) {
var newFileIds []string

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

@@ -484,6 +484,10 @@ func (api *PluginAPI) ReadFile(path string) ([]byte, *model.AppError) {
return api.app.ReadFile(path)
}
func (api *PluginAPI) GetFile(fileId string) ([]byte, *model.AppError) {
return api.app.GetFile(fileId)
}
func (api *PluginAPI) UploadFile(data []byte, channelId string, filename string) (*model.FileInfo, *model.AppError) {
return api.app.UploadFile(data, channelId, filename)
}

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

@@ -15,6 +15,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
@@ -173,6 +174,31 @@ func TestPluginAPIUpdateUserStatus(t *testing.T) {
assert.Nil(t, status)
}
func TestPluginAPIGetFile(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
api := th.SetupPluginAPI()
// check a valid file first
uploadTime := time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local)
filename := "testGetFile"
fileData := []byte("Hello World")
info, err := th.App.DoUploadFile(uploadTime, th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, filename, fileData)
require.Nil(t, err)
defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info.Id)
th.App.RemoveFile(info.Path)
}()
data, err1 := api.GetFile(info.Id)
require.Nil(t, err1)
assert.Equal(t, data, fileData)
// then checking invalid file
data, err = api.GetFile("../fake/testingApi")
require.NotNil(t, err)
require.Nil(t, data)
}
func TestPluginAPISavePluginConfig(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()