-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
40 lines (36 loc) · 1.02 KB
/
utils.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
32
33
34
35
36
37
38
39
40
const fs = require("fs");
module.exports = {
cleanUpImages: () => {
console.log("Cleaning up images");
// Delete files which are 5 minutes old in images folder
fs.readdir("./images", (err, files) => {
if (err) {
console.log(err);
} else {
files.forEach((file) => {
const filePath = `./images/${file}`;
fs.stat(filePath, (err, stats) => {
if (err) {
console.log(err);
} else {
if (stats.isFile()) {
const fileDate = new Date(stats.birthtime);
const now = new Date();
const diff = now - fileDate;
if (diff > 300000) {
fs.unlink(filePath, (err) => {
if (err) {
console.log(err);
} else {
console.log(`${file} deleted`);
}
});
}
}
}
});
});
}
});
},
};