diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/helpers/requests.ts | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r-- | server/helpers/requests.ts | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index ee9e80404..5c6dc5e19 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -1,17 +1,19 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { createWriteStream } from 'fs-extra' | 2 | import { createWriteStream } from 'fs-extra' |
3 | import * as request from 'request' | 3 | import * as request from 'request' |
4 | import { ACTIVITY_PUB } from '../initializers' | 4 | import { ACTIVITY_PUB, CONFIG } from '../initializers' |
5 | import { processImage } from './image-utils' | ||
6 | import { join } from 'path' | ||
5 | 7 | ||
6 | function doRequest ( | 8 | function doRequest <T> ( |
7 | requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean } | 9 | requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean } |
8 | ): Bluebird<{ response: request.RequestResponse, body: any }> { | 10 | ): Bluebird<{ response: request.RequestResponse, body: T }> { |
9 | if (requestOptions.activityPub === true) { | 11 | if (requestOptions.activityPub === true) { |
10 | if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {} | 12 | if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {} |
11 | requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER | 13 | requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER |
12 | } | 14 | } |
13 | 15 | ||
14 | return new Bluebird<{ response: request.RequestResponse, body: any }>((res, rej) => { | 16 | return new Bluebird<{ response: request.RequestResponse, body: T }>((res, rej) => { |
15 | request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body })) | 17 | request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body })) |
16 | }) | 18 | }) |
17 | } | 19 | } |
@@ -27,9 +29,18 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U | |||
27 | }) | 29 | }) |
28 | } | 30 | } |
29 | 31 | ||
32 | async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) { | ||
33 | const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName) | ||
34 | await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath) | ||
35 | |||
36 | const destPath = join(destDir, destName) | ||
37 | await processImage({ path: tmpPath }, destPath, size) | ||
38 | } | ||
39 | |||
30 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
31 | 41 | ||
32 | export { | 42 | export { |
33 | doRequest, | 43 | doRequest, |
34 | doRequestAndSaveToFile | 44 | doRequestAndSaveToFile, |
45 | downloadImage | ||
35 | } | 46 | } |