forked from fritsvt/puppeteer-html-to-pdf-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge_expired.js
31 lines (25 loc) · 841 Bytes
/
purge_expired.js
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
const fs = require('fs');
const expiresIn = process.env.EXPIRES_IN;
purgeExpired();
setInterval( () => {
purgeExpired();
}, process.env.EXPIRE_CHECK_INTERVAL * 1000);
function purgeExpired() {
const currentTimestamp = Math.round(Date.now() / 1000);
console.log(`Purging expired: ${currentTimestamp}`)
fs.readdir(__dirname + '/static/exports', (err, files) => {
if (err) {
console.log('Error listing exports directory.', err);
return;
}
for (const file of files) {
const fileTimestamp = Number(file.split('-')[0]);
if (isNaN(fileTimestamp)) {
return;
}
if (fileTimestamp + expiresIn < currentTimestamp) {
fs.unlinkSync(`${__dirname}/static/exports/${file}`);
}
}
});
}