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