-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
38 lines (34 loc) · 1.28 KB
/
index.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
'use strict'
const fs = require("fs");
const util = require("util");
const path = require("path");
const ImageProcessor = require('./lib/ImageProcessor');
const S3Client = require('./lib/S3Client');
const Message = require('./lib/Message');
exports.handler = function (event, context, callback) {
let s3Record = event.Records[0].s3;
let srcBucket = s3Record.bucket.name;
let srcKey = decodeURIComponent(s3Record.object.key.replace(/\+/g, " "));
let config = JSON.parse(fs.readFileSync(path.resolve(__dirname, "config.json"), {encoding: "utf8"}));
let s3Client = new S3Client();
s3Client.getS3Image(srcBucket, srcKey)
.then(function (image) {
let processor = new ImageProcessor(image, config);
return processor.run();
})
.then(function(images) {
return s3Client.putS3Images(images);
})
.then((results) => {
var message = util.format("[%s] [%s] - %s", srcBucket, srcKey,
`Successfully processed. Generated ${results.length} images.`);
callback(null, message);
})
.catch((reason) => {
if(reason instanceof Message) {
callback(null, reason.body)
} else {
callback(reason);
}
});
};