From 5bd11098ba6a85587fa342ffde818910f331a480 Mon Sep 17 00:00:00 2001 From: Montassar Ghanmy Date: Thu, 27 Feb 2025 10:38:21 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20delete=20root=20file/folde?= =?UTF-8?q?r=20on=20upload=20cancel=20(#825)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../files/services/file-upload-service.ts | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/tdrive/frontend/src/app/features/files/services/file-upload-service.ts b/tdrive/frontend/src/app/features/files/services/file-upload-service.ts index 260a8d90..4ae4218a 100644 --- a/tdrive/frontend/src/app/features/files/services/file-upload-service.ts +++ b/tdrive/frontend/src/app/features/files/services/file-upload-service.ts @@ -50,6 +50,7 @@ class FileUploadService { public currentTaskId = ''; public parentId = ''; public uploadStatus = UploadStateEnum.Progress; + private companyId = ''; private recoilHandler: Function = () => undefined; private logger: Logger.Logger = Logger.getLogger('FileUploadService'); @@ -131,6 +132,7 @@ class FileUploadService { ) { // reset the upload status this.uploadStatus = UploadStateEnum.Progress; + this.companyId = context.companyId; const root = tree.tree; this.rootSizes = this.rootSizes = { @@ -250,12 +252,16 @@ class FileUploadService { } as Partial; // create the document - const documentId = await DriveApiClient.create(context.companyId, { item, version }); - // assign the group id with the document id - if (isFileRoot) { - this.groupIds[root] = documentId.id; - // set the id for the root - this.notify(); + try { + const documentId = await DriveApiClient.create(context.companyId, { item, version }); + // assign the group id with the document id + if (isFileRoot) { + this.groupIds[root] = documentId.id; + // set the id for the root + this.notify(); + } + } catch (error) { + logger.error('Error while creating document', error); } } }, @@ -427,6 +433,8 @@ class FileUploadService { public cancelUpload() { this.uploadStatus = UploadStateEnum.Cancelled; + // copy the group ids + const rootItemIds = _.cloneDeep(this.groupIds); // pause or resume the resumable tasks const fileToCancel = this.pendingFiles; @@ -455,6 +463,17 @@ class FileUploadService { } } + // delete the roots in progress + for (const rootItem of Object.keys(rootItemIds)) { + const rootItemId = rootItemIds[rootItem]; + // check if the root completed skip it + if (this.rootStates.completed[rootItem]) continue; + this.deleteOneDriveItem({ + companyId: this.companyId, + id: rootItemId, + }); + } + // clean everything this.pendingFiles = []; this.groupedPendingFiles = {}; @@ -466,6 +485,7 @@ class FileUploadService { public cancelRootUpload(id: string) { this.rootStates.cancelled[id] = true; + const rootItemId = this.groupIds[id]; // if it's 1 root, cancel the upload if (Object.keys(this.groupedPendingFiles).length === 1) { this.cancelUpload(); @@ -507,6 +527,12 @@ class FileUploadService { // remove the root id delete this.groupIds[id]; this.notify(); + + // delete the root + this.deleteOneDriveItem({ + companyId: this.companyId, + id: rootItemId, + }); } } @@ -666,6 +692,20 @@ class FileUploadService { } } + public async deleteOneDriveItem({ + companyId, + id, + }: { + companyId: string; + id: string; + }): Promise { + try { + await DriveApiClient.remove(companyId, id); + } catch (error) { + logger.error('Error while deleting drive item ', error); + } + } + public download({ companyId, fileId }: { companyId: string; fileId: string }): Promise { return FileUploadAPIClient.download({ companyId: companyId,