summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/file.go30
-rw-r--r--app/file_test.go31
-rw-r--r--app/plugin_api.go4
3 files changed, 65 insertions, 0 deletions
diff --git a/app/file.go b/app/file.go
index 7dbcdd394..7a642a956 100644
--- a/app/file.go
+++ b/app/file.go
@@ -604,3 +604,33 @@ func (a *App) GetFileInfo(fileId string) (*model.FileInfo, *model.AppError) {
return result.Data.(*model.FileInfo), nil
}
}
+
+func (a *App) CopyFileInfos(userId string, fileIds []string) ([]string, *model.AppError) {
+ newFileIds := []string{}
+
+ now := model.GetMillis()
+
+ for _, fileId := range fileIds {
+ fileInfo := &model.FileInfo{}
+
+ if result := <-a.Srv.Store.FileInfo().Get(fileId); result.Err != nil {
+ return nil, result.Err
+ } else {
+ fileInfo = result.Data.(*model.FileInfo)
+ }
+
+ fileInfo.Id = model.NewId()
+ fileInfo.CreatorId = userId
+ fileInfo.CreateAt = now
+ fileInfo.UpdateAt = now
+ fileInfo.PostId = ""
+
+ if result := <-a.Srv.Store.FileInfo().Save(fileInfo); result.Err != nil {
+ return newFileIds, result.Err
+ }
+
+ newFileIds = append(newFileIds, fileInfo.Id)
+ }
+
+ return newFileIds, nil
+}
diff --git a/app/file_test.go b/app/file_test.go
index 23750ad6e..c736328cf 100644
--- a/app/file_test.go
+++ b/app/file_test.go
@@ -161,3 +161,34 @@ func TestMigrateFilenamesToFileInfos(t *testing.T) {
infos = th.App.MigrateFilenamesToFileInfos(rpost)
assert.Equal(t, 1, len(infos))
}
+
+func TestCopyFileInfos(t *testing.T) {
+ th := Setup().InitBasic()
+ defer th.TearDown()
+
+ teamId := model.NewId()
+ channelId := model.NewId()
+ userId := model.NewId()
+ filename := "test"
+ data := []byte("abcd")
+
+ info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
+ require.Nil(t, err)
+ defer func() {
+ <-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
+ th.App.RemoveFile(info1.Path)
+ }()
+
+ infoIds, err := th.App.CopyFileInfos(userId, []string{info1.Id})
+ require.Nil(t, err)
+
+ info2, err := th.App.GetFileInfo(infoIds[0])
+ require.Nil(t, err)
+ defer func() {
+ <-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
+ th.App.RemoveFile(info2.Path)
+ }()
+
+ assert.NotEqual(t, info1.Id, info2.Id, "should not be equal")
+ assert.Equal(t, info2.PostId, "", "should be empty string")
+}
diff --git a/app/plugin_api.go b/app/plugin_api.go
index 31bfd401d..c3ab8fab2 100644
--- a/app/plugin_api.go
+++ b/app/plugin_api.go
@@ -275,6 +275,10 @@ func (api *PluginAPI) UpdatePost(post *model.Post) (*model.Post, *model.AppError
return api.app.UpdatePost(post, false)
}
+func (api *PluginAPI) CopyFileInfos(userId string, fileIds []string) ([]string, *model.AppError) {
+ return api.app.CopyFileInfos(userId, fileIds)
+}
+
func (api *PluginAPI) KVSet(key string, value []byte) *model.AppError {
return api.app.SetPluginKey(api.id, key, value)
}