-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (39 loc) · 1.35 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
39
40
41
42
43
44
45
46
47
const AWS = require('aws-sdk');
const fileType = require('file-type');
const sha1 = require('sha1');
const sharp = require('sharp');
const S3Upload = require('./s3Upload');
const ImageResizer = require('./resizer');
exports.handler = function(event, context, callback) {
const request = event.body;
const base64String = request.base64String;
const buffer = new Buffer(base64String, 'base64');
const fileMime = fileType(buffer);
console.log('if filemime...');
if (fileMime === null) callback({"error" : "The string suppplied is not a file type"});
const fileExt = fileMime.ext;
const randomFolderName = sha1(new Buffer(new Date().toString()));
const thumbName = `thumb_128x128.${fileExt}`;
const originalName = `original.${fileExt}`;
const resizer = new ImageResizer(sharp);
const originalUpload = new S3Upload(AWS);
const thumbnailUpload = new S3Upload(AWS);
const size = {
w: 128,
h: 128,
};
async function resizeAndUpload() {
try {
thumbImage = await resizer.resize(base64String, size);
thumb = await thumbnailUpload.upload(thumbImage, thumbName, randomFolderName, true);
original = await originalUpload.upload(buffer, originalName, randomFolderName, true);
callback(null, {
original: `${randomFolderName}/${originalName}`,
thumb: `${randomFolderName}/${thumbName}`
});
}catch (err){
callback(err);
}
}
resizeAndUpload();
}