-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.js
31 lines (26 loc) · 832 Bytes
/
cleanup.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 glob = require('glob');
const fs = require('fs');
const config = require('config');
const moment = require('moment');
const photoPath = config.get('photo.path');
/**
* Clean up old photos
*/
glob(`${photoPath}**/*.jpg`, null, (er, files) => {
const isoDate = moment().toISOString();
const maxFiles = config.get('photo.maxFiles');
if (files.length > maxFiles) {
// Sort files: older files first in array
files = files.sort((a, b) =>
fs.statSync(a).mtime.getTime() - fs.statSync(b).mtime.getTime()
);
// Files to delete
const filesToDelete = files.slice(0, files.length - maxFiles);
for (const file of filesToDelete) {
fs.unlinkSync(file);
console.log(`${isoDate} - File: ${file} removed`);
}
} else {
console.log(`${isoDate} - No files to delete 💁`);
}
});