MCPcopy Create free account
hub / github.com/FlashpointProject/launcher / downloadFile

Function downloadFile

src/shared/Util.ts:417–451  ·  view source on GitHub ↗
(axios: axiosImport.AxiosInstance, url: string, filePath: string, abortSignal?: AbortSignal, onProgress?: (percent: number) => void, onDetails?: (details: DownloadDetails) => void, options?: axiosImport.AxiosRequestConfig)

Source from the content-addressed store, hash-verified

415}
416
417export 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
453export function generateTagFilterGroup(tags?: string[]): TagFilterGroup {
454 return {

Callers 2

downloadGameDataFunction · 0.90
registerRequestCallbacksFunction · 0.90

Calls 6

throttleFunction · 0.90
getMethod · 0.80
onMethod · 0.65
closeMethod · 0.65
onDetailsFunction · 0.50
onProgressFunction · 0.50

Tested by

no test coverage detected