-
Notifications
You must be signed in to change notification settings - Fork 243
Allowed resolution validation #65
Allowed resolution validation #65
Conversation
|
Thanks for opening this! When I built this, I wanted to make it a minimal example for folks to play with, learn from, and possibly fork and build off of, so I intentionally eschewed features required to make it production ready like constraining dimensions. That said, I'm not opposed to adding this, and I'd like it to be optional. Your implementation seems to check that box.
|
|
Hi, The allowed resolutions are set using an env. variable (similar to
The check is performed against the whole heightxwidth string between |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. A couple of things before we merge:
- I added some suggestions for easier readability for newbies. I'd like to err on the side of being verbose to make it easier to read as this is meant as a reference.
- Please add documentation of this feature in the README.
Thank you so much for your contribution! Looks great.
lambda/index.js
Outdated
| const match = key.match(/((\d+)x(\d+))\/(.*)/); | ||
|
|
||
| //Check if requested resolution is allowed | ||
| if(0 != ALLOWED_RESOLUTIONS.size && !ALLOWED_RESOLUTIONS.has(match[1]) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be phrased in the affirmative for easier parsing? I'd suggest something like:
if(ALLOWED_DIMENSIONS.size > 0 && ALLOWED_DIMENSIONS.has(dimensions)) {
// Do the thing.
}I'd also like to see the matches set to locals for easier parsing up top, so:
const dimensions = match[1];There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem with this one.
lambda/index.js
Outdated
|
|
||
| const BUCKET = process.env.BUCKET; | ||
| const URL = process.env.URL; | ||
| const ALLOWED_RESOLUTIONS = process.env.ALLOWED_RESOLUTIONS ? new Set(process.env.ALLOWED_RESOLUTIONS.split(/\s*,\s*/)) : new Set([]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about killing the ternary which could confuse newbies? Maybe something like:
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));
}A little too explicit, maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure no problem, you can remove the ternary.
lambda/index.js
Outdated
| return; | ||
| } | ||
|
|
||
| const width = parseInt(match[2], 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and if you could move these up. I'm fine doing these assignments even if we bail. I think it reads better. Basically:
const key = event.queryStringParameters.key;
const match = key.match(/((\d+)x(\d+))\/(.*)/);
const dimensions = match[1];
const width = parseInt(match[2], 10);
const height = parseInt(match[3], 10);
const originalKey = match[4];There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure no problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
lambda/index.js
Outdated
| return; | ||
| } | ||
|
|
||
| const width = parseInt(match[2], 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure no problem
Reformatted text and added section about restricting allowed resize resolutions.
|
I updated the README file and (hopefully) reviewed your changes. |
Updated README.md to reflect changed env. var. name
|
I applied your recommendations. Also changed the README file to reflect the changed env. variable name. Please review. Thx. |
|
Thanks for contributing! ✨👊🎉 |
Support for validating against a set of allowed resolutions configured via environment variable.