Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/image-handler/image-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ImageHandler {
* @param {Object} edits - The edits to be made to the original image.
*/
async applyEdits(originalImage, edits) {
const image = sharp(originalImage);
const image = sharp(originalImage, { failOnError: false }).rotate();
const keys = Object.keys(edits);
const values = Object.values(edits);
// Apply the image edits
Expand Down
13 changes: 8 additions & 5 deletions source/image-handler/image-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const ThumborMapping = require('./thumbor-mapping');

class ImageRequest {

/**
* Initializer function for creating a new image request, used by the image
* handler to perform image modifications.
Expand Down Expand Up @@ -137,8 +137,11 @@ class ImageRequest {
return decoded.key;
} else if (requestType === "Thumbor" || requestType === "Custom") {
// Parse the key from the end of the path
const key = (event["path"]).split("/");
return key[key.length - 1];
//const key = (event["path"]).split("/");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete these two lines? or add comment?

//return key[key.length - 1];

//Arpee: Support serving images under s3 subdirectories
return decodeURIComponent(event["path"].replace(/\d+x\d+\/|filters[:-][^/;]+|\/fit-in\/+|^\/+/g,'').replace(/^\/+/,''));
} else {
// Return an error for all other conditions
throw ({
Expand All @@ -160,8 +163,8 @@ class ImageRequest {
const path = event["path"];
// ----
const matchDefault = new RegExp(/^(\/?)([0-9a-zA-Z+\/]{4})*(([0-9a-zA-Z+\/]{2}==)|([0-9a-zA-Z+\/]{3}=))?$/);
const matchThumbor = new RegExp(/^(\/?)((fit-in)?|(filters:.+\(.?\))?|(unsafe)?).*(.+jpg|.+png|.+webp|.+tiff|.+jpeg)$/);
const matchCustom = new RegExp(/(\/?)(.*)(jpg|png|webp|tiff|jpeg)/);
const matchThumbor = new RegExp(/^(\/?)((fit-in)?|(filters:.+\(.?\))?|(unsafe)?).*(.+jpg|.+png|.+webp|.+tiff|.+jpeg)$/i);
const matchCustom = new RegExp(/(\/?)(.*)(jpg|png|webp|tiff|jpeg)/i);
const definedEnvironmentVariables = (
(process.env.REWRITE_MATCH_PATTERN !== "") &&
(process.env.REWRITE_SUBSTITUTION !== "") &&
Expand Down
2 changes: 1 addition & 1 deletion source/image-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "ISC",
"dependencies": {
"mocha": "^6.1.4",
"sharp": "^0.21.3",
"sharp": "^0.22.1",
"sinon": "^7.3.2",
"nyc": "^14.0.0"
},
Expand Down
11 changes: 6 additions & 5 deletions source/image-handler/test/test-thumbor-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ describe('process()', function() {
thumborMapping.process(event);
// Assert
const expectedResult = {
edits: {
edits: {
grayscale: true,
resize: {
width: 200,
height: 300
},
grayscale: true
height: 300,
fit: 'inside'
}
}
};
assert.deepEqual(thumborMapping.edits, expectedResult.edits);
Expand Down Expand Up @@ -628,7 +629,7 @@ describe('mapFilter()', function() {
thumborMapping.mapFilter(edit, filetype);
// Assert
const expectedResult = {
edits: {
edits: {
rotate: 0
}
};
Expand Down
29 changes: 20 additions & 9 deletions source/image-handler/thumbor-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,31 @@ class ThumborMapping {
this.path = event.path;
const edits = this.path.split('/');
const filetype = (this.path.split('.'))[(this.path.split('.')).length - 1];

//Process the Dimenions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dimensions?

const dimPath = this.path.match(/[^\/]\d+x\d+/g);
if (dimPath) {
const dims = dimPath[0].split('x');
// Set only if the dimensions provided are valid
if (isNaN(dims[0]) == false && isNaN(dims[1]) == false ){
this.edits.resize = {};
// Assign dimenions from the first match only to avoid parsing dimension from image file names
this.edits.resize.width = Number(dims[0]);
this.edits.resize.height = Number(dims[1]);

}
}

// Parse the image path
for (let i = 0; i < edits.length; i++) {
const edit = edits[i];
if (edit === ('fit-in')) {
this.edits.resize = {};
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
this.edits.resize.fit = 'inside'
this.sizingMethod = edit;
}
else if (edit.includes('x')) {
this.edits.resize = {};
const dims = edit.split('x');
this.edits.resize.width = Number(dims[0]);
this.edits.resize.height = Number(dims[1]);
}
if (edit.includes('filters:')) {
} else if (edit.includes('filters:')) {
this.mapFilter(edit, filetype);
}
}
Expand Down