2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { MoreThan } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 08:28:29 +03:00
|
|
|
import type { UsersRepository, DriveFilesRepository, MiDriveFile } from '@/models/_.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
2023-05-29 05:54:49 +03:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
2023-05-29 05:54:49 +03:00
|
|
|
import type * as Bull from 'bullmq';
|
2023-04-12 03:13:58 +03:00
|
|
|
import type { DbJobDataWithUser } from '../types.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DeleteDriveFilesProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@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('delete-drive-files');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-05-29 05:54:49 +03:00
|
|
|
public async process(job: Bull.Job<DbJobDataWithUser>): Promise<void> {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info(`Deleting drive files of ${job.data.user.id} ...`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let deletedCount = 0;
|
2023-08-16 11:51:28 +03:00
|
|
|
let cursor: MiDriveFile['id'] | null = null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
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) {
|
2023-05-29 05:54:49 +03:00
|
|
|
job.updateProgress(100);
|
2022-09-17 21:27:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 04:45:01 +03:00
|
|
|
cursor = files.at(-1)?.id ?? null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
await this.driveService.deleteFileSync(file);
|
|
|
|
deletedCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const total = await this.driveFilesRepository.countBy({
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
|
2023-05-29 05:54:49 +03:00
|
|
|
job.updateProgress(deletedCount / total);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ(`All drive files (${deletedCount}) of ${user.id} has been deleted.`);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
}
|