aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/requests.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r--server/helpers/requests.ts15
1 files changed, 13 insertions, 2 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index 2e30c94a1..80476d649 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -1,18 +1,22 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { createWriteStream, remove } from 'fs-extra' 2import { createWriteStream, remove } from 'fs-extra'
3import * as request from 'request' 3import * as request from 'request'
4import { ACTIVITY_PUB } from '../initializers/constants' 4import { ACTIVITY_PUB, WEBSERVER } from '../initializers/constants'
5import { processImage } from './image-utils' 5import { processImage } from './image-utils'
6import { join } from 'path' 6import { join } from 'path'
7import { logger } from './logger' 7import { logger } from './logger'
8import { CONFIG } from '../initializers/config' 8import { CONFIG } from '../initializers/config'
9 9
10const packageJSON = require('../../../package.json')
11
10function doRequest <T> ( 12function doRequest <T> (
11 requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }, 13 requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean },
12 bodyKBLimit = 1000 // 1MB 14 bodyKBLimit = 1000 // 1MB
13): Bluebird<{ response: request.RequestResponse, body: T }> { 15): Bluebird<{ response: request.RequestResponse, body: T }> {
16 if (!(requestOptions.headers)) requestOptions.headers = {}
17 requestOptions.headers['User-Agent'] = getUserAgent()
18
14 if (requestOptions.activityPub === true) { 19 if (requestOptions.activityPub === true) {
15 if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {}
16 requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER 20 requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
17 } 21 }
18 22
@@ -27,6 +31,9 @@ function doRequestAndSaveToFile (
27 destPath: string, 31 destPath: string,
28 bodyKBLimit = 10000 // 10MB 32 bodyKBLimit = 10000 // 10MB
29) { 33) {
34 if (!requestOptions.headers) requestOptions.headers = {}
35 requestOptions.headers['User-Agent'] = getUserAgent()
36
30 return new Bluebird<void>((res, rej) => { 37 return new Bluebird<void>((res, rej) => {
31 const file = createWriteStream(destPath) 38 const file = createWriteStream(destPath)
32 file.on('finish', () => res()) 39 file.on('finish', () => res())
@@ -60,6 +67,10 @@ async function downloadImage (url: string, destDir: string, destName: string, si
60 } 67 }
61} 68}
62 69
70function getUserAgent () {
71 return `PeerTube/${packageJSON.version} (+${WEBSERVER.URL})`
72}
73
63// --------------------------------------------------------------------------- 74// ---------------------------------------------------------------------------
64 75
65export { 76export {