From adf4f9df6e6f5e677c6f7905bbfe63d7eb782393 Mon Sep 17 00:00:00 2001 From: gabriel-logan Date: Tue, 21 May 2024 20:31:51 -0300 Subject: [PATCH] feat: isValidImage gif validation --- packages/typescript/package.json | 2 +- packages/typescript/src/isValidImage.ts | 9 +++++- .../typescript/tests/src/isValidImage.test.ts | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/typescript/package.json b/packages/typescript/package.json index c149e4c..c9bf3a2 100644 --- a/packages/typescript/package.json +++ b/packages/typescript/package.json @@ -1,6 +1,6 @@ { "name": "multiform-validator", - "version": "2.0.3", + "version": "2.0.4", "description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.", "main": "./dist/index.js", "types": "./types/index.d.ts", diff --git a/packages/typescript/src/isValidImage.ts b/packages/typescript/src/isValidImage.ts index f0b4315..431ef20 100644 --- a/packages/typescript/src/isValidImage.ts +++ b/packages/typescript/src/isValidImage.ts @@ -8,13 +8,20 @@ function isValidImage(fileBuffer: Buffer): boolean { // Check magic numbers to determine the mimetype const isJpeg: boolean = fileBuffer[0] === 0xff && fileBuffer[1] === 0xd8 && fileBuffer[2] === 0xff; + const isPng: boolean = fileBuffer[0] === 0x89 && fileBuffer[1] === 0x50 && fileBuffer[2] === 0x4e && fileBuffer[3] === 0x47; - return isJpeg || isPng; + const isGif: boolean = + fileBuffer[0] == 0x47 && + fileBuffer[1] == 0x49 && + fileBuffer[2] == 0x46 && + fileBuffer[3] == 0x38; + + return isJpeg || isPng || isGif; } export default isValidImage; diff --git a/packages/typescript/tests/src/isValidImage.test.ts b/packages/typescript/tests/src/isValidImage.test.ts index 989123c..46afb49 100644 --- a/packages/typescript/tests/src/isValidImage.test.ts +++ b/packages/typescript/tests/src/isValidImage.test.ts @@ -18,5 +18,35 @@ describe('isValidImage', () => { const result = isValidImage(fileBuffer); expect(result).toBe(false); }); + + it('should return false for a GIF image', () => { + const fileBuffer = Buffer.from([0x47, 0x49, 0x46, 0x38, /* ... */]); + const result = isValidImage(fileBuffer); + expect(result).toBe(true); + }); + + it('should return false for an empty buffer', () => { + const fileBuffer = Buffer.from([]); + const result = isValidImage(fileBuffer); + expect(result).toBe(false); + }); + + it('should return false for a buffer with less than 4 bytes', () => { + const fileBuffer = Buffer.from([0x47, 0x49, 0x46]); + const result = isValidImage(fileBuffer); + expect(result).toBe(false); + }); + + it('should return false for a buffer with less than 3 bytes', () => { + const fileBuffer = Buffer.from([0x47, 0x49]); + const result = isValidImage(fileBuffer); + expect(result).toBe(false); + }); + + it('should return false for a buffer with less than 2 bytes', () => { + const fileBuffer = Buffer.from([0x47]); + const result = isValidImage(fileBuffer); + expect(result).toBe(false); + }); }); export default isValidImage;