-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
37 lines (34 loc) · 943 Bytes
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const imageGenerator = require('./index');
const reply = (callback, stream) => {
stream.toBuffer()
.then((buffer) => {
callback(null, {
statusCode: 200,
headers: {
'Content-Type': 'image/jpeg',
},
body: buffer.toString('base64'),
isBase64Encoded: true,
})
})
.catch(callback);
};
module.exports.hello = (event, context, callback) => {
const url = event.queryStringParameters && event.queryStringParameters.i;
if (!url) {
imageGenerator.getPlaceholder()
.then((res) => reply(callback, res))
.catch(callback);
return;
}
const options = Object.assign({ url, }, event.queryStringParameters);
imageGenerator.generateImage(options)
.then(res => reply(callback, res))
.catch((error) => {
imageGenerator.getPlaceholder()
.then(res => reply(callback, res))
.catch(callback);
return;
})
};