This repository was archived by the owner on Oct 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
97 lines (79 loc) · 2.74 KB
/
app.ts
File metadata and controls
97 lines (79 loc) · 2.74 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { join } from 'path';
import AutoLoad, { AutoloadPluginOptions } from 'fastify-autoload';
import { FastifyPluginAsync } from 'fastify';
import cron from 'node-cron';
import prisma from './utils/prisma';
import s3 from './utils/s3';
export type AppOptions = {
// Place your custom options for app below here.
} & Partial<AutoloadPluginOptions>;
const app: FastifyPluginAsync<AppOptions> = async (
fastify,
opts
): Promise<void> => {
// Place here your custom code!
cron.schedule('0 * * * *', async () => {
const expiredFiles = await prisma.file.findMany({
where: { expire: { lte: new Date() } }
});
await prisma.fileWebhooks.deleteMany({
where: { file: { id: { in: expiredFiles.map(file => file.id) } } }
});
await prisma.file.deleteMany({
where: { id: { in: expiredFiles.map(file => file.id) } }
});
const allFilesInS3 = await s3.listObjectsV2({
Bucket: 'hiberstorage',
}).promise();
const allFilesInDB = await prisma.file.findMany();
let expiredFilesId = expiredFiles.map(file => file.hiberfileId);
allFilesInS3.Contents?.forEach(s3File => {
if (!allFilesInDB.find(dbFile => dbFile.hiberfileId === s3File.Key) && s3File.Key) {
expiredFilesId.push(s3File.Key);
return;
}
});
console.log('The following files should be deleted soon: ', expiredFiles);
for (const expiredFileIdsChunked of expiredFilesId.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / 1000);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [] as (typeof expiredFilesId);
}
resultArray[chunkIndex].push(item);
return resultArray;
}, [] as (typeof expiredFilesId)[])) {
try {
const deleteObjectsRes = await s3.deleteObjects({
Bucket: 'hiberstorage',
Delete: {
Objects: expiredFileIdsChunked.map((fileId) => {
return { Key: fileId };
})
}
}).promise();
console.log(deleteObjectsRes);
} catch (err) {
console.log('Failed to delete the following files: ', expiredFileIdsChunked);
console.log(err);
}
}
});
fastify.setSerializerCompiler(() => (data) => JSON.stringify(data));
// Do not touch the following lines
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
void fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
options: opts
});
// This loads all plugins defined in routes
// define your routes in one of these
void fastify.register(AutoLoad, {
dir: join(__dirname, 'routes'),
options: opts,
routeParams: true
});
};
export default app;
export { app };