Skip to content

Commit 3a6f480

Browse files
committed
Add daily cleanup task to delete old files and update README
1 parent faa0b19 commit 3a6f480

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Secure Document Share - Backend
22

3-
## Overview
4-
This is the backend for our Secure Document Share App.
5-
6-
## Features
7-
tbd
3+
## About
4+
This is the backend for our Secure Document Share App.<br>
5+
It provides the WebSocket and Http Server.
86

97
### Prerequisites
108
Ensure you have the following installed:
@@ -30,3 +28,8 @@ Ensure you have the following installed:
3028
```sh
3129
node server.mjs --port=<port>
3230
```
31+
32+
4. **Setup the cron**
33+
```sh
34+
0 0 * * * /usr/bin/node /path/to/your/project/cron/daily.mjs
35+
```

cron/daily.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { deleteOldFiles } from './lib.js';
2+
3+
// Run the cleanup task to delete old files
4+
deleteOldFiles('./data', 3).catch(console.error);

cron/lib.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as fs from 'fs/promises';
2+
import * as path from 'path';
3+
4+
export async function deleteOldFiles(dir, retentionDays) {
5+
const now = Date.now();
6+
const files = await fs.readdir(dir);
7+
8+
for (const file of files) {
9+
const filePath = path.join(dir, file);
10+
const stat = await fs.stat(filePath);
11+
12+
if (file.endsWith('.bin')) {
13+
const ageInDays = (now - stat.mtimeMs) / (1000 * 60 * 60 * 24);
14+
if (ageInDays > retentionDays && stat.isFile()) {
15+
await fs.unlink(filePath);
16+
console.log(`Deleted: ${filePath}`);
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)