Create tmp.js
Browse files
tmp.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const fs = require('fs');
|
| 3 |
+
const path = require('path');
|
| 4 |
+
|
| 5 |
+
const router = express.Router();
|
| 6 |
+
|
| 7 |
+
router.get('/tmp/:filename', (req, res) => {
|
| 8 |
+
try {
|
| 9 |
+
const filename = req.params.filename;
|
| 10 |
+
|
| 11 |
+
if (!filename) {
|
| 12 |
+
return res.status(400).send('Missing filename');
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const tmpDir = path.join(process.cwd(), 'tmp');
|
| 16 |
+
const filePath = path.join(tmpDir, filename);
|
| 17 |
+
|
| 18 |
+
if (!fs.existsSync(filePath)) {
|
| 19 |
+
return res.status(404).send('File not found or expired');
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const ext = path.extname(filename).toLowerCase();
|
| 23 |
+
const contentTypes = {
|
| 24 |
+
'.webp': 'image/webp',
|
| 25 |
+
'.jpg': 'image/jpeg',
|
| 26 |
+
'.jpeg': 'image/jpeg',
|
| 27 |
+
'.png': 'image/png',
|
| 28 |
+
'.gif': 'image/gif'
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
const contentType = contentTypes[ext] || 'application/octet-stream';
|
| 32 |
+
|
| 33 |
+
res.setHeader('Content-Type', contentType);
|
| 34 |
+
res.setHeader('Cache-Control', 'public, max-age=60');
|
| 35 |
+
|
| 36 |
+
const fileStream = fs.createReadStream(filePath);
|
| 37 |
+
fileStream.pipe(res);
|
| 38 |
+
|
| 39 |
+
} catch (error) {
|
| 40 |
+
res.status(500).send(error.message);
|
| 41 |
+
}
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
module.exports = router;
|