]>
Commit | Line | Data |
---|---|---|
6b2ef589 | 1 | import * as Bluebird from 'bluebird' |
bfe2ef6b | 2 | import { createWriteStream, remove } from 'fs-extra' |
571389d4 | 3 | import * as request from 'request' |
6040f87d | 4 | import { ACTIVITY_PUB, CONFIG } from '../initializers' |
58d515e3 | 5 | import { processImage } from './image-utils' |
6040f87d | 6 | import { join } from 'path' |
bfe2ef6b | 7 | import { logger } from './logger' |
da854ddd | 8 | |
5c6d985f | 9 | function doRequest <T> ( |
bfe2ef6b C |
10 | requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }, |
11 | bodyKBLimit = 1000 // 1MB | |
4c280004 | 12 | ): Bluebird<{ response: request.RequestResponse, body: T }> { |
da854ddd C |
13 | if (requestOptions.activityPub === true) { |
14 | if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {} | |
15 | requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER | |
16 | } | |
e4f97bab | 17 | |
5c6d985f | 18 | return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => { |
e4f97bab | 19 | request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body })) |
bfe2ef6b | 20 | .on('data', onRequestDataLengthCheck(bodyKBLimit)) |
e4f97bab C |
21 | }) |
22 | } | |
dac0a531 | 23 | |
bfe2ef6b C |
24 | function doRequestAndSaveToFile ( |
25 | requestOptions: request.CoreOptions & request.UriOptions, | |
26 | destPath: string, | |
27 | bodyKBLimit = 10000 // 10MB | |
28 | ) { | |
02988fdc C |
29 | return new Bluebird<void>((res, rej) => { |
30 | const file = createWriteStream(destPath) | |
31 | file.on('finish', () => res()) | |
32 | ||
0d0e8dd0 | 33 | request(requestOptions) |
bfe2ef6b C |
34 | .on('data', onRequestDataLengthCheck(bodyKBLimit)) |
35 | .on('error', err => { | |
36 | file.close() | |
37 | ||
38 | remove(destPath) | |
39 | .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err })) | |
40 | ||
41 | return rej(err) | |
42 | }) | |
02988fdc | 43 | .pipe(file) |
0d0e8dd0 C |
44 | }) |
45 | } | |
46 | ||
6040f87d C |
47 | async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) { |
48 | const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName) | |
58d515e3 C |
49 | await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath) |
50 | ||
6040f87d | 51 | const destPath = join(destDir, destName) |
58d515e3 C |
52 | await processImage({ path: tmpPath }, destPath, size) |
53 | } | |
54 | ||
9f10b292 | 55 | // --------------------------------------------------------------------------- |
dac0a531 | 56 | |
65fcc311 | 57 | export { |
e4f97bab | 58 | doRequest, |
58d515e3 C |
59 | doRequestAndSaveToFile, |
60 | downloadImage | |
65fcc311 | 61 | } |
bfe2ef6b C |
62 | |
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | // Thanks to https://github.com/request/request/issues/2470#issuecomment-268929907 <3 | |
66 | function onRequestDataLengthCheck (bodyKBLimit: number) { | |
67 | let bufferLength = 0 | |
68 | const bytesLimit = bodyKBLimit * 1000 | |
69 | ||
70 | return function (chunk) { | |
71 | bufferLength += chunk.length | |
72 | if (bufferLength > bytesLimit) { | |
73 | this.abort() | |
74 | ||
75 | const error = new Error(`Response was too large - aborted after ${bytesLimit} bytes.`) | |
76 | this.emit('error', error) | |
77 | } | |
78 | } | |
79 | } |