| 415 | } |
| 416 | |
| 417 | export async function downloadFile(axios: axiosImport.AxiosInstance, url: string, filePath: string, abortSignal?: AbortSignal, onProgress?: (percent: number) => void, onDetails?: (details: DownloadDetails) => void, options?: axiosImport.AxiosRequestConfig): Promise<number> { |
| 418 | try { |
| 419 | const res = await axios.get(url, { |
| 420 | ...options, |
| 421 | responseType: 'stream', |
| 422 | signal: abortSignal |
| 423 | }); |
| 424 | let progress = 0; |
| 425 | const contentLength = res.headers['content-length']; |
| 426 | onDetails && onDetails({ downloadSize: contentLength }); |
| 427 | const progressThrottle = onProgress && throttle(onProgress, 200); |
| 428 | const fileStream = fs.createWriteStream(filePath); |
| 429 | return new Promise<number>((resolve, reject) => { |
| 430 | fileStream.on('close', () => { |
| 431 | resolve(res.status); |
| 432 | }); |
| 433 | res.data.on('end', () => { |
| 434 | fileStream.close(); |
| 435 | onProgress && onProgress(100); |
| 436 | }); |
| 437 | res.data.on('data', (chunk: any) => { |
| 438 | progress = progress + chunk.length; |
| 439 | progressThrottle && progressThrottle((progress / contentLength) * 100); |
| 440 | fileStream.write(chunk); |
| 441 | }); |
| 442 | res.data.on('error', async () => { |
| 443 | fileStream.close(); |
| 444 | await fs.promises.unlink(filePath); |
| 445 | reject(res.status); |
| 446 | }); |
| 447 | }); |
| 448 | } catch (error) { |
| 449 | throw `Error opening Axios request. Do you have internet access?: ${error}`; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | export function generateTagFilterGroup(tags?: string[]): TagFilterGroup { |
| 454 | return { |