-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (127 loc) · 3.4 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
const AWSXRay = require('aws-xray-sdk-core');
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
const S3 = new AWS.S3({
signatureVersion: 'v4',
});
const Sharp = require('sharp');
// set the S3 and API GW endpoints
const SOURCE_BUCKET = process.env.SOURCE_BUCKET;
const DEST_BUCKET = process.env.DEST_BUCKET;
const URL = process.env.URL;
// set allowed sizes
// const ALLOWED_DIMENSIONS = new Set();
// if (process.env.ALLOWED_DIMENSIONS) {
// const dimensions = process.env.ALLOWED_DIMENSIONS.split(/\s*,\s*/);
// dimensions.forEach((dimension) => ALLOWED_DIMENSIONS.add(dimension));
// }
exports.handler = (event, context, callback) => {
const key = event.queryStringParameters.key;
const match = key.match(/(.*)\/([s|w|h])(\d+x{0,1}\d+)\/(.*)/);
if (!match) {
callback(null, {
statusCode: '403',
headers: {},
body: 'Wrong file name',
});
return;
}
// requested resize direction
// s: width and height | w: width | h: height
let direction = match[2];
// requested size
let size = match[3];
let filename = match[4];
let folder = match[1];
// determine widht & height for resize
let width, height;
switch (direction) {
case 's':
width = parseInt(size.split('x')[0], 10);
height = parseInt(size.split('x')[1], 10);
break;
case 'w':
width = parseInt(size, 10);
height = null;
break;
case 'h':
width = null;
height = parseInt(size, 10);
break;
default:
callback(null, {
statusCode: '403',
headers: {},
body: 'Wrong size',
});
return;
break;
}
// determine file format
let extension = filename.split('.')[1];
let requiredFormat = extension == "jpg" ? "jpeg" : extension;
// set original jpg filename
let requiredFile = filename.split('.')[0] + '.jpg';
if (['jpg', 'png'].includes(extension)) {
requiredFile = filename;
}
let originalFolder = folder.split('/');
originalFolder.pop();
originalFolder = originalFolder.join('/');
// original file in the bucket
let originalKey = `${originalFolder}/${requiredFile}`;
// check only allowed sizes
//
// if (ALLOWED_DIMENSIONS.size > 0 && !ALLOWED_DIMENSIONS.has(dimensions)) {
// callback(null, {
// statusCode: '403',
// headers: {},
// body: '',
// });
// return;
// }
// get S3 object
S3.getObject({
Bucket: SOURCE_BUCKET,
Key: originalKey
}).promise()
.then(data => Sharp(data.Body)
.resize(width, height)
.toFormat(requiredFormat)
.toBuffer()
)
.then(buffer => S3.putObject({
Body: buffer,
Bucket: DEST_BUCKET,
ContentType: 'image/' + requiredFormat,
CacheControl: 'max-age=31536000',
Key: key,
StorageClass: 'REDUCED_REDUNDANCY'
}).promise()
.catch(err => callback(null, {
statusCode: '404',
headers: {},
body: {
message: 'Error saving the new file.',
error: JSON.stringify(err)
}
})))
.then(() => {
callback(null, {
statusCode: '301',
headers: {
'location': `${URL}/${key}`,
'Cache-Control': 'no-cache'
},
body: '',
})
})
.catch(err => callback(null, {
statusCode: '404',
headers: {},
body: {
message: 'Original file not found.',
error: JSON.stringify(err)
},
}))
};