Skip to content
This repository was archived by the owner on Oct 31, 2018. It is now read-only.

Commit ba53cf7

Browse files
andybabjpignata
andybab
authored andcommitted
Applied changes from jpignata
Updated README.md to reflect changed env. var. name
1 parent eb58b40 commit ba53cf7

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ Resizes images on the fly using Amazon S3, AWS Lambda, and Amazon API Gateway. U
3232

3333
**Note:** If you create the Lambda function yourself, make sure to select Node.js version 6.10.
3434

35-
4. Restrict resize resolutions
35+
4. Restrict resize dimensions
3636

37-
To restrict the resolutions client services can convert images to, set the environment variable ```ALLOWED_RESOLUTIONS``` to a string in the format *(HEIGHT)x(WIDTH),(HEIGHT)x(WIDTH),...(HEIGHT)x(WIDTH)*.
38-
For example: *200x150,300x200,800x600*.
37+
To restrict the resolutions client services can convert images to, set the environment variable ```ALLOWED_DIMENSIONS``` to a string in the format *(HEIGHT)x(WIDTH),(HEIGHT)x(WIDTH),...(HEIGHT)x(WIDTH)*.
38+
For example: *300x300,90x90,40x40*.
3939

4040

4141
## License

lambda/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,32 @@ const Sharp = require('sharp');
88

99
const BUCKET = process.env.BUCKET;
1010
const URL = process.env.URL;
11-
const ALLOWED_RESOLUTIONS = process.env.ALLOWED_RESOLUTIONS ? new Set(process.env.ALLOWED_RESOLUTIONS.split(/\s*,\s*/)) : new Set([]);
11+
const ALLOWED_DIMENSIONS = new Set();
12+
13+
if (process.env.ALLOWED_DIMENSIONS) {
14+
const dimensions = process.env.ALLOWED_DIMENSIONS.split(/\s*,\s*/);
15+
dimensions.forEach((dimension) => ALLOWED_DIMENSIONS.add(dimension));
16+
}
1217

1318
exports.handler = function(event, context, callback) {
1419
const key = event.queryStringParameters.key;
1520
const match = key.match(/((\d+)x(\d+))\/(.*)/);
21+
const dimensions = match[1];
22+
const width = parseInt(match[2], 10);
23+
const height = parseInt(match[3], 10);
24+
const originalKey = match[4];
1625

1726
//Check if requested resolution is allowed
18-
if(0 != ALLOWED_RESOLUTIONS.size && !ALLOWED_RESOLUTIONS.has(match[1]) ) {
19-
callback(null, {
27+
if(ALLOWED_DIMENSIONS.size > 0 && !ALLOWED_DIMENSIONS.has(dimensions)) {
28+
callback(null, {
2029
statusCode: '403',
2130
headers: {},
2231
body: '',
2332
});
2433
return;
2534
}
2635

27-
const width = parseInt(match[2], 10);
28-
const height = parseInt(match[3], 10);
29-
const originalKey = match[4];
30-
36+
//Get object from S3, resize and store backe to S3
3137
S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
3238
.then(data => Sharp(data.Body)
3339
.resize(width, height)

0 commit comments

Comments
 (0)