-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (44 loc) · 1.62 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
48
49
50
51
52
53
54
const qs = require('querystring')
var Jimp = require("jimp");
const AWS = require('aws-sdk');
AWS.config.update({region: 'ap-northeast-2'});
const s3 = new AWS.S3()
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const { w } = qs.parse(request.querystring)
const width = parseInt(w)
if (Number.isNaN(width)) {
callback(null, request)
return
}
s3.getObject({
Bucket: "wsi-objects",
Key: request.uri.replace('/', '')
}, (_, object) => {
const contentType = object.ContentType
Jimp.read(object.Body)
.then((jimp) => {
jimp
.resize(width, Jimp.AUTO)
.getBase64(contentType, (_, result) => {
const response = {
status: '200',
statusDescription: 'OK',
headers: {
'cache-control': [{
key: 'Cache-Control',
value: 'max-age=100'
}],
'content-type': [{
key: 'Content-Type',
value: contentType
}]
},
body: result.replace(`data:${contentType};base64,`, ''),
bodyEncoding: 'base64'
};
callback(null, response);
})
})
})
};