-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
66 lines (54 loc) · 1.94 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
62
63
64
65
66
'use strict';
const exec = require('child_process').exec;
const crypto = require('crypto');
const fs = require('fs');
const AWS = require('aws-sdk');
module.exports.screenshot = (event, context, callback) => {
console.log(event);
const targetUrl = event.url;
var place = event.place;
var d = new Date;
var day = [d.getFullYear(), d.getMonth()+1, d.getDate()].join('-');
var time = [d.getHours(), d.getMinutes(), d.getSeconds()].join('-');
const targetBucket = `traffic-maps`;
const targetHash = crypto.createHash('md5').update(targetUrl).digest('hex');
const targetFilename = `screenshot/${place}/${day}/${time}.png`;
console.log(`Snapshotting ${targetUrl} to s3://${targetBucket}/${targetFilename}`);
const cmd = `./phantomjs/phantomjs --debug=yes --ignore-ssl-errors=true ./phantomjs/screenshot.js "${targetUrl}" /tmp/${targetHash}.png`;
exec(cmd, (error, stdout, stderr) => {
console.log(`error: ${error}`);
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error) {
// the command failed (non-zero), fail the entire call
console.warn(`exec error: ${error}`, stdout, stderr);
callback(`422, please try again ${error}`);
}
else {
console.log(`Reading file from tmp: /tmp/${targetHash}.png`);
// snapshotting succeeded, let's upload to S3
const fileBuffer = fs.readFileSync(`/tmp/${targetHash}.png`);
// upload the file
const s3 = new AWS.S3();
s3.putObject({
ACL: 'public-read',
Key: targetFilename,
Body: fileBuffer,
Bucket: targetBucket,
ContentType: 'image/png',
}, (err) => {
if (err) {
console.warn(err);
callback(err);
} else {
callback(null, {
hash: targetHash,
key: `${targetFilename}`,
bucket: targetBucket,
});
}
return;
});
}
});
};