Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bitmap support [WIP] #483

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
42 changes: 42 additions & 0 deletions source/image-handler/bitmap-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import Jimp from "jimp";
import { ImageHandlerError, StatusCodes } from "./lib";

export class BitmapRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

This class mostly converter from BMP to PNG, IMO ImageConverter is more appropriate name, or you can get rid of it at all and move into image-request.ts, like

private convert(imageBuffer: Buffer): Promise<Buffer> {
  // if image is BMP return converted into PNG buffer
  // else do nothing and return `imageBuffer`
}

public convertToPng = async (imageBuffer: Buffer): Promise<Buffer> => {
try {
const image = await Jimp.read(imageBuffer);
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
return buffer;
} catch (err) {
console.error("Error getting image buffer:", err);
throw new ImageHandlerError(
StatusCodes.BAD_REQUEST,
"BitmapRequest::convertToPng",
"It was not possible to convert buffer to png buffer"
);
}
};

public isBmp(imageBuffer: Buffer): boolean {
const imageSignature = imageBuffer.subarray(0, 4).toString("hex").toUpperCase();
if (
imageSignature === "89504E47" ||
imageSignature === "FFD8FFDB" ||
imageSignature === "FFD8FFE0" ||
imageSignature === "FFD8FFEE" ||
imageSignature === "FFD8FFE1" ||
imageSignature === "52494646" ||
imageSignature === "49492A00" ||
imageSignature === "4D4D002A"
Comment on lines +26 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

These constants are already defined, in the image-request.ts, why don't check only 424d here?

) {
return false;
} else {
const bmpMagicNumber = "424d";
const fileSignature = imageBuffer.subarray(0, 2).toString("hex");
return fileSignature === bmpMagicNumber;
}
}
}
10 changes: 9 additions & 1 deletion source/image-handler/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./lib";
import { SecretProvider } from "./secret-provider";
import { ThumborMapper } from "./thumbor-mapper";
import {BitmapRequest} from "./bitmap-handler";

type OriginalImageInfo = Partial<{
contentType: string;
Expand Down Expand Up @@ -177,7 +178,14 @@ export class ImageRequest {
}

result.cacheControl = originalImage.CacheControl ?? "max-age=31536000,public";
result.originalImage = imageBuffer;
const bitmapRequest = new BitmapRequest();

if (bitmapRequest.isBmp(imageBuffer)) {
result.contentType = 'image/png';
result.originalImage = await bitmapRequest.convertToPng(imageBuffer);
} else {
result.originalImage = imageBuffer;
}

return result;
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion source/image-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"color": "4.2.3",
"color-name": "1.1.4",
"sharp": "^0.31.3",
"aws-sdk": "^2.1356.0"
"aws-sdk": "^2.1356.0",
"jimp": "^0.22.8"
Copy link
Contributor

Choose a reason for hiding this comment

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

This package brings a lot of dependencies, for BMP it uses https://github.com/shaozilee/bmp-js, please refer to https://github.com/jimp-dev/jimp/blob/245f1cf60e0167d4d733ef90e638428d065856b2/packages/type-bmp/package.json#L25 I think you could use bmp-js instead.

},
"devDependencies": {
"@types/color": "^3.0.3",
Expand Down
83 changes: 83 additions & 0 deletions source/image-handler/test/bitmap-handler/bitmap-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import Jimp from 'jimp';
import { BitmapRequest } from '../../bitmap-handler';
import fs from 'fs';
import { ImageHandlerError, StatusCodes } from '../../lib';

// Mock the Jimp module to prevent actual image processing during tests
jest.mock('jimp', () => ({
read: jest.fn()
}));

describe('BitmapRequest', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

From my point of view, you should not consider it as a Request. Please create appropriate tests in

  • source/image-handler/test/image-handler
  • source/image-handler/test/image-request/infer-image-type.spec.ts

describe('convertToPng', () => {
const mockedRead = Jimp.read as jest.MockedFunction<typeof Jimp.read>;

afterEach(() => {
jest.resetAllMocks();
});

it('should convert the image buffer to PNG format', async () => {
const imageBuffer = Buffer.from('mocked image buffer');
const pngBuffer = Buffer.from('mocked PNG buffer');

mockedRead.mockResolvedValueOnce({
getBufferAsync: jest.fn().mockResolvedValueOnce(pngBuffer)
} as unknown as Jimp);

const bitmapRequest = new BitmapRequest();
const result = await bitmapRequest.convertToPng(imageBuffer);

expect(mockedRead).toHaveBeenCalledWith(imageBuffer);
expect(result).toEqual(pngBuffer);
});

it('should be able to identify bitmap format', async () => {
const bmpImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.bmp');
const pngImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.png');
const jpgImage = fs.readFileSync('./test/image/1x1.jpg');

const bmpBuffer = Buffer.from(bmpImage);
const pngBuffer = Buffer.from(pngImage);
const jpgBuffer = Buffer.from(jpgImage);

const bitmapRequest = new BitmapRequest();

expect(true).toEqual(bitmapRequest.isBmp(bmpBuffer));
expect(false).toEqual(bitmapRequest.isBmp(pngBuffer));
expect(false).toEqual(bitmapRequest.isBmp(jpgBuffer));

});

it('should convert the image file to PNG format', async () => {
const originalImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.bmp');
const pngImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.png');

const imageBuffer = Buffer.from(originalImage);
const pngBuffer = Buffer.from(pngImage);

mockedRead.mockResolvedValueOnce({
getBufferAsync: jest.fn().mockResolvedValueOnce(pngBuffer)
} as unknown as Jimp);

const bitmapRequest = new BitmapRequest();
const result = await bitmapRequest.convertToPng(imageBuffer);

expect(mockedRead).toHaveBeenCalledWith(imageBuffer);
expect(result).toEqual(pngBuffer);
});

it('should throw an error if an error occurs during conversion', async () => {
const imageBuffer = Buffer.from('mocked image buffer');
const error = new ImageHandlerError(StatusCodes.BAD_REQUEST, 'BitmapRequest::convertToPng', 'It was not possible to convert buffer to png buffer');

mockedRead.mockRejectedValueOnce(error);

const bitmapRequest = new BitmapRequest();

await expect(bitmapRequest.convertToPng(imageBuffer)).rejects.toThrowError(error);
});
});
});
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.