95 lines
3.1 KiB
TypeScript

import { Request, Response } from 'express';
import path from 'node:path';
import fs from 'node:fs';
import db, { handlePrismaError } from '../../../../handlers/db.js'; // Database
import log from '../../../../handlers/log.js';
import __path from '../../../../handlers/path.js';
import { schema_get, schema_post, schema_del } from './image_schema.js';
// MARK: GET image
async function get(req: Request, res: Response) {
const { error, value } = schema_get.validate(req.query);
if (error) {
log.api?.debug('GET image Error:', req.query, value, error.details[0].message);
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else {
log.api?.debug('GET image Success:', req.query, value);
await db.products
.findUnique({
where: {
id: value.id
}
})
.then((result) => {
if (result) {
const img_path = path.join(__path, 'images', `${value.id}.png`);
// Serve stored or default image
fs.existsSync(img_path) ? res.sendFile(img_path) : res.sendFile(path.join(__path, 'images', 'default.png'));
} else {
// Product does not exist
res.sendFile(path.join(__path, 'images', 'default.png'));
}
})
.catch((err) => {
handlePrismaError(err, res, 'GET image_provider');
});
}
}
// MARK: CREATE image (upload)
async function post(req: Request, res: Response) {
const { error, value } = schema_post.validate(req.query);
if (error) {
log.api?.debug('POST image Error:', req.query, value, error.details[0].message);
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else {
log.api?.debug('POST image Success:', req.query, value);
// if (!req.files || Object.keys(req.files).length === 0) {
// res.status(400).send('No files were uploaded.');
// }
// const upload_file = req.files?.file;
// let upload_path;
// //uploadPath = __dirname + '/somewhere/on/your/server/' + sampleFile.name;
// const allowedMimeTypes = ['image/png', 'image/jpeg'];
// // if (!allowedMimeTypes.includes(upload_file.mimetype)) {
// // return res.status(400).send('Invalid file type. Only PNG and JPEG are allowed.');
// // }
// uploadedFile.mv(`/path/to/save/${uploadedFile.name}`, (err) => {
// if (err) {
// return res.status(500).send(err);
// }
// res.send('File uploaded!');
}
}
// MARK: DELETE image
async function del(req: Request, res: Response) {
const { error, value } = schema_del.validate(req.query);
if (error) {
log.api?.debug('DEL image Error:', req.query, value, error.details[0].message);
res.status(400).json({ status: 'ERROR', errorcode: 'VALIDATION_ERROR', message: error.details[0].message });
} else {
log.api?.debug('DEL image Success:', req.query, value);
// await db.products
// .delete({
// where: {
// id: value.id
// }
// })
// .then((result) => {
// res.status(200).json({ status: 'DELETED', message: 'Successfully deleted product', id: result.id });
// })
// .catch((err) => {
// handlePrismaError(err, res, 'DEL products');
// });
}
}
export default { get, post, del };