@@ -25,6 +25,7 @@ let downProgressInterval = undefined;
25
25
let totalDone = 0 ;
26
26
let totalToDo = 0 ;
27
27
let aborted = false ;
28
+ let cancelled = false ;
28
29
29
30
// Use a 50 MiB segment when downloading
30
31
const DOWNLOAD_SEGMENT_SIZE = 1024 * 1024 * 50 ;
@@ -66,6 +67,7 @@ if (inServiceWorker) {
66
67
// Create a download session
67
68
function createDownloadSession ( container , handle , archive ) {
68
69
aborted = false ; //reset
70
+ cancelled = false ;
69
71
70
72
let keypairPtr = Module . ccall (
71
73
"create_keypair" ,
@@ -292,6 +294,7 @@ class FileSlicer {
292
294
await this . getStart ( ) ;
293
295
294
296
while ( ! this . done ) {
297
+ if ( cancelled ) return ;
295
298
if ( this . output instanceof WritableStream ) {
296
299
await this . output . write ( this . chunk ) ;
297
300
} else {
@@ -321,6 +324,7 @@ class FileSlicer {
321
324
322
325
// Slice the file and write decrypted content to output
323
326
while ( ! this . done ) {
327
+ if ( cancelled ) return ;
324
328
await this . getSlice ( ) ;
325
329
326
330
if ( this . output instanceof WritableStream ) {
@@ -361,6 +365,13 @@ class FileSlicer {
361
365
) ;
362
366
return true ;
363
367
}
368
+
369
+ async abortStream ( ) {
370
+ if ( this . output instanceof WritableStream ) {
371
+ //stop writing to stream
372
+ await this . output . abort ( ) ;
373
+ }
374
+ }
364
375
}
365
376
366
377
function clear ( ) {
@@ -372,7 +383,8 @@ function clear() {
372
383
totalToDo = 0 ;
373
384
}
374
385
375
- function abortDownloads ( direct , abortReason ) {
386
+ async function abortDownloads ( direct , abortReason ) {
387
+ aborted = true ;
376
388
const msg = {
377
389
eventType : "abort" ,
378
390
reason : abortReason ,
@@ -386,7 +398,7 @@ function abortDownloads(direct, abortReason) {
386
398
} ) ;
387
399
}
388
400
clear ( ) ;
389
- aborted = true ;
401
+
390
402
for ( let container in downloads ) {
391
403
finishDownloadSession ( container ) ;
392
404
}
@@ -511,10 +523,18 @@ async function beginDownloadInSession(
511
523
res = await slicer . sliceFile ( ) . catch ( ( ) => {
512
524
return false ;
513
525
} ) ;
514
- if ( ! res ) {
515
- if ( ! aborted ) abortDownloads ( ! inServiceWorker , "error" ) ;
516
- return ;
526
+ }
527
+ if ( ! res ) {
528
+ await slicer . abortStream ( ) . then ( async ( ) => {
529
+ //remove temporary files
530
+ if ( downloads [ container ] . direct ) {
531
+ await downloads [ container ] . handle . remove ( ) ;
532
+ }
533
+ } ) ;
534
+ if ( ! aborted ) {
535
+ await abortDownloads ( ! inServiceWorker , cancelled ? "cancel" : "error" ) ;
517
536
}
537
+ return ;
518
538
}
519
539
}
520
540
@@ -711,7 +731,7 @@ self.addEventListener("message", async (e) => {
711
731
clear ( ) ;
712
732
break ;
713
733
case "cancel" :
714
- abortDownloads ( ! inServiceWorker , "cancel" ) ;
734
+ cancelled = true ;
715
735
break ;
716
736
}
717
737
} ) ;
0 commit comments