mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-11-16 16:57:14 +01:00
36b9a0d42f
* プロキシの除外ホスト * オブジェクトストレージとの通信にProxyを使うかを選択できるように * fix lint * コメント Co-authored-by: rinsuki <428rinsuki+git@gmail.com>
39 lines
1 KiB
TypeScript
39 lines
1 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as stream from 'stream';
|
|
import * as util from 'util';
|
|
import fetch from 'node-fetch';
|
|
import { getAgentByUrl } 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: getAgentByUrl,
|
|
});
|
|
|
|
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)}`);
|
|
}
|