diff --git a/images/thumb/fjord.jpg b/images/thumb/fjord.jpg index 1b175fd..9879351 100644 Binary files a/images/thumb/fjord.jpg and b/images/thumb/fjord.jpg differ diff --git a/package.json b/package.json index 888a4ec..2d0ed55 100644 --- a/package.json +++ b/package.json @@ -32,14 +32,15 @@ "eslint-plugin-prettier": "^4.0.0", "jasmine": "^4.0.2", "jasmine-spec-reporter": "^7.0.0", + "supertest": "^6.2.2", "ts-node": "^10.4.0", "typescript": "^4.5.4" }, "dependencies": { "express": "^4.17.2", + "image-size": "^1.0.1", "nodemon": "^2.0.15", "path": "^0.12.7", - "sharp": "^0.29.3", - "supertest": "^6.2.2" + "sharp": "^0.29.3" } } diff --git a/src/routes/api/image.ts b/src/routes/api/image.ts index b2f5138..dc1f109 100644 --- a/src/routes/api/image.ts +++ b/src/routes/api/image.ts @@ -1,26 +1,10 @@ import express from "express" import fs from "fs" import path from "path" -import sharp from "sharp" +import resizeImg from "../../utilities/imgProcessing" const image = express.Router(); -const resizeImg = async (width: number, height: number, inputimgPath: string, fileName: string): Promise => { - // output file for resized img - let outputImg = path.resolve("images/thumb", `${fileName}.jpg`); - await sharp(path.resolve(inputimgPath)) - .resize(width, height) - .toFormat("jpeg") - .jpeg({ - quality: 100, - mozjpeg: true - }) - .toFile(path.resolve(outputImg)) - - return outputImg -} - - image.get('/', async (req: express.Request, res: express.Response): Promise => { @@ -28,10 +12,9 @@ image.get('/', async (req: express.Request, res: express.Response): Promise { + it('Image should have correct size after resize', async () => { + let outputImg = await resizeImg(400, 400, 'fjord'); + let dimensions = sizeOf(outputImg); + expect(dimensions.width).toBe(400); + expect(dimensions.height).toBe(400); + }) +}) + +describe('Test on the image endpoint with resize', () => { + it('Resize working with filename and width/height parameters', async () => { + const response = await request.get('/api/image?filename=encenadaport&width=200&height=200'); + expect(response.status).toBe(200); + }) +}) \ No newline at end of file diff --git a/src/tests/indexSpec.ts b/src/tests/indexSpec.ts index e8d2442..80619cb 100644 --- a/src/tests/indexSpec.ts +++ b/src/tests/indexSpec.ts @@ -1,11 +1,21 @@ import app from "../index" -import image from "../routes/api/image" import supertest from "supertest" +import path from "path" +import fs from "fs" + + + + const request = supertest(app); // Endpoint testing describe('Test endpoint responses', () => { + it('Check if input images folder exists', async () => { + let inputimgPath: string = path.resolve("images/full", `fjord.jpg`); + let result = fs.existsSync(inputimgPath) + expect(result).toBe(true); + }) it('Get the api endpoint', async () => { const response = await request.get('/api/image?filename=fjord'); expect(response.status).toBe(200); @@ -14,29 +24,10 @@ describe('Test endpoint responses', () => { const response = await request.get('/api/image?filename=test'); expect(response.status).toBe(404); }) - it('Working if we only have a filename in the url', async () => { - const response = await request.get('/api/image?filename=encenadaport'); - expect(response.status).toBe(200); - }) }) -// Image resize test -describe('Test of the image endpoint with resize', () => { - it('Resize working with filename and width parameter', async () => { - const response = await request.get('/api/image?filename=encenadaport&height=200'); - expect(response.status).toBe(200); - }) - it('Resize working with filename and height parameter', async () => { - const response = await request.get('/api/image?filename=encenadaport&width=200'); - expect(response.status).toBe(200); - }) - it('Resize working with filename and width, height parameter', async () => { - const response = await request.get('/api/image?filename=encenadaport&height=200'); - expect(response.status).toBe(200); - }) - -}) + diff --git a/src/utilities/imgProcessing.ts b/src/utilities/imgProcessing.ts new file mode 100644 index 0000000..fd96edb --- /dev/null +++ b/src/utilities/imgProcessing.ts @@ -0,0 +1,20 @@ +import sharp from "sharp" +import path from "path" + +const resizeImg = async (width: number, height: number, fileName: string): Promise => { + // output file for resized img + let inputimgPath: string = path.resolve("images/full", `${fileName}.jpg`); + let outputImg: string = path.resolve("images/thumb", `${fileName}.jpg`); + await sharp(path.resolve(inputimgPath)) + .resize(width, height) + .toFormat("jpeg") + .jpeg({ + quality: 100, + mozjpeg: true + }) + .toFile(path.resolve(outputImg)) + + return outputImg +} + +export default resizeImg \ No newline at end of file