From 2056996a30c5587f5ff5749536391977c1623319 Mon Sep 17 00:00:00 2001 From: Eric Doughty-Papassideris Date: Tue, 28 May 2024 16:10:10 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Avoid=20iterating=20ite?= =?UTF-8?q?m=20path=20twice=20to=20move=20to=20trash=20(#433)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/services/documents/services/index.ts | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/tdrive/backend/node/src/services/documents/services/index.ts b/tdrive/backend/node/src/services/documents/services/index.ts index 6814d42a..53b4b054 100644 --- a/tdrive/backend/node/src/services/documents/services/index.ts +++ b/tdrive/backend/node/src/services/documents/services/index.ts @@ -632,12 +632,12 @@ export class DocumentsService { this.logger.error({ error: `${error}` }, "Failed to grant access to the drive item"); throw new CrudException("User does not have access to this item or its children", 401); } - + const path = await getPath(item.parent_id, this.repository, true, context); const previousParentId = item.parent_id; if ( (await isInTrash(item, this.repository, context)) || item.parent_id === this.TRASH || - (await getPath(item.parent_id, this.repository, true, context))[0].id === this.TRASH + path[0].id === this.TRASH ) { //This item is already in trash, we can delete it definitively @@ -681,22 +681,19 @@ export class DocumentsService { const creator = await this.userRepository.findOne({ id: item.creator }); if (creator.type === "anonymous") { const loadedCreators = new Map(); - const path = await getPath( - item.id, - this.repository, - true, - context, - async item => { - if (!item.creator) return true; - const user = - loadedCreators.get(item.creator) ?? - (await this.userRepository.findOne({ id: item.creator })); - loadedCreators.set(item.creator, user); - return user.type !== "anonymous"; - }, - true, - ); - const [firstOwnedItem] = path; + let firstOwnedItem: DriveFile | undefined; + for (let i = path.length - 1; i >= 0; i--) { + const item = path[i]; + if (!item.creator) continue; + const user = + loadedCreators.get(item.creator) ?? + (await this.userRepository.findOne({ id: item.creator })); + loadedCreators.set(item.creator, user); + if (user.type !== "anonymous") { + firstOwnedItem = item; + break; + } + } if (firstOwnedItem) { const firstKnownCreator = loadedCreators.get(firstOwnedItem.creator); const accessEntitiesWithoutUser = item.access_info.entities.filter( @@ -704,6 +701,8 @@ export class DocumentsService { ); item.access_info.entities = [ ...accessEntitiesWithoutUser, + // This is not functionally required, but creates an audit trace of what + // happened to this anonymously uploaded file { type: "user", id: firstKnownCreator.id,