diff options
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 | } |