@@ -50,6 +50,7 @@ class FileUploadService {
50
50
public currentTaskId = '' ;
51
51
public parentId = '' ;
52
52
public uploadStatus = UploadStateEnum . Progress ;
53
+ private companyId = '' ;
53
54
private recoilHandler : Function = ( ) => undefined ;
54
55
private logger : Logger . Logger = Logger . getLogger ( 'FileUploadService' ) ;
55
56
@@ -131,6 +132,7 @@ class FileUploadService {
131
132
) {
132
133
// reset the upload status
133
134
this . uploadStatus = UploadStateEnum . Progress ;
135
+ this . companyId = context . companyId ;
134
136
135
137
const root = tree . tree ;
136
138
this . rootSizes = this . rootSizes = {
@@ -250,12 +252,16 @@ class FileUploadService {
250
252
} as Partial < DriveItemVersion > ;
251
253
252
254
// 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 ) ;
259
265
}
260
266
}
261
267
} ,
@@ -427,6 +433,8 @@ class FileUploadService {
427
433
428
434
public cancelUpload ( ) {
429
435
this . uploadStatus = UploadStateEnum . Cancelled ;
436
+ // copy the group ids
437
+ const rootItemIds = _ . cloneDeep ( this . groupIds ) ;
430
438
431
439
// pause or resume the resumable tasks
432
440
const fileToCancel = this . pendingFiles ;
@@ -455,6 +463,17 @@ class FileUploadService {
455
463
}
456
464
}
457
465
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
+
458
477
// clean everything
459
478
this . pendingFiles = [ ] ;
460
479
this . groupedPendingFiles = { } ;
@@ -466,6 +485,7 @@ class FileUploadService {
466
485
467
486
public cancelRootUpload ( id : string ) {
468
487
this . rootStates . cancelled [ id ] = true ;
488
+ const rootItemId = this . groupIds [ id ] ;
469
489
// if it's 1 root, cancel the upload
470
490
if ( Object . keys ( this . groupedPendingFiles ) . length === 1 ) {
471
491
this . cancelUpload ( ) ;
@@ -507,6 +527,12 @@ class FileUploadService {
507
527
// remove the root id
508
528
delete this . groupIds [ id ] ;
509
529
this . notify ( ) ;
530
+
531
+ // delete the root
532
+ this . deleteOneDriveItem ( {
533
+ companyId : this . companyId ,
534
+ id : rootItemId ,
535
+ } ) ;
510
536
}
511
537
}
512
538
@@ -666,6 +692,20 @@ class FileUploadService {
666
692
}
667
693
}
668
694
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
+
669
709
public download ( { companyId, fileId } : { companyId : string ; fileId : string } ) : Promise < Blob > {
670
710
return FileUploadAPIClient . download ( {
671
711
companyId : companyId ,
0 commit comments