🐛 Fix delete root file/folder on upload cancel (#825)
This commit is contained in:
@@ -50,6 +50,7 @@ class FileUploadService {
|
|||||||
public currentTaskId = '';
|
public currentTaskId = '';
|
||||||
public parentId = '';
|
public parentId = '';
|
||||||
public uploadStatus = UploadStateEnum.Progress;
|
public uploadStatus = UploadStateEnum.Progress;
|
||||||
|
private companyId = '';
|
||||||
private recoilHandler: Function = () => undefined;
|
private recoilHandler: Function = () => undefined;
|
||||||
private logger: Logger.Logger = Logger.getLogger('FileUploadService');
|
private logger: Logger.Logger = Logger.getLogger('FileUploadService');
|
||||||
|
|
||||||
@@ -131,6 +132,7 @@ class FileUploadService {
|
|||||||
) {
|
) {
|
||||||
// reset the upload status
|
// reset the upload status
|
||||||
this.uploadStatus = UploadStateEnum.Progress;
|
this.uploadStatus = UploadStateEnum.Progress;
|
||||||
|
this.companyId = context.companyId;
|
||||||
|
|
||||||
const root = tree.tree;
|
const root = tree.tree;
|
||||||
this.rootSizes = this.rootSizes = {
|
this.rootSizes = this.rootSizes = {
|
||||||
@@ -250,12 +252,16 @@ class FileUploadService {
|
|||||||
} as Partial<DriveItemVersion>;
|
} as Partial<DriveItemVersion>;
|
||||||
|
|
||||||
// create the document
|
// create the document
|
||||||
const documentId = await DriveApiClient.create(context.companyId, { item, version });
|
try {
|
||||||
// assign the group id with the document id
|
const documentId = await DriveApiClient.create(context.companyId, { item, version });
|
||||||
if (isFileRoot) {
|
// assign the group id with the document id
|
||||||
this.groupIds[root] = documentId.id;
|
if (isFileRoot) {
|
||||||
// set the id for the root
|
this.groupIds[root] = documentId.id;
|
||||||
this.notify();
|
// 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() {
|
public cancelUpload() {
|
||||||
this.uploadStatus = UploadStateEnum.Cancelled;
|
this.uploadStatus = UploadStateEnum.Cancelled;
|
||||||
|
// copy the group ids
|
||||||
|
const rootItemIds = _.cloneDeep(this.groupIds);
|
||||||
|
|
||||||
// pause or resume the resumable tasks
|
// pause or resume the resumable tasks
|
||||||
const fileToCancel = this.pendingFiles;
|
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
|
// clean everything
|
||||||
this.pendingFiles = [];
|
this.pendingFiles = [];
|
||||||
this.groupedPendingFiles = {};
|
this.groupedPendingFiles = {};
|
||||||
@@ -466,6 +485,7 @@ class FileUploadService {
|
|||||||
|
|
||||||
public cancelRootUpload(id: string) {
|
public cancelRootUpload(id: string) {
|
||||||
this.rootStates.cancelled[id] = true;
|
this.rootStates.cancelled[id] = true;
|
||||||
|
const rootItemId = this.groupIds[id];
|
||||||
// if it's 1 root, cancel the upload
|
// if it's 1 root, cancel the upload
|
||||||
if (Object.keys(this.groupedPendingFiles).length === 1) {
|
if (Object.keys(this.groupedPendingFiles).length === 1) {
|
||||||
this.cancelUpload();
|
this.cancelUpload();
|
||||||
@@ -507,6 +527,12 @@ class FileUploadService {
|
|||||||
// remove the root id
|
// remove the root id
|
||||||
delete this.groupIds[id];
|
delete this.groupIds[id];
|
||||||
this.notify();
|
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<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> {
|
public download({ companyId, fileId }: { companyId: string; fileId: string }): Promise<Blob> {
|
||||||
return FileUploadAPIClient.download({
|
return FileUploadAPIClient.download({
|
||||||
companyId: companyId,
|
companyId: companyId,
|
||||||
|
|||||||
Reference in New Issue
Block a user