-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
61 lines (51 loc) · 1.56 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const config = require('./config.html2pdf');
const {pdf, util, storage} = require('./core');
let $callback;
exports.handler = (event, context, callback) => {
// make it available globally
$callback = callback;
// Get POST request data
const body = JSON.parse(event.body); // parse body
const template = body.template; // get template
const options = body.options || {}; // get options
const token = body.token; // get token
// Check auth token
if (token !== config.AUTH_TOKEN) {
$callback(new Error('Unauthorized'));
return;
}
// Render PDF
pdf.render(template, options, sendToS3);
};
const sendToS3 = (error, pdfOutput) => {
// Check if something bad happened
if (error) {
$callback(error);
return;
}
// S3 PUT params
const params = {
Bucket: config.S3_BUCKET, // Bucket name
Key: `${util.strRandom()}.pdf`, // Random file name
Body: pdfOutput, // File Buffer
ACL: 'public-read',
Expires: 120,
ContentType: 'application/pdf'
};
// Send it away to S3
storage.put(params, prepareResponse);
};
const prepareResponse = (error, url) => {
// Check if something bad happened
if(error) {
$callback(error);
return;
}
// Respond the callback with the S3 response
$callback(null, {
"isBase64Encoded": false,
"statusCode": 200,
"headers": { "Content-Type": "application/json; charset=utf-8" },
"body": JSON.stringify({ url })
});
};