| const express = require('express'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const router = express.Router(); | |
| router.get('/tmp/:filename', (req, res) => { | |
| try { | |
| const filename = req.params.filename; | |
| if (!filename) { | |
| return res.status(400).send('Missing filename'); | |
| } | |
| const tmpDir = path.join(process.cwd(), 'tmp'); | |
| const filePath = path.join(tmpDir, filename); | |
| if (!fs.existsSync(filePath)) { | |
| return res.status(404).send('File not found or expired'); | |
| } | |
| const ext = path.extname(filename).toLowerCase(); | |
| const contentTypes = { | |
| '.webp': 'image/webp', | |
| '.jpg': 'image/jpeg', | |
| '.jpeg': 'image/jpeg', | |
| '.png': 'image/png', | |
| '.gif': 'image/gif' | |
| }; | |
| const contentType = contentTypes[ext] || 'application/octet-stream'; | |
| res.setHeader('Content-Type', contentType); | |
| res.setHeader('Cache-Control', 'public, max-age=60'); | |
| const fileStream = fs.createReadStream(filePath); | |
| fileStream.pipe(res); | |
| } catch (error) { | |
| res.status(500).send(error.message); | |
| } | |
| }); | |
| module.exports = router; |