2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { IsNull, MoreThan, Not } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { DriveFilesRepository } from '@/models/index.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type Bull from 'bull';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CleanRemoteFilesProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private driveService: DriveService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('clean-remote-files');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public async process(job: Bull.Job<Record<string, unknown>>, done: () => void): Promise<void> {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info('Deleting cached remote files...');
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
let deletedCount = 0;
|
|
|
|
let cursor: any = null;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const files = await this.driveFilesRepository.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 => this.driveService.deleteFileSync(file, true)));
|
|
|
|
|
|
|
|
deletedCount += 8;
|
|
|
|
|
|
|
|
const total = await this.driveFilesRepository.countBy({
|
|
|
|
userHost: Not(IsNull()),
|
|
|
|
isLink: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
job.progress(deletedCount / total);
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:53:50 +02:00
|
|
|
this.logger.succ('All cached remote files has been deleted.');
|
2022-09-17 21:27:08 +03:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|