Skip to content

Commit 5bd1109

Browse files
authored
🐛 Fix delete root file/folder on upload cancel (#825)
1 parent 42ab4ec commit 5bd1109

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

Diff for: tdrive/frontend/src/app/features/files/services/file-upload-service.ts

+46-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class FileUploadService {
5050
public currentTaskId = '';
5151
public parentId = '';
5252
public uploadStatus = UploadStateEnum.Progress;
53+
private companyId = '';
5354
private recoilHandler: Function = () => undefined;
5455
private logger: Logger.Logger = Logger.getLogger('FileUploadService');
5556

@@ -131,6 +132,7 @@ class FileUploadService {
131132
) {
132133
// reset the upload status
133134
this.uploadStatus = UploadStateEnum.Progress;
135+
this.companyId = context.companyId;
134136

135137
const root = tree.tree;
136138
this.rootSizes = this.rootSizes = {
@@ -250,12 +252,16 @@ class FileUploadService {
250252
} as Partial<DriveItemVersion>;
251253

252254
// create the document
253-
const documentId = await DriveApiClient.create(context.companyId, { item, version });
254-
// assign the group id with the document id
255-
if (isFileRoot) {
256-
this.groupIds[root] = documentId.id;
257-
// set the id for the root
258-
this.notify();
255+
try {
256+
const documentId = await DriveApiClient.create(context.companyId, { item, version });
257+
// assign the group id with the document id
258+
if (isFileRoot) {
259+
this.groupIds[root] = documentId.id;
260+
// set the id for the root
261+
this.notify();
262+
}
263+
} catch (error) {
264+
logger.error('Error while creating document', error);
259265
}
260266
}
261267
},
@@ -427,6 +433,8 @@ class FileUploadService {
427433

428434
public cancelUpload() {
429435
this.uploadStatus = UploadStateEnum.Cancelled;
436+
// copy the group ids
437+
const rootItemIds = _.cloneDeep(this.groupIds);
430438

431439
// pause or resume the resumable tasks
432440
const fileToCancel = this.pendingFiles;
@@ -455,6 +463,17 @@ class FileUploadService {
455463
}
456464
}
457465

466+
// delete the roots in progress
467+
for (const rootItem of Object.keys(rootItemIds)) {
468+
const rootItemId = rootItemIds[rootItem];
469+
// check if the root completed skip it
470+
if (this.rootStates.completed[rootItem]) continue;
471+
this.deleteOneDriveItem({
472+
companyId: this.companyId,
473+
id: rootItemId,
474+
});
475+
}
476+
458477
// clean everything
459478
this.pendingFiles = [];
460479
this.groupedPendingFiles = {};
@@ -466,6 +485,7 @@ class FileUploadService {
466485

467486
public cancelRootUpload(id: string) {
468487
this.rootStates.cancelled[id] = true;
488+
const rootItemId = this.groupIds[id];
469489
// if it's 1 root, cancel the upload
470490
if (Object.keys(this.groupedPendingFiles).length === 1) {
471491
this.cancelUpload();
@@ -507,6 +527,12 @@ class FileUploadService {
507527
// remove the root id
508528
delete this.groupIds[id];
509529
this.notify();
530+
531+
// delete the root
532+
this.deleteOneDriveItem({
533+
companyId: this.companyId,
534+
id: rootItemId,
535+
});
510536
}
511537
}
512538

@@ -666,6 +692,20 @@ class FileUploadService {
666692
}
667693
}
668694

695+
public async deleteOneDriveItem({
696+
companyId,
697+
id,
698+
}: {
699+
companyId: string;
700+
id: string;
701+
}): Promise<void> {
702+
try {
703+
await DriveApiClient.remove(companyId, id);
704+
} catch (error) {
705+
logger.error('Error while deleting drive item ', error);
706+
}
707+
}
708+
669709
public download({ companyId, fileId }: { companyId: string; fileId: string }): Promise<Blob> {
670710
return FileUploadAPIClient.download({
671711
companyId: companyId,

0 commit comments

Comments
 (0)