const router = require('express-promise-router')() const db = require('../db') const ensureAdmin = require('./middleware/ensureAdmin') const sharp = require('sharp') router.get('/:uuid/:size', async (req, res) => { let extension if(req.params.size.includes('.')) { extension = req.params.size.split('.').slice(1).join('.'); req.params.size = req.params.size.split('.')[0]; } const image = await db.item.getImage(req.params.uuid, req.params.size) // Cache a month const cacheSeconds = 60 * 60 * 24 * 30; res.set('Cache-Control', cacheSeconds); if(extension === "webp") { const webp = await sharp(image.file).webp().toBuffer(); res.set('Content-Type', "image/webp") res.end(webp) return; } res.set('Content-Type', image.mime_type) res.end(image.file) }) router.post('/:uuid/featured', ensureAdmin, async (req, res) => { const item = await db.item.setFeatured(req.params.uuid) res.json(item) }) router.delete('/:uuid', ensureAdmin, async (req, res) => { const item = await db.item.removeImage(req.params.uuid) res.json(item) }) module.exports = router