optimize image route / implement check if img exists for frontend purposes
This commit is contained in:
		@@ -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 });
 | 
							res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		log.api?.debug('GET image Success:', req.query, value);
 | 
							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
 | 
							await db.products
 | 
				
			||||||
			.findUnique({
 | 
								.findUnique({
 | 
				
			||||||
				where: {
 | 
									where: {
 | 
				
			||||||
@@ -24,18 +39,17 @@ async function get(req: Request, res: Response) {
 | 
				
			|||||||
			})
 | 
								})
 | 
				
			||||||
			.then((result) => {
 | 
								.then((result) => {
 | 
				
			||||||
				if (result) {
 | 
									if (result) {
 | 
				
			||||||
					const img_path = path.join(__path, 'images', `${value.id}.png`);
 | 
					 | 
				
			||||||
					// Serve stored or default image
 | 
										// Serve stored or default image
 | 
				
			||||||
					log.api?.debug('Image exists:', fs.existsSync(img_path));
 | 
										log.api?.debug('Image exists:', img_existing);
 | 
				
			||||||
					fs.existsSync(img_path) ? res.sendFile(img_path) : res.sendFile(path.join(__path, 'images', 'default.png'));
 | 
										img_existing ? res.sendFile(img_path) : res.sendFile(img_default_path);
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					// Product does not exist
 | 
										// Product does not exist
 | 
				
			||||||
					log.api?.debug('Product does not exist, using default image ');
 | 
										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) => {
 | 
								.catch((err) => {
 | 
				
			||||||
				handlePrismaError(err, res, 'GET image_provider');
 | 
									handlePrismaError(err, res, 'GET image');
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,8 @@ import validator from 'joi'; // DOCS: https://joi.dev/api
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// MARK: GET image
 | 
					// MARK: GET image
 | 
				
			||||||
const schema_get = validator.object({
 | 
					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
 | 
					// MARK: CREATE / UPDATE image
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user