diff --git a/.gitignore b/.gitignore index d28341e..7114b93 100644 --- a/.gitignore +++ b/.gitignore @@ -41,7 +41,7 @@ bower_components .lock-wscript # Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release +build/ # Dependency directories node_modules/ diff --git a/images/thumb/fjord.jpg b/images/thumb/fjord.jpg new file mode 100644 index 0000000..1b175fd Binary files /dev/null and b/images/thumb/fjord.jpg differ diff --git a/src/routes/api/image.ts b/src/routes/api/image.ts index 450350e..760a01d 100644 --- a/src/routes/api/image.ts +++ b/src/routes/api/image.ts @@ -1,26 +1,58 @@ import express from "express" -import path from "path" import fs from "fs" +import path from "path" +import sharp from "sharp" const image = express.Router(); -image.get('/', - async (req: express.Request, res: express.Response): Promise => { - let filename = req.query.filename as string; - - try { - let filePath = path.resolve("images/full", `${filename}.jpg`); - console.log('FILE PATH', filePath); - if (!fs.existsSync(filePath)) { - res.status(404).send("Image don't exist! Try another name"); - return; - } - res.sendFile(filePath); - } catch (err) { - res.status(404).send(`An error occured`); - } - - //res.send('Welcome to image route please add parametres in the url'); - }) +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 => { + let fileName = req.query.filename as string; + let width = parseInt(req.query.width as string); + let height = parseInt(req.query.height as string); + + // fetch file from server + let inputimgPath: string = path.resolve("images/full", `${fileName}.jpg`); + + + // check if the inputFile exist + if (!fs.existsSync(inputimgPath)) { + res.status(404).send("Image not found"); + return; + } + + + // display image if we have fileName, width, height attributs in the request + if (fileName && width && height) { + + //resize image + let outputImg: string = await resizeImg(width, height, inputimgPath, fileName); + res.sendFile(outputImg); + + } else if (fileName) { + // display original image if we only have filename parameter in the request + res.sendFile(inputimgPath); + } +}) + + + export default image; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index b4f1c0d..8e9392a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -74,7 +74,7 @@ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ /* Type Checking */ - "strict": true, /* Enable all strict type-checking options. */ + "strict": false, /* Enable all strict type-checking options. */ "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */