2019-02-21 10:19:16 -06:00
|
|
|
import { createWriteStream, remove } from 'fs-extra'
|
2021-03-08 07:24:11 -06:00
|
|
|
import got, { CancelableRequest, Options as GotOptions } from 'got'
|
|
|
|
import { join } from 'path'
|
|
|
|
import { CONFIG } from '../initializers/config'
|
2019-07-17 03:03:55 -05:00
|
|
|
import { ACTIVITY_PUB, PEERTUBE_VERSION, WEBSERVER } from '../initializers/constants'
|
2021-03-08 07:24:11 -06:00
|
|
|
import { pipelinePromise } from './core-utils'
|
2018-11-16 09:48:17 -06:00
|
|
|
import { processImage } from './image-utils'
|
2019-02-21 10:19:16 -06:00
|
|
|
import { logger } from './logger'
|
2017-12-28 04:16:08 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
const httpSignature = require('http-signature')
|
|
|
|
|
|
|
|
type PeerTubeRequestOptions = {
|
|
|
|
activityPub?: boolean
|
|
|
|
bodyKBLimit?: number // 1MB
|
|
|
|
httpSignature?: {
|
|
|
|
algorithm: string
|
|
|
|
authorizationHeaderName: string
|
|
|
|
keyId: string
|
|
|
|
key: string
|
|
|
|
headers: string[]
|
|
|
|
}
|
|
|
|
jsonResponse?: boolean
|
|
|
|
} & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
|
|
|
|
|
|
|
|
const peertubeGot = got.extend({
|
|
|
|
headers: {
|
|
|
|
'user-agent': getUserAgent()
|
|
|
|
},
|
|
|
|
|
|
|
|
handlers: [
|
|
|
|
(options, next) => {
|
|
|
|
const promiseOrStream = next(options) as CancelableRequest<any>
|
|
|
|
const bodyKBLimit = options.context?.bodyKBLimit
|
|
|
|
if (!bodyKBLimit) throw new Error('No KB limit for this request')
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
|
|
promiseOrStream.on('downloadProgress', progress => {
|
|
|
|
if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) {
|
|
|
|
promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`)
|
|
|
|
}
|
|
|
|
})
|
2019-07-16 07:52:24 -05:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
return promiseOrStream
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
hooks: {
|
|
|
|
beforeRequest: [
|
|
|
|
options => {
|
|
|
|
const headers = options.headers || {}
|
|
|
|
headers['host'] = options.url.host
|
|
|
|
},
|
|
|
|
|
|
|
|
options => {
|
|
|
|
const httpSignatureOptions = options.context?.httpSignature
|
|
|
|
|
|
|
|
if (httpSignatureOptions) {
|
|
|
|
const method = options.method ?? 'GET'
|
|
|
|
const path = options.path ?? options.url.pathname
|
|
|
|
|
|
|
|
if (!method || !path) {
|
|
|
|
throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpSignature.signRequest({
|
|
|
|
getHeader: function (header) {
|
|
|
|
return options.headers[header]
|
|
|
|
},
|
|
|
|
|
|
|
|
setHeader: function (header, value) {
|
|
|
|
options.headers[header] = value
|
|
|
|
},
|
|
|
|
|
|
|
|
method,
|
|
|
|
path
|
|
|
|
}, httpSignatureOptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-12-28 04:16:08 -06:00
|
|
|
}
|
2021-03-08 07:24:11 -06:00
|
|
|
})
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
|
|
|
|
const gotOptions = buildGotOptions(options)
|
|
|
|
|
|
|
|
return peertubeGot(url, gotOptions)
|
|
|
|
.catch(err => { throw buildRequestError(err) })
|
|
|
|
}
|
|
|
|
|
|
|
|
function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
|
|
|
|
const gotOptions = buildGotOptions(options)
|
|
|
|
|
|
|
|
return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
|
|
|
|
.catch(err => { throw buildRequestError(err) })
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
2016-02-05 11:03:20 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
async function doRequestAndSaveToFile (
|
|
|
|
url: string,
|
2019-02-21 10:19:16 -06:00
|
|
|
destPath: string,
|
2021-03-08 07:24:11 -06:00
|
|
|
options: PeerTubeRequestOptions = {}
|
2019-02-21 10:19:16 -06:00
|
|
|
) {
|
2021-03-08 07:24:11 -06:00
|
|
|
const gotOptions = buildGotOptions(options)
|
2018-02-15 11:40:24 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
const outFile = createWriteStream(destPath)
|
2019-02-21 10:19:16 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
try {
|
|
|
|
await pipelinePromise(
|
|
|
|
peertubeGot.stream(url, gotOptions),
|
|
|
|
outFile
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
remove(destPath)
|
|
|
|
.catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err }))
|
2019-02-21 10:19:16 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
throw buildRequestError(err)
|
|
|
|
}
|
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)
|
2021-03-08 07:24:11 -06:00
|
|
|
await doRequestAndSaveToFile(url, tmpPath)
|
2018-11-16 09:48:17 -06:00
|
|
|
|
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 () {
|
2019-07-17 03:03:55 -05:00
|
|
|
return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
|
2019-07-16 07:52:24 -05:00
|
|
|
}
|
|
|
|
|
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,
|
2021-03-08 07:24:11 -06:00
|
|
|
doJSONRequest,
|
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
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
function buildGotOptions (options: PeerTubeRequestOptions) {
|
|
|
|
const { activityPub, bodyKBLimit = 1000 } = options
|
2019-02-21 10:19:16 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
const context = { bodyKBLimit, httpSignature: options.httpSignature }
|
2019-02-21 10:19:16 -06:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
let headers = options.headers || {}
|
|
|
|
|
|
|
|
headers = { ...headers, date: new Date().toUTCString() }
|
|
|
|
|
|
|
|
if (activityPub) {
|
|
|
|
headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
|
2019-02-21 10:19:16 -06:00
|
|
|
}
|
2021-03-08 07:24:11 -06:00
|
|
|
|
|
|
|
return {
|
|
|
|
method: options.method,
|
|
|
|
json: options.json,
|
|
|
|
searchParams: options.searchParams,
|
|
|
|
headers,
|
|
|
|
context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildRequestError (error: any) {
|
|
|
|
const newError = new Error(error.message)
|
|
|
|
newError.name = error.name
|
|
|
|
newError.stack = error.stack
|
|
|
|
|
|
|
|
if (error.response?.body) {
|
|
|
|
error.responseBody = error.response.body
|
|
|
|
}
|
|
|
|
|
|
|
|
return newError
|
2019-02-21 10:19:16 -06:00
|
|
|
}
|