From 437e4e0b292c30f86c4a58a34c453692a7955d5e Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Sun, 10 Nov 2024 17:23:26 -0600 Subject: [PATCH] more robust recursive folder check Signed-off-by: eternal-flame-AD --- .../src/core/entities/DriveFolderEntityService.ts | 6 ++++-- .../src/server/api/endpoints/drive/folders/update.ts | 9 ++++++--- packages/backend/test/e2e/endpoints.ts | 9 +++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/core/entities/DriveFolderEntityService.ts b/packages/backend/src/core/entities/DriveFolderEntityService.ts index 299f23ad38..3ff9898673 100644 --- a/packages/backend/src/core/entities/DriveFolderEntityService.ts +++ b/packages/backend/src/core/entities/DriveFolderEntityService.ts @@ -30,7 +30,8 @@ export class DriveFolderEntityService { public async pack( src: MiDriveFolder['id'] | MiDriveFolder, options?: { - detail: boolean + detail: boolean, + maxDepth: number, }, ): Promise> { const opts = Object.assign({ @@ -55,7 +56,8 @@ export class DriveFolderEntityService { ...(folder.parentId ? { parent: this.pack(folder.parentId, { - detail: true, + detail: (options?.maxDepth ? options.maxDepth > 0 : true), + maxDepth: options?.maxDepth || 32, }), } : {}), } : {}), diff --git a/packages/backend/src/server/api/endpoints/drive/folders/update.ts b/packages/backend/src/server/api/endpoints/drive/folders/update.ts index 62b04e1df3..cc45bd8c58 100644 --- a/packages/backend/src/server/api/endpoints/drive/folders/update.ts +++ b/packages/backend/src/server/api/endpoints/drive/folders/update.ts @@ -32,7 +32,7 @@ export const meta = { }, recursiveNesting: { - message: 'It can not be structured like nesting folders recursively.', + message: 'Folders are linked recursively or too deeply.', code: 'RECURSIVE_NESTING', id: 'dbeb024837894013aed44279f9199740', }, @@ -94,7 +94,10 @@ export default class extends Endpoint { // eslint- } // Check if the circular reference will occur - const checkCircle = async (folderId: string): Promise => { + const checkCircle = async (folderId: string, limit: number = 32): Promise => { + if (limit <= 0) { + return false; + } const folder2 = await this.driveFoldersRepository.findOneByOrFail({ id: folderId, }); @@ -102,7 +105,7 @@ export default class extends Endpoint { // eslint- if (folder2.id === folder.id) { return true; } else if (folder2.parentId) { - return await checkCircle(folder2.parentId); + return await checkCircle(folder2.parentId, limit - 1); } else { return false; } diff --git a/packages/backend/test/e2e/endpoints.ts b/packages/backend/test/e2e/endpoints.ts index b91d77c398..a80ad3b087 100644 --- a/packages/backend/test/e2e/endpoints.ts +++ b/packages/backend/test/e2e/endpoints.ts @@ -927,14 +927,15 @@ describe('Endpoints', () => { const folderC = (await api('drive/folders/create', { name: 'test', }, alice)).body; - await api('drive/folders/update', { + assert.ok(folderA.id && folderB.id && folderC.id); + assert.strictEqual((await api('drive/folders/update', { folderId: folderB.id, parentId: folderA.id, - }, alice); - await api('drive/folders/update', { + }, alice)).status, 200); + assert.strictEqual((await api('drive/folders/update', { folderId: folderC.id, parentId: folderB.id, - }, alice); + }, alice)).status, 200); const res = await api('drive/folders/update', { folderId: folderA.id,