From 3bf0e2fdd540f21bbc3cc0eb3768fcbea1546c22 Mon Sep 17 00:00:00 2001
From: Spacelord <git@spacelord.de>
Date: Sun, 9 Mar 2025 21:36:00 +0100
Subject: [PATCH] optimize image route / implement check if img exists for
 frontend purposes

---
 src/routes/api/v1/image/image.ts        | 24 +++++++++++++++++++-----
 src/routes/api/v1/image/image_schema.ts |  3 ++-
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/routes/api/v1/image/image.ts b/src/routes/api/v1/image/image.ts
index 2c6770c..effc5aa 100644
--- a/src/routes/api/v1/image/image.ts
+++ b/src/routes/api/v1/image/image.ts
@@ -16,6 +16,21 @@ async function get(req: Request, res: Response) {
 		res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
 	} else {
 		log.api?.debug('GET image Success:', req.query, value);
+		const img_path = path.join(__path, 'images', `${value.id}.png`);
+		const img_default_path = path.join(__path, 'images', 'default.png');
+
+		const img_existing = fs.existsSync(img_path);
+		const img_default_existing = fs.existsSync(img_default_path);
+
+		if (value.check) {
+			res.status(200).json(img_existing);
+			return;
+		}
+
+		if (!img_default_existing) {
+			log.api?.warn('Default image not found! Please make sure the following path contains an png file:', img_default_path);
+		}
+
 		await db.products
 			.findUnique({
 				where: {
@@ -24,18 +39,17 @@ async function get(req: Request, res: Response) {
 			})
 			.then((result) => {
 				if (result) {
-					const img_path = path.join(__path, 'images', `${value.id}.png`);
 					// Serve stored or default image
-					log.api?.debug('Image exists:', fs.existsSync(img_path));
-					fs.existsSync(img_path) ? res.sendFile(img_path) : res.sendFile(path.join(__path, 'images', 'default.png'));
+					log.api?.debug('Image exists:', img_existing);
+					img_existing ? res.sendFile(img_path) : res.sendFile(img_default_path);
 				} else {
 					// Product does not exist
 					log.api?.debug('Product does not exist, using default image ');
-					res.sendFile(path.join(__path, 'images', 'default.png'));
+					res.sendFile(img_default_path);
 				}
 			})
 			.catch((err) => {
-				handlePrismaError(err, res, 'GET image_provider');
+				handlePrismaError(err, res, 'GET image');
 			});
 	}
 }
diff --git a/src/routes/api/v1/image/image_schema.ts b/src/routes/api/v1/image/image_schema.ts
index 86fdbe4..f69ea98 100644
--- a/src/routes/api/v1/image/image_schema.ts
+++ b/src/routes/api/v1/image/image_schema.ts
@@ -3,7 +3,8 @@ import validator from 'joi'; // DOCS: https://joi.dev/api
 
 // MARK: GET image
 const schema_get = validator.object({
-	id: validator.number().positive().precision(0).default(0).note('product id') // id 0 should never exist, since id autoincrement starts at 1
+	id: validator.number().positive().precision(0).default(0).note('product id'), // id 0 should never exist, since id autoincrement starts at 1
+	check: validator.boolean().default(false)
 });
 
 // MARK: CREATE / UPDATE image