misskey/packages/backend/src/queue/processors/DeleteDriveFilesProcessorService.ts

79 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-09-17 20:27:08 +02:00
import { Inject, Injectable } from '@nestjs/common';
import { MoreThan } from 'typeorm';
import { DI } from '@/di-symbols.js';
import { UsersRepository, DriveFilesRepository } from '@/models/index.js';
import { Config } from '@/config.js';
import type Logger from '@/logger.js';
import { DriveService } from '@/core/DriveService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type Bull from 'bull';
import type { DbUserJobData } from '../types.js';
@Injectable()
export class DeleteDriveFilesProcessorService {
2022-09-18 20:11:50 +02:00
private logger: Logger;
2022-09-17 20:27:08 +02:00
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private driveService: DriveService,
private queueLoggerService: QueueLoggerService,
) {
2022-09-18 20:11:50 +02:00
this.logger = this.queueLoggerService.logger.createSubLogger('delete-drive-files');
2022-09-17 20:27:08 +02:00
}
public async process(job: Bull.Job<DbUserJobData>, done: () => void): Promise<void> {
2022-09-18 20:11:50 +02:00
this.logger.info(`Deleting drive files of ${job.data.user.id} ...`);
2022-09-17 20:27:08 +02:00
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
if (user == null) {
done();
return;
}
let deletedCount = 0;
let cursor: any = null;
while (true) {
const files = await this.driveFilesRepository.find({
where: {
userId: user.id,
...(cursor ? { id: MoreThan(cursor) } : {}),
},
take: 100,
order: {
id: 1,
},
});
if (files.length === 0) {
job.progress(100);
break;
}
cursor = files[files.length - 1].id;
for (const file of files) {
await this.driveService.deleteFileSync(file);
deletedCount++;
}
const total = await this.driveFilesRepository.countBy({
userId: user.id,
});
job.progress(deletedCount / total);
}
2022-09-18 20:11:50 +02:00
this.logger.succ(`All drive files (${deletedCount}) of ${user.id} has been deleted.`);
2022-09-17 20:27:08 +02:00
done();
}
}