Skip to content

Commit 3051d5c

Browse files
dougtoppinDoug Toppin
andauthored
fix for thumbor watermark issue, add unit tests (#432)
Co-authored-by: Doug Toppin <[email protected]>
1 parent 2ae1bee commit 3051d5c

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

source/image-handler/image-request.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,12 @@ export class ImageRequest {
291291
}
292292

293293
return decodeURIComponent(
294-
path.replace(/\/\d+x\d+:\d+x\d+\/|(?<=\/)\d+x\d+\/|filters:[^/]+|\/fit-in(?=\/)|^\/+/g, "").replace(/^\/+/, "")
294+
path
295+
.replace(
296+
/\/\d+x\d+:\d+x\d+\/|(?<=\/)\d+x\d+\/|filters:watermark\(.*\)|filters:[^/]+|\/fit-in(?=\/)|^\/+/g,
297+
""
298+
)
299+
.replace(/^\/+/, "")
295300
);
296301
}
297302

source/image-handler/test/image-request/parse-image-key.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,34 @@ describe("parseImageKey", () => {
261261
});
262262
}
263263
});
264+
265+
it("Should pass if an image key value is provided in the thumbor request format with a watermark containing a slash", () => {
266+
// Arrange
267+
const event = {
268+
path: "/fit-in/400x400/filters:watermark(bucket,folder/key.png,0,0)/image.jpg",
269+
};
270+
271+
// Act
272+
const imageRequest = new ImageRequest(s3Client, secretProvider);
273+
const result = imageRequest.parseImageKey(event, RequestTypes.THUMBOR);
274+
275+
// Assert
276+
const expectedResult = "image.jpg";
277+
expect(result).toEqual(expectedResult);
278+
});
279+
280+
it("Should pass if an image key value is provided in the thumbor request format with a watermark not containing a slash", () => {
281+
// Arrange
282+
const event = {
283+
path: "/fit-in/400x400/filters:watermark(bucket,key.png,0,0)/image.jpg",
284+
};
285+
286+
// Act
287+
const imageRequest = new ImageRequest(s3Client, secretProvider);
288+
const result = imageRequest.parseImageKey(event, RequestTypes.THUMBOR);
289+
290+
// Assert
291+
const expectedResult = "image.jpg";
292+
expect(result).toEqual(expectedResult);
293+
});
264294
});

source/image-handler/test/thumbor-mapper/filter.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,68 @@ describe("filter", () => {
638638
};
639639
expect(edits).toEqual(expectedResult.edits);
640640
});
641+
642+
it("Should pass for fit-in combined with watermark in folder", () => {
643+
// watermark params: bucket, key, xPos, yPos, alpha, wRatio, hRatio
644+
const path = "/fit-in/400x400/filters:watermark(bucket,folder/key.png,0,0)/image.jpg";
645+
646+
// Act
647+
const thumborMapper = new ThumborMapper();
648+
const edits = thumborMapper.mapPathToEdits(path);
649+
650+
// Assert
651+
const expectedResult = {
652+
edits: {
653+
resize: {
654+
width: 400,
655+
height: 400,
656+
fit: "inside",
657+
},
658+
overlayWith: {
659+
bucket: "bucket",
660+
key: "folder/key.png",
661+
alpha: undefined,
662+
wRatio: undefined,
663+
hRatio: undefined,
664+
options: {
665+
left: "0",
666+
top: "0",
667+
},
668+
},
669+
},
670+
};
671+
expect(edits).toEqual(expectedResult.edits);
672+
});
673+
674+
it("Should pass for fit-in combined with watermark not in folder", () => {
675+
// watermark params: bucket, key, xPos, yPos, alpha, wRatio, hRatio
676+
const path = "/fit-in/400x400/filters:watermark(bucket,key.png,0,0)/image.jpg";
677+
678+
// Act
679+
const thumborMapper = new ThumborMapper();
680+
const edits = thumborMapper.mapPathToEdits(path);
681+
682+
// Assert
683+
const expectedResult = {
684+
edits: {
685+
resize: {
686+
width: 400,
687+
height: 400,
688+
fit: "inside",
689+
},
690+
overlayWith: {
691+
bucket: "bucket",
692+
key: "key.png",
693+
alpha: undefined,
694+
wRatio: undefined,
695+
hRatio: undefined,
696+
options: {
697+
left: "0",
698+
top: "0",
699+
},
700+
},
701+
},
702+
};
703+
expect(edits).toEqual(expectedResult.edits);
704+
});
641705
});

0 commit comments

Comments
 (0)