diff options
Diffstat (limited to 'src/misc/download-url.ts')
| -rw-r--r-- | src/misc/download-url.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/misc/download-url.ts b/src/misc/download-url.ts new file mode 100644 index 0000000000..3f42fb3bef --- /dev/null +++ b/src/misc/download-url.ts @@ -0,0 +1,39 @@ +import * as fs from 'fs'; +import * as stream from 'stream'; +import * as util from 'util'; +import fetch from 'node-fetch'; +import { httpAgent, httpsAgent } from './fetch'; +import { AbortController } from 'abort-controller'; +import config from '../config'; +import * as chalk from 'chalk'; +import Logger from '../services/logger'; + +const pipeline = util.promisify(stream.pipeline); + +export async function downloadUrl(url: string, path: string) { + const logger = new Logger('download'); + + logger.info(`Downloading ${chalk.cyan(url)} ...`); + const controller = new AbortController(); + setTimeout(() => { + controller.abort(); + }, 11 * 1000); + + const response = await fetch(new URL(url).href, { + headers: { + 'User-Agent': config.userAgent + }, + timeout: 10 * 1000, + signal: controller.signal, + agent: u => u.protocol == 'http:' ? httpAgent : httpsAgent, + }); + + if (!response.ok) { + logger.error(`Got ${response.status} (${url})`); + throw response.status; + } + + await pipeline(response.body, fs.createWriteStream(path)); + + logger.succ(`Download finished: ${chalk.cyan(url)}`); +} |