From 4ba0f467aa4f2b327005098d8e278c163f7f1a5d Mon Sep 17 00:00:00 2001 From: Max Zhang <48145854+chetbae@users.noreply.github.com> Date: Wed, 2 Aug 2023 11:32:39 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Add=20move-to=20target=20is=20no?= =?UTF-8?q?t=20subfolder=20check,=20update=20moveEntry=20to=20use=20canMov?= =?UTF-8?q?eEntry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Dashboard/FileSystem/fs_functions.ts | 37 +++++++++++++++--------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Dashboard/FileSystem/fs_functions.ts b/src/Dashboard/FileSystem/fs_functions.ts index b5b4b077..10537f48 100644 --- a/src/Dashboard/FileSystem/fs_functions.ts +++ b/src/Dashboard/FileSystem/fs_functions.ts @@ -89,19 +89,33 @@ const canRemoveEntry = (entry: IEntry, parent: IFolder): responseProp => { * @returns response object { succeeded: boolean, error: string } */ const canMoveEntry = (entry: IEntry, parent: IFolder, newParent: IFolder): responseProp => { + // is A a child of B + function isChildOf(a: IEntry, b: IFolder) { + return b.content + .filter((e) => e.type === EntryType.Folder) + .map((c: IFolder) => { + if (c === a) return true; + else return isChildOf(a, c); + }) + .some((e) => e === true); + } + const isSame = entry === newParent; const isDuplicate = newParent.content.some((e) => e.name === entry.name); const isImmutableParent = parent.metadata['immutable']; const isImmutableNewParent = newParent.metadata['immutable']; - const idx = parent.content.findIndex((e) => e.name === entry.name); - const existsInParent = idx !== -1; + const existsInParent = parent.content.findIndex((e) => e.name === entry.name) !== -1; + // if entry is a folder, check if newParent is a subfolder of entry + const newParentIsChildOfEntry = (entry.type == EntryType.Folder) ? isChildOf(newParent, entry as IFolder) : false; + const returnObj = { succeeded: false, error: '' }; - if (isSame) returnObj.error = `Cannot move ${entry.name} to itself.`; - else if (isDuplicate) returnObj.error = `Duplicate name: ${entry.name} already exists in ${newParent.name}.`; - else if (isImmutableParent) returnObj.error = `${parent.name} is immutable.`; - else if (isImmutableNewParent) returnObj.error = `${newParent.name} is immutable.`; - else if (!existsInParent) returnObj.error = `${entry.name} does not exist in ${parent.name}.`; + if (isSame) returnObj.error = `Cannot move ${entry.name} to itself.`; + else if (isDuplicate) returnObj.error = `Duplicate name: ${entry.name} already exists in ${newParent.name}.`; + else if (isImmutableParent) returnObj.error = `${parent.name} is immutable.`; + else if (isImmutableNewParent) returnObj.error = `${newParent.name} is immutable.`; + else if (!existsInParent) returnObj.error = `${entry.name} does not exist in ${parent.name}.`; + else if (newParentIsChildOfEntry) returnObj.error = `Cannot move ${entry.name} into a its own subfolder, ${newParent.name}.`; else returnObj.succeeded = true; return returnObj; @@ -173,11 +187,9 @@ const removeEntry = (entry: IEntry, parent: IFolder, force=false): boolean => { * @returns true if moved, else false */ function moveEntry(entry: IEntry, parent: IFolder, newParent: IFolder): boolean { - const addResponse = canAddEntry(entry, newParent); - const removeResponse = canRemoveEntry(entry, parent); - const canMove = addResponse.succeeded && removeResponse.succeeded; + const moveResponse = canMoveEntry(entry, parent, newParent); - if (canMove) { + if (moveResponse.succeeded) { const removed = removeEntry(entry, parent, true); if (removed) { const added = addEntry(entry, newParent); @@ -188,8 +200,7 @@ function moveEntry(entry: IEntry, parent: IFolder, newParent: IFolder): boolean console.error(`Moving: failed to remove ${entry.name} from ${parent.name}`); } } else { - console.error(addResponse.error); - console.error(removeResponse.error); + console.error(moveResponse.error); } return false; }