-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (24 loc) · 779 Bytes
/
script.js
File metadata and controls
31 lines (24 loc) · 779 Bytes
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 File = require("./models/file");
const fs = require("fs");
const connectDB = require("./config/db");
// make connection
connectDB();
const fetchData = async () => {
// 24 hours old file fetch
const pastDate = new Date(Date.now() - (24 * 60 * 60 * 1000));
const files = await File.find({ createdAt: { $lt: pastDate } })
// delete files
if(files.length) {
try {
for(const file of files) {
fs.unlinkSync(file.path);
await file.remove();
console.log(`Successfully deleted ${file.filename}`);
}
} catch(error) {
console.log(`Error while deleting file ${error}`);
}
}
console.log(`Cron Job Done`);
}
fetchData().then(process.exit);