Skip to content

Commit e34f0b6

Browse files
Adding functionality for create image middleware
1 parent a92fdd1 commit e34f0b6

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
import express from 'express';
22
import sharp from 'sharp';
3+
import { promises as fsPromises } from 'fs';
34

45
export default async function (req : express.Request , res : express.Response , next : express.NextFunction) {
5-
6-
7-
8-
6+
// if the request entred this middleware this means it's not in the thumv folder
7+
// so we need to find if the photo exists in the full folder and if yes resize it for the given width and height
8+
// then we save it in the thumb folder
9+
// then res.sendFile(requiredImage , {root : "path"});
10+
const absolutePath = "C:\\Users\\AhmedZein\\Desktop\\FWD Advanced\\full\\" ;
11+
const thumbPath = 'C:\\Users\\AhmedZein\\Desktop\\FWD Advanced\\thumb\\'
12+
try {
13+
const arrayOfExistsImages = await fsPromises.readdir('./full');
14+
let requiredImage : ( string | undefined ) ;
15+
// this loop finds if the Image exists in the full folder
16+
for (let i = 0 ; i <= arrayOfExistsImages.length - 1 ; i++){
17+
const nameOfTheImage = arrayOfExistsImages[i].slice(0,-4) ;
18+
if ( nameOfTheImage === res.locals.name ) requiredImage = arrayOfExistsImages[i] ;
19+
}
20+
// does not exist ? res with resource was not found
21+
if (! requiredImage ) res.status (404).end();
22+
// if the Image exists please resize it and chached in the thumb folder
23+
else {
24+
await sharp (`${absolutePath}\\${requiredImage}`).resize(res.locals.width,res.locals.height,{
25+
fit : 'inside'
26+
}).toFile(`${thumbPath}\\${requiredImage}`);
27+
res.status(200).sendFile(`${requiredImage}` , { root : thumbPath })
28+
}
29+
}catch (error) {
30+
console.log (error);
31+
next();
32+
}
933
}

src/middlewares/api.image.Middleware/isExist.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ export default async function isExist (req : express.Request , res : express.Res
1212
// else created it form the full folder and chach it in the thumb folder
1313
const absolutePath = "C:\\Users\\AhmedZein\\Desktop\\FWD Advanced\\thumb\\" ;
1414
try {
15-
const arrayOfExistsImagesCached = await fsPromises.readdir('./thumb');
16-
let requiredImage : ( string | undefined ) ;
15+
const arrayOfExistsImagesCached = await fsPromises.readdir('./thumb');
16+
let requiredImage : (string | undefined );
1717
for (let i = 0 ; i <= arrayOfExistsImagesCached.length - 1 ; i++){
18-
const nameOfTheImage = arrayOfExistsImagesCached[i].split("").slice(0 , arrayOfExistsImagesCached.length - 5).join("") ;
19-
if ( nameOfTheImage !== res.locals.name ) continue;
20-
const imageHeight = (await sharp (`${absolutePath}\\${arrayOfExistsImagesCached[i]}`).metadata()).height;
21-
const imageWidth = (await sharp (`${absolutePath}\\${arrayOfExistsImagesCached[i]}`).metadata()).width;
22-
if ( imageHeight !== res.locals.height) continue;
23-
if ( imageWidth !== res.locals.width) continue;
24-
18+
const nameOfTheImage = arrayOfExistsImagesCached[i].slice(0,-4) ;
19+
if ( nameOfTheImage != res.locals.name ) continue;
20+
const imageWidthHeight = await sharp (`${absolutePath}\\${arrayOfExistsImagesCached[i]}`).metadata();
21+
if ( imageWidthHeight.height != res.locals.height) continue;
22+
if ( imageWidthHeight.width != res.locals.width) continue;
2523
requiredImage = arrayOfExistsImagesCached[i] ;
2624
}
2725
if ( !requiredImage ) {
2826
// here we go to the next() middleware to create the image
29-
res.status (400).send('bad Request').end();
27+
next();
3028
}else {
3129
res.status(200).sendFile( `${requiredImage}` ,{ root : absolutePath } ) ;
3230
}

src/routes/image.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ route.get ( '/' , validate, isExist , createImage ,(req,res)=>{
1313
// if the request is validate 'next()'
1414
// check the chashed folder for existing photo
1515

16-
res.status (500).send('bad coding').end();
16+
res.status (500).end();
1717
});
1818

1919

thumb/smtp1.png

-21 KB
Binary file not shown.

0 commit comments

Comments
 (0)