summaryrefslogtreecommitdiff
path: root/packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-09-18 03:27:08 +0900
committerGitHub <noreply@github.com>2022-09-18 03:27:08 +0900
commitb75184ec8e3436200bacdcd832e3324702553d20 (patch)
tree8b7e316f29e95df921db57289c8b8da476d18f07 /packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts
parentUpdate ROADMAP.md (diff)
downloadsharkey-b75184ec8e3436200bacdcd832e3324702553d20.tar.gz
sharkey-b75184ec8e3436200bacdcd832e3324702553d20.tar.bz2
sharkey-b75184ec8e3436200bacdcd832e3324702553d20.zip
なんかもうめっちゃ変えた
Diffstat (limited to 'packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts')
-rw-r--r--packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts b/packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts
new file mode 100644
index 0000000000..8c53632563
--- /dev/null
+++ b/packages/backend/src/queue/processors/CleanRemoteFilesProcessorService.ts
@@ -0,0 +1,69 @@
+import { Inject, Injectable } from '@nestjs/common';
+import { IsNull, MoreThan, Not } from 'typeorm';
+import { DI } from '@/di-symbols.js';
+import { 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';
+
+@Injectable()
+export class CleanRemoteFilesProcessorService {
+ #logger: Logger;
+
+ constructor(
+ @Inject(DI.config)
+ private config: Config,
+
+ @Inject(DI.driveFilesRepository)
+ private driveFilesRepository: DriveFilesRepository,
+
+ private driveService: DriveService,
+ private queueLoggerService: QueueLoggerService,
+ ) {
+ this.#logger = this.queueLoggerService.logger.createSubLogger('clean-remote-files');
+ }
+
+ public async process(job: Bull.Job<Record<string, unknown>>, done: () => void): Promise<void> {
+ this.#logger.info('Deleting cached remote files...');
+
+ 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);
+ }
+
+ this.#logger.succ('All cahced remote files has been deleted.');
+ done();
+ }
+}