Reactoring + added new tests

master
Vic 2 years ago
parent 30775e7b41
commit 41d83e8b61

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 110 KiB

@ -32,14 +32,15 @@
"eslint-plugin-prettier": "^4.0.0", "eslint-plugin-prettier": "^4.0.0",
"jasmine": "^4.0.2", "jasmine": "^4.0.2",
"jasmine-spec-reporter": "^7.0.0", "jasmine-spec-reporter": "^7.0.0",
"supertest": "^6.2.2",
"ts-node": "^10.4.0", "ts-node": "^10.4.0",
"typescript": "^4.5.4" "typescript": "^4.5.4"
}, },
"dependencies": { "dependencies": {
"express": "^4.17.2", "express": "^4.17.2",
"image-size": "^1.0.1",
"nodemon": "^2.0.15", "nodemon": "^2.0.15",
"path": "^0.12.7", "path": "^0.12.7",
"sharp": "^0.29.3", "sharp": "^0.29.3"
"supertest": "^6.2.2"
} }
} }

@ -1,26 +1,10 @@
import express from "express" import express from "express"
import fs from "fs" import fs from "fs"
import path from "path" import path from "path"
import sharp from "sharp" import resizeImg from "../../utilities/imgProcessing"
const image = express.Router(); const image = express.Router();
const resizeImg = async (width: number, height: number, inputimgPath: string, fileName: string): Promise<string> => {
// 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<void> => { image.get('/', async (req: express.Request, res: express.Response): Promise<void> => {
@ -28,10 +12,9 @@ image.get('/', async (req: express.Request, res: express.Response): Promise<void
let width = parseInt(req.query.width as string); let width = parseInt(req.query.width as string);
let height = parseInt(req.query.height as string); let height = parseInt(req.query.height as string);
// fetch file from server // get correct path
let inputimgPath: string = path.resolve("images/full", `${fileName}.jpg`); let inputimgPath: string = path.resolve("images/full", `${fileName}.jpg`);
// check if the inputFile exist // check if the inputFile exist
if (!fs.existsSync(inputimgPath)) { if (!fs.existsSync(inputimgPath)) {
res.status(404).send("Image not found"); res.status(404).send("Image not found");
@ -43,7 +26,7 @@ image.get('/', async (req: express.Request, res: express.Response): Promise<void
if (fileName && width && height) { if (fileName && width && height) {
//resize image //resize image
let outputImg: string = await resizeImg(width, height, inputimgPath, fileName); let outputImg: string = await resizeImg(width, height, fileName);
res.sendFile(outputImg); res.sendFile(outputImg);
} else if (fileName) { } else if (fileName) {

@ -0,0 +1,26 @@
import app from "../index"
import resizeImg from "../utilities/imgProcessing"
import sizeOf from 'image-size'
import supertest from "supertest"
const request = supertest(app);
describe('Test on utilities', () => {
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);
})
})

@ -1,11 +1,21 @@
import app from "../index" import app from "../index"
import image from "../routes/api/image"
import supertest from "supertest" import supertest from "supertest"
import path from "path"
import fs from "fs"
const request = supertest(app); const request = supertest(app);
// Endpoint testing // Endpoint testing
describe('Test endpoint responses', () => { 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 () => { it('Get the api endpoint', async () => {
const response = await request.get('/api/image?filename=fjord'); const response = await request.get('/api/image?filename=fjord');
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -14,29 +24,10 @@ describe('Test endpoint responses', () => {
const response = await request.get('/api/image?filename=test'); const response = await request.get('/api/image?filename=test');
expect(response.status).toBe(404); 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);
})
})

@ -0,0 +1,20 @@
import sharp from "sharp"
import path from "path"
const resizeImg = async (width: number, height: number, fileName: string): Promise<string> => {
// 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
Loading…
Cancel
Save