const fs = require('fs'); const path = require('path'); const TEMP_DIR = path.join(__dirname, '..', '..', 'temp'); const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days /** * Deletes files in the temp directory that are older than 7 days. * Returns a summary: { deleted, errors }. */ function cleanupTempFiles() { try { if (!fs.existsSync(TEMP_DIR)) return { deleted: 0, errors: 0 }; const files = fs.readdirSync(TEMP_DIR); const cutoff = Date.now() - MAX_AGE_MS; let deleted = 0; let errors = 0; for (const file of files) { try { const filePath = path.join(TEMP_DIR, file); const stat = fs.statSync(filePath); if (stat.isFile() && stat.mtimeMs < cutoff) { fs.unlinkSync(filePath); deleted++; } } catch { errors++; } } return { deleted, errors }; } catch { return { deleted: 0, errors: 1 }; } } module.exports = { cleanupTempFiles };