Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit f1f8055

Browse files
chore: delete local file when abort download (#954)
1 parent 49c428a commit f1f8055

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { HttpService } from '@nestjs/axios';
88
import { Injectable } from '@nestjs/common';
99
import { EventEmitter2 } from '@nestjs/event-emitter';
1010
import { Presets, SingleBar } from 'cli-progress';
11-
import { createWriteStream } from 'node:fs';
11+
import { createWriteStream, unlinkSync } from 'node:fs';
1212
import { basename } from 'node:path';
1313
import { 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

Comments
 (0)