| 3 | |
| 4 | // Utility for cleaning temp files |
| 5 | function cleanupTempFiles() { |
| 6 | const tempDir = path.join(process.cwd(), 'temp'); |
| 7 | |
| 8 | if (!fs.existsSync(tempDir)) { |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | fs.readdir(tempDir, (err, files) => { |
| 13 | if (err) { |
| 14 | console.error('Error reading temp directory:', err); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | let cleanedCount = 0; |
| 19 | const now = Date.now(); |
| 20 | const maxAge = 3 * 60 * 60 * 1000; // 3 hours |
| 21 | |
| 22 | files.forEach(file => { |
| 23 | const filePath = path.join(tempDir, file); |
| 24 | |
| 25 | fs.stat(filePath, (err, stats) => { |
| 26 | if (err) return; |
| 27 | |
| 28 | // Delete files older than 3 hours |
| 29 | if (now - stats.mtimeMs > maxAge) { |
| 30 | fs.unlink(filePath, (err) => { |
| 31 | if (!err) { |
| 32 | cleanedCount++; |
| 33 | console.log(`🧹 Cleaned temp file: ${file}`); |
| 34 | } |
| 35 | }); |
| 36 | } |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | if (cleanedCount > 0) { |
| 41 | console.log(`🧹 Cleaned ${cleanedCount} temp files`); |
| 42 | } |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | // Cleanup on startup |
| 47 | cleanupTempFiles(); |