Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions source/image-handler/image-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ export class ImageHandler {
return "png";
case ImageFormatTypes.WEBP:
return "webp";
case ImageFormatTypes.TIF:
return "tiff";
case ImageFormatTypes.TIFF:
return "tiff";
case ImageFormatTypes.HEIF:
Expand Down
6 changes: 5 additions & 1 deletion source/image-handler/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { QueryParamMapper } from "./query-param-mapper";
dayjs.extend(customParseFormat);
dayjs.extend(utc);


type OriginalImageInfo = Partial<{
contentType: string;
expires: string;
Expand Down Expand Up @@ -73,13 +74,16 @@ export class ImageRequest {
ImageFormatTypes.JPEG,
ImageFormatTypes.PNG,
ImageFormatTypes.WEBP,
ImageFormatTypes.TIF,
ImageFormatTypes.TIFF,
ImageFormatTypes.HEIF,
ImageFormatTypes.GIF,
ImageFormatTypes.AVIF,
];

imageRequestInfo.contentType = `image/${imageRequestInfo.outputFormat}`;
imageRequestInfo.contentType = imageRequestInfo.outputFormat === ImageFormatTypes.TIF
? ContentTypes.TIFF
: `image/${imageRequestInfo.outputFormat}`;
if (
requestType.includes(imageRequestInfo.requestType) &&
acceptedValues.includes(imageRequestInfo.outputFormat)
Expand Down
1 change: 1 addition & 0 deletions source/image-handler/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum ImageFormatTypes {
JPEG = "jpeg",
PNG = "png",
WEBP = "webp",
TIF = "tif",
TIFF = "tiff",
HEIF = "heif",
HEIC = "heic",
Expand Down
23 changes: 23 additions & 0 deletions source/image-handler/test/image-handler/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ describe("modifyImageOutput", () => {
expect(resultFormat).toEqual(ImageFormatTypes.JPEG);
});

it("Should return an image in TIFF format when outputFormat is TIF", async () => {
// Arrange
const request: ImageRequestInfo = {
requestType: RequestTypes.DEFAULT,
bucket: "sample-bucket",
key: "sample-image-001.png",
edits: { grayscale: true, flip: true },
outputFormat: ImageFormatTypes.TIF,
originalImage: image,
};
const imageHandler = new ImageHandler(s3Client, rekognitionClient);
const sharpImage = sharp(request.originalImage, { failOnError: false }).withMetadata();
const toFormatSpy = jest.spyOn(sharp.prototype, "toFormat");
const result = await imageHandler["modifyImageOutput"](sharpImage, request).toBuffer();

// Act
const resultFormat = (await sharp(result).metadata()).format;

// Assert
expect(toFormatSpy).toHaveBeenCalledWith("tiff");
expect(resultFormat).toEqual(ImageFormatTypes.TIFF);
});

it("Should return an image in the same format when outputFormat is not provided", async () => {
// Arrange
const request: ImageRequestInfo = {
Expand Down
31 changes: 31 additions & 0 deletions source/image-handler/test/image-request/setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,37 @@ describe("setup", () => {
expect(imageRequestInfo).toEqual(expectedResult);
});

it("Should pass when a default image request with TIF format is provided and populate the ImageRequest object with the proper values", async () => {
// Arrange
const event = {
path: "/eyJidWNrZXQiOiJ2YWxpZEJ1Y2tldCIsImtleSI6InZhbGlkS2V5IiwiZWRpdHMiOnsidG9Gb3JtYXQiOiJ0aWYifX0=",
};
process.env.SOURCE_BUCKETS = "validBucket, validBucket2";

// Mock
mockS3Commands.getObject.mockResolvedValue({ Body: mockImageBody });

// Act
const imageRequest = new ImageRequest(s3Client, secretProvider);
const imageRequestInfo = await imageRequest.setup(event);
const expectedResult = {
requestType: "Default",
bucket: "validBucket",
key: "validKey",
edits: { toFormat: "tif" },
outputFormat: "tif",
originalImage: mockImage,
cacheControl: "max-age=31536000,public",
contentType: "image/tiff",
};
// Assert
expect(mockS3Commands.getObject).toHaveBeenCalledWith({
Bucket: "validBucket",
Key: "validKey",
});
expect(imageRequestInfo).toEqual(expectedResult);
});

it("Should pass when a thumbor image request is provided and populate the ImageRequest object with the proper values", async () => {
// Arrange
const event = { path: "/filters:grayscale()/test-image-001.jpg" };
Expand Down
14 changes: 14 additions & 0 deletions source/image-handler/test/thumbor-mapper/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ describe("filter", () => {
expect(edits).toEqual(expectedResult);
});

it("Should pass if the filter is successfully translated from Thumbor:quality() for TIF", () => {
// Arrange
const edit = "filters:quality(50)";
const filetype = ImageFormatTypes.TIF;

// Act
const thumborMapper = new ThumborMapper();
const edits = thumborMapper.mapFilter(edit, filetype);

// Assert
const expectedResult = { tiff: { quality: 50 } };
expect(edits).toEqual(expectedResult);
});

it("Should pass if the filter is successfully translated from Thumbor:quality()", () => {
// Arrange
const edit = "filters:quality(50)";
Expand Down
3 changes: 2 additions & 1 deletion source/image-handler/thumbor-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ export class ThumborMapper {
const toSupportedImageFormatType = (format: ImageFormatTypes): ImageFormatTypes => {
if ([ImageFormatTypes.JPG, ImageFormatTypes.JPEG].includes(format)) {
return ImageFormatTypes.JPEG;
} else if ([ImageFormatTypes.TIF, ImageFormatTypes.TIFF].includes(format)) {
return ImageFormatTypes.TIFF;
} else if (
[
ImageFormatTypes.PNG,
ImageFormatTypes.WEBP,
ImageFormatTypes.TIFF,
ImageFormatTypes.HEIF,
ImageFormatTypes.GIF,
ImageFormatTypes.AVIF,
Expand Down