2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { IsNull, MoreThan, Not } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { MiDriveFile, DriveFilesRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-05-29 11:54:49 +09:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type * as Bull from 'bullmq';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CleanRemoteFilesProcessorService {
|
2022-09-19 03:11:50 +09:00
|
|
|
private logger: Logger;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private driveService: DriveService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('clean-remote-files');
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-05-29 11:54:49 +09:00
|
|
|
public async process(job: Bull.Job<Record<string, unknown>>): Promise<void> {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.logger.info('Deleting cached remote files...');
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
let deletedCount = 0;
|
2023-08-16 17:51:28 +09:00
|
|
|
let cursor: MiDriveFile['id'] | null = null;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
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) {
|
2023-05-29 11:54:49 +09:00
|
|
|
job.updateProgress(100);
|
2022-09-18 03:27:08 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 10:45:01 +09:00
|
|
|
cursor = files.at(-1)?.id ?? null;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
await Promise.all(files.map(file => this.driveService.deleteFileSync(file, true)));
|
|
|
|
|
|
|
|
deletedCount += 8;
|
|
|
|
|
|
|
|
const total = await this.driveFilesRepository.countBy({
|
|
|
|
userHost: Not(IsNull()),
|
|
|
|
isLink: false,
|
|
|
|
});
|
|
|
|
|
2023-05-29 11:54:49 +09:00
|
|
|
job.updateProgress(deletedCount / total);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-11-14 06:53:50 +09:00
|
|
|
this.logger.succ('All cached remote files has been deleted.');
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
}
|