|
| 1 | +import { |
| 2 | + DownloadItem, |
| 3 | + DownloadState, |
| 4 | + DownloadStatus, |
| 5 | + DownloadType, |
| 6 | +} from '@/domain/models/download.interface'; |
| 7 | +import { HttpService } from '@nestjs/axios'; |
| 8 | +import { Injectable } from '@nestjs/common'; |
| 9 | +import { EventEmitter2 } from '@nestjs/event-emitter'; |
| 10 | +import { createWriteStream } from 'node:fs'; |
| 11 | +import { firstValueFrom } from 'rxjs'; |
| 12 | + |
| 13 | +@Injectable() |
| 14 | +export class DownloadManagerService { |
| 15 | + private allDownloadStates: DownloadState[] = []; |
| 16 | + |
| 17 | + constructor( |
| 18 | + private readonly httpService: HttpService, |
| 19 | + private readonly eventEmitter: EventEmitter2, |
| 20 | + ) { |
| 21 | + // start emitting download state each 500ms |
| 22 | + setInterval(() => { |
| 23 | + this.eventEmitter.emit('download.event', this.allDownloadStates); |
| 24 | + }, 500); |
| 25 | + } |
| 26 | + |
| 27 | + async submitDownloadRequest( |
| 28 | + downloadId: string, |
| 29 | + title: string, |
| 30 | + downloadType: DownloadType, |
| 31 | + urlToDestination: Record<string, string>, |
| 32 | + ) { |
| 33 | + if ( |
| 34 | + this.allDownloadStates.find( |
| 35 | + (downloadState) => downloadState.id === downloadId, |
| 36 | + ) |
| 37 | + ) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const downloadItems: DownloadItem[] = Object.keys(urlToDestination).map( |
| 42 | + (url) => { |
| 43 | + const destination = urlToDestination[url]; |
| 44 | + const downloadItem: DownloadItem = { |
| 45 | + id: destination, |
| 46 | + time: { |
| 47 | + elapsed: 0, |
| 48 | + remaining: 0, |
| 49 | + }, |
| 50 | + size: { |
| 51 | + total: 0, |
| 52 | + transferred: 0, |
| 53 | + }, |
| 54 | + status: DownloadStatus.Downloading, |
| 55 | + }; |
| 56 | + |
| 57 | + return downloadItem; |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + const downloadState: DownloadState = { |
| 62 | + id: downloadId, |
| 63 | + title: title, |
| 64 | + type: downloadType, |
| 65 | + status: DownloadStatus.Downloading, |
| 66 | + children: downloadItems, |
| 67 | + }; |
| 68 | + |
| 69 | + this.allDownloadStates.push(downloadState); |
| 70 | + |
| 71 | + Object.keys(urlToDestination).forEach((url) => { |
| 72 | + const destination = urlToDestination[url]; |
| 73 | + this.downloadFile(downloadId, url, destination); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + private async downloadFile( |
| 78 | + downloadId: string, |
| 79 | + url: string, |
| 80 | + destination: string, |
| 81 | + ) { |
| 82 | + const response = await firstValueFrom( |
| 83 | + this.httpService.get(url, { |
| 84 | + responseType: 'stream', |
| 85 | + }), |
| 86 | + ); |
| 87 | + // check if response is success |
| 88 | + if (!response) { |
| 89 | + throw new Error('Failed to download model'); |
| 90 | + } |
| 91 | + |
| 92 | + const writer = createWriteStream(destination); |
| 93 | + const totalBytes = response.headers['content-length']; |
| 94 | + |
| 95 | + // update download state |
| 96 | + // TODO: separte this duplicate code to a function |
| 97 | + const currentDownloadState = this.allDownloadStates.find( |
| 98 | + (downloadState) => downloadState.id === downloadId, |
| 99 | + ); |
| 100 | + if (!currentDownloadState) { |
| 101 | + return; |
| 102 | + } |
| 103 | + const downloadItem = currentDownloadState?.children.find( |
| 104 | + (downloadItem) => downloadItem.id === destination, |
| 105 | + ); |
| 106 | + if (downloadItem) { |
| 107 | + downloadItem.size.total = totalBytes; |
| 108 | + } |
| 109 | + |
| 110 | + let transferredBytes = 0; |
| 111 | + |
| 112 | + writer.on('finish', () => { |
| 113 | + const currentDownloadState = this.allDownloadStates.find( |
| 114 | + (downloadState) => downloadState.id === downloadId, |
| 115 | + ); |
| 116 | + if (!currentDownloadState) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + // update current child status to downloaded, find by destination as id |
| 121 | + const downloadItem = currentDownloadState?.children.find( |
| 122 | + (downloadItem) => downloadItem.id === destination, |
| 123 | + ); |
| 124 | + if (downloadItem) { |
| 125 | + downloadItem.status = DownloadStatus.Downloaded; |
| 126 | + } |
| 127 | + |
| 128 | + const allChildrenDownloaded = currentDownloadState?.children.every( |
| 129 | + (downloadItem) => downloadItem.status === DownloadStatus.Downloaded, |
| 130 | + ); |
| 131 | + |
| 132 | + if (allChildrenDownloaded) { |
| 133 | + currentDownloadState.status = DownloadStatus.Downloaded; |
| 134 | + // TODO: notify if download success so that client can auto refresh |
| 135 | + // remove download state if all children is downloaded |
| 136 | + this.allDownloadStates = this.allDownloadStates.filter( |
| 137 | + (downloadState) => downloadState.id !== downloadId, |
| 138 | + ); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + writer.on('error', (error) => { |
| 143 | + const currentDownloadState = this.allDownloadStates.find( |
| 144 | + (downloadState) => downloadState.id === downloadId, |
| 145 | + ); |
| 146 | + if (!currentDownloadState) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + const downloadItem = currentDownloadState?.children.find( |
| 151 | + (downloadItem) => downloadItem.id === destination, |
| 152 | + ); |
| 153 | + if (downloadItem) { |
| 154 | + downloadItem.status = DownloadStatus.Error; |
| 155 | + downloadItem.error = error.message; |
| 156 | + } |
| 157 | + |
| 158 | + currentDownloadState.status = DownloadStatus.Error; |
| 159 | + currentDownloadState.error = error.message; |
| 160 | + |
| 161 | + // TODO: notify if download error |
| 162 | + // remove download state if all children is downloaded |
| 163 | + this.allDownloadStates = this.allDownloadStates.filter( |
| 164 | + (downloadState) => downloadState.id !== downloadId, |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + response.data.on('data', (chunk: any) => { |
| 169 | + transferredBytes += chunk.length; |
| 170 | + |
| 171 | + const currentDownloadState = this.allDownloadStates.find( |
| 172 | + (downloadState) => downloadState.id === downloadId, |
| 173 | + ); |
| 174 | + if (!currentDownloadState) return; |
| 175 | + |
| 176 | + const downloadItem = currentDownloadState?.children.find( |
| 177 | + (downloadItem) => downloadItem.id === destination, |
| 178 | + ); |
| 179 | + if (downloadItem) { |
| 180 | + downloadItem.size.transferred = transferredBytes; |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + response.data.pipe(writer); |
| 185 | + } |
| 186 | +} |
0 commit comments