Create tmp.js
Browse files- plugins/tmp.js +60 -0
plugins/tmp.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs');
|
| 2 |
+
const path = require('path');
|
| 3 |
+
|
| 4 |
+
const handler = async (req, res) => {
|
| 5 |
+
try {
|
| 6 |
+
const filename = req.params.filename || req.query.filename;
|
| 7 |
+
|
| 8 |
+
if (!filename) {
|
| 9 |
+
return res.status(400).json({
|
| 10 |
+
success: false,
|
| 11 |
+
error: 'Missing filename parameter'
|
| 12 |
+
});
|
| 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).json({
|
| 20 |
+
success: false,
|
| 21 |
+
error: 'File not found or expired'
|
| 22 |
+
});
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
const ext = path.extname(filename).toLowerCase();
|
| 26 |
+
const contentTypes = {
|
| 27 |
+
'.webp': 'image/webp',
|
| 28 |
+
'.jpg': 'image/jpeg',
|
| 29 |
+
'.jpeg': 'image/jpeg',
|
| 30 |
+
'.png': 'image/png',
|
| 31 |
+
'.gif': 'image/gif'
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
const contentType = contentTypes[ext] || 'application/octet-stream';
|
| 35 |
+
|
| 36 |
+
res.setHeader('Content-Type', contentType);
|
| 37 |
+
res.setHeader('Cache-Control', 'public, max-age=60');
|
| 38 |
+
|
| 39 |
+
const fileStream = fs.createReadStream(filePath);
|
| 40 |
+
fileStream.pipe(res);
|
| 41 |
+
|
| 42 |
+
} catch (error) {
|
| 43 |
+
res.status(500).json({
|
| 44 |
+
success: false,
|
| 45 |
+
error: error.message
|
| 46 |
+
});
|
| 47 |
+
}
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
module.exports = {
|
| 51 |
+
name: 'Serve Temporary Files',
|
| 52 |
+
description: 'Serve temporary image files from tmp directory',
|
| 53 |
+
type: 'GET',
|
| 54 |
+
routes: ['tmp/:filename'],
|
| 55 |
+
tags: ['utility', 'files'],
|
| 56 |
+
main: ['tools'],
|
| 57 |
+
parameters: ['filename'],
|
| 58 |
+
enabled: true,
|
| 59 |
+
handler
|
| 60 |
+
};
|