2018-01-15 04:10:46 -06:00
|
|
|
import * as Bluebird from 'bluebird'
|
2019-02-21 10:19:16 -06:00
|
|
|
import { createWriteStream, remove } from 'fs-extra'
|
2017-11-10 10:27:49 -06:00
|
|
|
import * as request from 'request'
|
2019-07-16 07:52:24 -05:00
|
|
|
import { ACTIVITY_PUB, WEBSERVER } from '../initializers/constants'
|
2018-11-16 09:48:17 -06:00
|
|
|
import { processImage } from './image-utils'
|
2018-12-04 09:02:49 -06:00
|
|
|
import { join } from 'path'
|
2019-02-21 10:19:16 -06:00
|
|
|
import { logger } from './logger'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../initializers/config'
|
2017-12-28 04:16:08 -06:00
|
|
|
|
2019-07-16 07:52:24 -05:00
|
|
|
const packageJSON = require('../../../package.json')
|
|
|
|
|
2018-11-14 08:01:28 -06:00
|
|
|
function doRequest <T> (
|
2019-02-21 10:19:16 -06:00
|
|
|
requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean },
|
|
|
|
bodyKBLimit = 1000 // 1MB
|
2019-02-07 08:08:19 -06:00
|
|
|
): Bluebird<{ response: request.RequestResponse, body: T }> {
|
2019-07-16 07:52:24 -05:00
|
|
|
if (!(requestOptions.headers)) requestOptions.headers = {}
|
|
|
|
requestOptions.headers['User-Agent'] = getUserAgent()
|
|
|
|
|
2017-12-28 04:16:08 -06:00
|
|
|
if (requestOptions.activityPub === true) {
|
|
|
|
requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
|
|
|
|
}
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2018-11-14 08:01:28 -06:00
|
|
|
return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => {
|
2017-11-09 10:51:58 -06:00
|
|
|
request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
|
2019-02-21 10:19:16 -06:00
|
|
|
.on('data', onRequestDataLengthCheck(bodyKBLimit))
|
2017-11-09 10:51:58 -06:00
|
|
|
})
|
|
|
|
}
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2019-02-21 10:19:16 -06:00
|
|
|
function doRequestAndSaveToFile (
|
|
|
|
requestOptions: request.CoreOptions & request.UriOptions,
|
|
|
|
destPath: string,
|
|
|
|
bodyKBLimit = 10000 // 10MB
|
|
|
|
) {
|
2019-07-16 07:52:24 -05:00
|
|
|
if (!requestOptions.headers) requestOptions.headers = {}
|
|
|
|
requestOptions.headers['User-Agent'] = getUserAgent()
|
|
|
|
|
2018-02-15 11:40:24 -06:00
|
|
|
return new Bluebird<void>((res, rej) => {
|
|
|
|
const file = createWriteStream(destPath)
|
|
|
|
file.on('finish', () => res())
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
request(requestOptions)
|
2019-02-21 10:19:16 -06:00
|
|
|
.on('data', onRequestDataLengthCheck(bodyKBLimit))
|
|
|
|
.on('error', err => {
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
remove(destPath)
|
|
|
|
.catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
|
|
|
|
|
|
|
|
return rej(err)
|
|
|
|
})
|
2018-02-15 11:40:24 -06:00
|
|
|
.pipe(file)
|
2017-11-10 07:34:45 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:02:49 -06:00
|
|
|
async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) {
|
|
|
|
const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
|
2018-11-16 09:48:17 -06:00
|
|
|
await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath)
|
|
|
|
|
2018-12-04 09:02:49 -06:00
|
|
|
const destPath = join(destDir, destName)
|
2019-04-15 08:04:02 -05:00
|
|
|
|
|
|
|
try {
|
2019-04-24 02:56:25 -05:00
|
|
|
await processImage(tmpPath, destPath, size)
|
2019-04-15 08:04:02 -05:00
|
|
|
} catch (err) {
|
|
|
|
await remove(tmpPath)
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2018-11-16 09:48:17 -06:00
|
|
|
}
|
|
|
|
|
2019-07-16 07:52:24 -05:00
|
|
|
function getUserAgent () {
|
|
|
|
return `PeerTube/${packageJSON.version} (+${WEBSERVER.URL})`
|
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
2017-11-09 10:51:58 -06:00
|
|
|
doRequest,
|
2018-11-16 09:48:17 -06:00
|
|
|
doRequestAndSaveToFile,
|
|
|
|
downloadImage
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
2019-02-21 10:19:16 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Thanks to https://github.com/request/request/issues/2470#issuecomment-268929907 <3
|
|
|
|
function onRequestDataLengthCheck (bodyKBLimit: number) {
|
|
|
|
let bufferLength = 0
|
|
|
|
const bytesLimit = bodyKBLimit * 1000
|
|
|
|
|
|
|
|
return function (chunk) {
|
|
|
|
bufferLength += chunk.length
|
|
|
|
if (bufferLength > bytesLimit) {
|
|
|
|
this.abort()
|
|
|
|
|
|
|
|
const error = new Error(`Response was too large - aborted after ${bytesLimit} bytes.`)
|
|
|
|
this.emit('error', error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|