This project changes the image sizes this project is using a library called sharp, but at first, if the image is created for the first time, it does not cause problems, but it always shows me that problem if it was already created.
In imageProcessing.ts
File
import { Request, Response } from "express";
import fs, { promises as fsPromises } from 'fs';
import resizingTheImage from "./resizingTheImage";
const imageProcessing = async(req: Request, res: Response, next: Function): Promise<void | string> => {
const widthValue = <string>req.query.width;
const heightValue = <string>req.query.height;
const nameFileValue = <string>req.query.namefile;
const outputDir = `../../images/imageOutput/${nameFileValue}${widthValue}_${heightValue}.jpg`;
const makeDir = async (): Promise<void> => {
await fsPromises.mkdir('./images/imageOutput');
};
const imageProcessing = async (): Promise<void> => {
try {
await resizingTheImage(widthValue, heightValue, nameFileValue);
} catch {
res.send('There is a problem with the image processing');
}
};
if (fs.existsSync(outputDir)) {
return outputDir;
} else {
makeDir();
await imageProcessing();
}
next();
}
export default imageProcessing;
In resizingTheImage.ts
File
import sharp from "sharp";
const resizingTheImage = async (imageWidth: string, imageHeight: string, fileName: string): Promise<void> => {
const imageDir: string = `./images/${fileName}.jpg`;
const outputDir: string = `./images/imageOutput/${fileName}${imageWidth}_${imageHeight}.jpg`;
if (isNaN(parseInt(imageWidth) && parseInt(imageHeight)) && fileName) {
console.log("the inputs is Invalid")
} else {
await sharp(imageDir)
.resize(parseInt(imageWidth), parseInt(imageHeight))
.toFile(outputDir);
}
}
export default resizingTheImage;
In error
[Error: EEXIST: file already exists, mkdir 'E:\Image Processing API\images\imageOutput'] {
errno: -4075,
code: 'EEXIST',
syscall: 'mkdir',
path: 'E:\\Image Processing API\\images\\imageOutput'
}