File size: 1,104 Bytes
7db9461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;