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