43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import path from 'path';
|
|
|
|
export function registerImages(app, cacheDir) {
|
|
app.get('/img/cat/:id.avif', (req, res) => {
|
|
const { id } = req.params;
|
|
const imagePath = path.join(cacheDir, 'img', 'categories', `${id}.avif`);
|
|
const resolvedPath = path.resolve(imagePath);
|
|
|
|
res.sendFile(resolvedPath, {
|
|
headers: {
|
|
'Cache-Control': 'public, max-age=31536000, immutable'
|
|
}
|
|
}, (err) => {
|
|
if (err) {
|
|
if (!res.headersSent) {
|
|
res.status(404).send('Image not found');
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
// Product images
|
|
app.get('/img/prod/:id.avif', (req, res) => {
|
|
const { id } = req.params;
|
|
const imagePath = path.join(cacheDir, 'img', 'products', `${id}.avif`);
|
|
const resolvedPath = path.resolve(imagePath);
|
|
res.sendFile(resolvedPath, {
|
|
headers: {
|
|
'Cache-Control': 'public, max-age=31536000, immutable'
|
|
}
|
|
}, (err) => {
|
|
if (err) {
|
|
if (err.code !== 'ENOENT') {
|
|
console.error(`❌ Error serving image ${resolvedPath}:`, err);
|
|
}
|
|
if (!res.headersSent) {
|
|
res.status(404).send('Image not found');
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|