summaryrefslogtreecommitdiff
path: root/src/queue/processors/object-storage/clean-remote-files.ts
blob: 7b34892e1f4c3b1f6ebc9c20b7f5bdd57e77c0fb (plain)
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
import * as Bull from 'bull';

import { queueLogger } from '../../logger';
import { deleteFileSync } from '../../../services/drive/delete-file';
import { DriveFiles } from '../../../models';
import { MoreThan, Not, IsNull } from 'typeorm';

const logger = queueLogger.createSubLogger('clean-remote-files');

export default async function cleanRemoteFiles(job: Bull.Job, done: any): Promise<void> {
	logger.info(`Deleting cached remote files...`);

	let deletedCount = 0;
	let cursor: any = null;

	while (true) {
		const files = await DriveFiles.find({
			where: {
				userHost: Not(IsNull()),
				isLink: false,
				...(cursor ? { id: MoreThan(cursor) } : {})
			},
			take: 8,
			order: {
				id: 1
			}
		});

		if (files.length === 0) {
			job.progress(100);
			break;
		}

		cursor = files[files.length - 1].id;

		await Promise.all(files.map(file => deleteFileSync(file, true)));

		deletedCount += 8;

		const total = await DriveFiles.count({
			userHost: Not(IsNull()),
			isLink: false,
		});

		job.progress(deletedCount / total);
	}

	logger.succ(`All cahced remote files has been deleted.`);
	done();
}