2019-03-20 13:50:44 -06:00
|
|
|
import * as fs from 'fs';
|
2020-04-11 03:28:40 -06:00
|
|
|
import * as stream from 'stream';
|
|
|
|
import * as util from 'util';
|
2020-04-09 08:42:23 -06:00
|
|
|
import fetch from 'node-fetch';
|
2020-04-12 05:32:34 -06:00
|
|
|
import { getAgentByUrl } from './fetch';
|
2020-04-11 03:28:40 -06:00
|
|
|
import { AbortController } from 'abort-controller';
|
2019-03-20 13:50:44 -06:00
|
|
|
import config from '../config';
|
2019-11-24 01:09:32 -07:00
|
|
|
import * as chalk from 'chalk';
|
2019-03-20 13:50:44 -06:00
|
|
|
import Logger from '../services/logger';
|
|
|
|
|
2020-04-11 03:28:40 -06:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2019-03-20 13:50:44 -06:00
|
|
|
export async function downloadUrl(url: string, path: string) {
|
2019-06-30 12:25:31 -06:00
|
|
|
const logger = new Logger('download');
|
2019-03-20 13:50:44 -06:00
|
|
|
|
2020-04-09 08:42:23 -06:00
|
|
|
logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
2020-04-11 03:28:40 -06:00
|
|
|
const controller = new AbortController();
|
|
|
|
setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, 11 * 1000);
|
2020-04-09 08:42:23 -06:00
|
|
|
|
|
|
|
const response = await fetch(new URL(url).href, {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': config.userAgent
|
|
|
|
},
|
|
|
|
timeout: 10 * 1000,
|
2020-04-11 03:28:40 -06:00
|
|
|
signal: controller.signal,
|
2020-04-12 05:32:34 -06:00
|
|
|
agent: getAgentByUrl,
|
2020-04-09 08:42:23 -06:00
|
|
|
});
|
2019-03-20 13:50:44 -06:00
|
|
|
|
2020-04-11 03:28:40 -06:00
|
|
|
if (!response.ok) {
|
|
|
|
logger.error(`Got ${response.status} (${url})`);
|
|
|
|
throw response.status;
|
|
|
|
}
|
2019-03-20 13:50:44 -06:00
|
|
|
|
2020-04-11 03:28:40 -06:00
|
|
|
await pipeline(response.body, fs.createWriteStream(path));
|
2019-03-20 13:50:44 -06:00
|
|
|
|
2020-04-11 03:28:40 -06:00
|
|
|
logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
2019-03-20 13:50:44 -06:00
|
|
|
}
|