@@ -8,7 +8,7 @@ import { HttpService } from '@nestjs/axios';
88import { Injectable } from '@nestjs/common' ;
99import { EventEmitter2 } from '@nestjs/event-emitter' ;
1010import { Presets , SingleBar } from 'cli-progress' ;
11- import { createWriteStream } from 'node:fs' ;
11+ import { createWriteStream , unlinkSync } from 'node:fs' ;
1212import { basename } from 'node:path' ;
1313import { firstValueFrom } from 'rxjs' ;
1414
@@ -31,9 +31,17 @@ export class DownloadManagerService {
3131 this . abortControllers [ downloadId ] [ destination ] . abort ( ) ;
3232 } ) ;
3333 delete this . abortControllers [ downloadId ] ;
34+
35+ const currentDownloadState = this . allDownloadStates . find (
36+ ( downloadState ) => downloadState . id === downloadId ,
37+ ) ;
3438 this . allDownloadStates = this . allDownloadStates . filter (
3539 ( downloadState ) => downloadState . id !== downloadId ,
3640 ) ;
41+
42+ if ( currentDownloadState ) {
43+ this . deleteDownloadStateFiles ( currentDownloadState ) ;
44+ }
3745 this . eventEmitter . emit ( 'download.event' , this . allDownloadStates ) ;
3846 }
3947
@@ -267,7 +275,6 @@ export class DownloadManagerService {
267275 }
268276
269277 private handleError ( error : Error , downloadId : string , destination : string ) {
270- console . log ( this . allDownloadStates , downloadId , destination ) ;
271278 delete this . abortControllers [ downloadId ] [ destination ] ;
272279 const currentDownloadState = this . allDownloadStates . find (
273280 ( downloadState ) => downloadState . id === downloadId ,
@@ -289,7 +296,15 @@ export class DownloadManagerService {
289296 this . allDownloadStates = this . allDownloadStates . filter (
290297 ( downloadState ) => downloadState . id !== downloadId ,
291298 ) ;
299+ this . deleteDownloadStateFiles ( currentDownloadState ) ;
292300 this . eventEmitter . emit ( 'download.event' , [ currentDownloadState ] ) ;
293301 this . eventEmitter . emit ( 'download.event' , this . allDownloadStates ) ;
294302 }
303+
304+ private deleteDownloadStateFiles ( downloadState : DownloadState ) {
305+ if ( ! downloadState . children ?. length ) return ;
306+ downloadState . children . forEach ( ( child ) => {
307+ unlinkSync ( child . id ) ;
308+ } ) ;
309+ }
295310}
0 commit comments