From 715c91846a98d825ea8e5c77e42b4bfb50500d95 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Wed, 31 Jul 2024 21:41:12 +0700 Subject: [PATCH 1/2] chore: delete local file when abort download --- .../download-manager/download-manager.service.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts index 1df006517..cbef08e49 100644 --- a/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts +++ b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts @@ -8,7 +8,7 @@ import { HttpService } from '@nestjs/axios'; import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Presets, SingleBar } from 'cli-progress'; -import { createWriteStream } from 'node:fs'; +import { createWriteStream, unlinkSync } from 'node:fs'; import { basename } from 'node:path'; import { firstValueFrom } from 'rxjs'; @@ -31,9 +31,19 @@ export class DownloadManagerService { this.abortControllers[downloadId][destination].abort(); }); delete this.abortControllers[downloadId]; + + const currentDownloadState = this.allDownloadStates.find( + (downloadState) => downloadState.id === downloadId, + ); this.allDownloadStates = this.allDownloadStates.filter( (downloadState) => downloadState.id !== downloadId, ); + + if (currentDownloadState) { + currentDownloadState.children.forEach((child) => { + unlinkSync(child.id); + }); + } this.eventEmitter.emit('download.event', this.allDownloadStates); } From 0fc429a0055c17284ff7262e550c3bf12b7397ba Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Wed, 31 Jul 2024 21:46:12 +0700 Subject: [PATCH 2/2] chore: refactor --- .../download-manager.service.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts index cbef08e49..0a4ea4018 100644 --- a/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts +++ b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts @@ -38,11 +38,9 @@ export class DownloadManagerService { this.allDownloadStates = this.allDownloadStates.filter( (downloadState) => downloadState.id !== downloadId, ); - - if (currentDownloadState) { - currentDownloadState.children.forEach((child) => { - unlinkSync(child.id); - }); + + if (currentDownloadState){ + this.deleteDownloadStateFiles(currentDownloadState); } this.eventEmitter.emit('download.event', this.allDownloadStates); } @@ -277,7 +275,6 @@ export class DownloadManagerService { } private handleError(error: Error, downloadId: string, destination: string) { - console.log(this.allDownloadStates, downloadId, destination); delete this.abortControllers[downloadId][destination]; const currentDownloadState = this.allDownloadStates.find( (downloadState) => downloadState.id === downloadId, @@ -299,7 +296,15 @@ export class DownloadManagerService { this.allDownloadStates = this.allDownloadStates.filter( (downloadState) => downloadState.id !== downloadId, ); + this.deleteDownloadStateFiles(currentDownloadState); this.eventEmitter.emit('download.event', [currentDownloadState]); this.eventEmitter.emit('download.event', this.allDownloadStates); } + + private deleteDownloadStateFiles(downloadState: DownloadState) { + if(!downloadState.children?.length) return; + downloadState.children.forEach((child) => { + unlinkSync(child.id); + }); + } }