From c84d0114ae53f67800cca8b702254b9f6e69b604 Mon Sep 17 00:00:00 2001 From: vvisionnn Date: Fri, 4 Feb 2022 12:07:56 +0800 Subject: [PATCH] Fix: trigger err when move folder into itself (#1128) --- models/folder.go | 7 +++++++ models/folder_test.go | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/models/folder.go b/models/folder.go index 8c8d6c0..8655b53 100644 --- a/models/folder.go +++ b/models/folder.go @@ -263,6 +263,13 @@ func (folder *Folder) CopyFolderTo(folderID uint, dstFolder *Folder) (size uint6 // MoveFolderTo 将folder目录下的dirs子目录复制或移动到dstFolder, // 返回此过程中增加的容量 func (folder *Folder) MoveFolderTo(dirs []uint, dstFolder *Folder) error { + + // 如果目标位置为待移动的目录,会导致 parent 为自己 + // 造成死循环且无法被除搜索以外的组件展示 + if folder.OwnerID == dstFolder.OwnerID && util.ContainsUint(dirs, dstFolder.ID) { + return errors.New("cannot move a folder into itself") + } + // 更改顶级要移动目录的父目录指向 err := DB.Model(Folder{}).Where( "id in (?) and owner_id = ? and parent_id = ?", diff --git a/models/folder_test.go b/models/folder_test.go index dd286f2..7c74f48 100644 --- a/models/folder_test.go +++ b/models/folder_test.go @@ -493,7 +493,8 @@ func TestFolder_MoveOrCopyFolderTo_Move(t *testing.T) { } // 目标目录 dstFolder := Folder{ - Model: gorm.Model{ID: 10}, + Model: gorm.Model{ID: 10}, + OwnerID: 1, } // 成功 @@ -507,6 +508,12 @@ func TestFolder_MoveOrCopyFolderTo_Move(t *testing.T) { asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(err) } + + // 移动自己到自己内部,失败 + { + err := parFolder.MoveFolderTo([]uint{10, 2}, &dstFolder) + asserts.Error(err) + } } func TestFolder_FileInfoInterface(t *testing.T) {