1 import * as Bluebird from 'bluebird'
2 import { createWriteStream, remove } from 'fs-extra'
3 import * as request from 'request'
4 import { ACTIVITY_PUB, CONFIG } from '../initializers'
5 import { processImage } from './image-utils'
6 import { join } from 'path'
7 import { logger } from './logger'
9 function doRequest <T> (
10 requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean },
11 bodyKBLimit = 1000 // 1MB
12 ): Bluebird<{ response: request.RequestResponse, body: T }> {
13 if (requestOptions.activityPub === true) {
14 if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {}
15 requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
18 return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => {
19 request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
20 .on('data', onRequestDataLengthCheck(bodyKBLimit))
24 function doRequestAndSaveToFile (
25 requestOptions: request.CoreOptions & request.UriOptions,
27 bodyKBLimit = 10000 // 10MB
29 return new Bluebird<void>((res, rej) => {
30 const file = createWriteStream(destPath)
31 file.on('finish', () => res())
33 request(requestOptions)
34 .on('data', onRequestDataLengthCheck(bodyKBLimit))
39 .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
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)
49 await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath)
51 const destPath = join(destDir, destName)
52 await processImage({ path: tmpPath }, destPath, size)
55 // ---------------------------------------------------------------------------
59 doRequestAndSaveToFile,
63 // ---------------------------------------------------------------------------
65 // Thanks to https://github.com/request/request/issues/2470#issuecomment-268929907 <3
66 function onRequestDataLengthCheck (bodyKBLimit: number) {
68 const bytesLimit = bodyKBLimit * 1000
70 return function (chunk) {
71 bufferLength += chunk.length
72 if (bufferLength > bytesLimit) {
75 const error = new Error(`Response was too large - aborted after ${bytesLimit} bytes.`)
76 this.emit('error', error)