Skip to content

Commit

Permalink
🐛 Fix delete root file/folder on upload cancel (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
MontaGhanmy authored Feb 27, 2025
1 parent 42ab4ec commit 5bd1109
Showing 1 changed file with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -250,12 +252,16 @@ class FileUploadService {
} as Partial<DriveItemVersion>;

// 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);
}
}
},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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();
Expand Down Expand Up @@ -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,
});
}
}

Expand Down Expand Up @@ -666,6 +692,20 @@ class FileUploadService {
}
}

public async deleteOneDriveItem({
companyId,
id,
}: {
companyId: string;
id: string;
}): Promise<void> {
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<Blob> {
return FileUploadAPIClient.download({
companyId: companyId,
Expand Down

0 comments on commit 5bd1109

Please sign in to comment.