scnario commited on
Commit
80971ae
·
verified ·
1 Parent(s): 13a47fe

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +17 -2
server.js CHANGED
@@ -14,13 +14,28 @@ function generateRandomHex(length) {
14
  }
15
 
16
  const rand = (length) =>
17
- [...Array(length)].map(() => (Math.random().toString(36)[2] || '0')).join('');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
 
19
  app.use("/tmp", express.static(tempDir));
20
 
21
  app.all("/", async (req, res) => {
22
  const captcha = new CaptchaGenerator({ height: 200, width: 600 })
23
- .setDimension(200, 6000)
24
  .setCaptcha({ text: rand(6) })
25
  const fileName = `temp_${generateRandomHex(8)}.png`;
26
  const filePath = path.join(tempDir, fileName);
 
14
  }
15
 
16
  const rand = (length) =>
17
+ [...Array(length)].map(() => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[Math.floor(Math.random() * 62)]).join('');
18
+
19
+ const deleteOldFiles = (folderPath, maxAgeMinutes = 10) => {
20
+ setInterval(() => {
21
+ fs.readdir(folderPath, (err, files) => {
22
+ if (err) return;
23
+ const now = Date.now(), maxAgeMs = maxAgeMinutes * 60 * 1000;
24
+ files.forEach(file => {
25
+ const filePath = path.join(folderPath, file);
26
+ fs.stat(filePath, (err, stats) => {
27
+ if (!err && now - stats.mtimeMs > maxAgeMs) fs.unlink(filePath, () => {});
28
+ });
29
+ });
30
+ });
31
+ }, 60000);
32
+ };
33
 
34
+ deleteOldFiles(tempDir, 5)
35
  app.use("/tmp", express.static(tempDir));
36
 
37
  app.all("/", async (req, res) => {
38
  const captcha = new CaptchaGenerator({ height: 200, width: 600 })
 
39
  .setCaptcha({ text: rand(6) })
40
  const fileName = `temp_${generateRandomHex(8)}.png`;
41
  const filePath = path.join(tempDir, fileName);