diff --git a/backend/src/collections/image-db/image-file-db.service.ts b/backend/src/collections/image-db/image-file-db.service.ts index a2680ff..f11e8c1 100644 --- a/backend/src/collections/image-db/image-file-db.service.ts +++ b/backend/src/collections/image-db/image-file-db.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { ImageEntryVariant } from 'picsur-shared/dist/dto/image-entry-variant.enum'; import { AsyncFailable, Fail, FT, HasFailed } from 'picsur-shared/dist/types'; -import { In, IsNull, LessThan, Repository } from 'typeorm'; +import { In, IsNull, LessThan, Not, Repository } from 'typeorm'; import { v4 as uuidv4 } from 'uuid'; import { EImageDerivativeBackend } from '../../database/entities/images/image-derivative.entity'; import { EImageFileBackend } from '../../database/entities/images/image-file.entity'; @@ -236,7 +236,7 @@ export class ImageFileDBService { try { let remaining = Infinity; let processed = 0; - + while (remaining > 0) { const orphaned = await repo.findAndCount({ where: { @@ -265,4 +265,38 @@ export class ImageFileDBService { return Fail(FT.Database, e); } } + + public async migrateFilesToFilekey(): AsyncFailable { + return this.migrateRepoToFilekey(this.imageFileRepo); + } + + public async migrateDerivativesToFilekey(): AsyncFailable { + return this.migrateRepoToFilekey(this.imageDerivativeRepo); + } + + private async migrateRepoToFilekey( + repo: Repository, + ): AsyncFailable { + let processed = 0; + + try { + while (true) { + const current = await repo.findOne({ + where: { + data: Not(IsNull()), + }, + }); + if (!current) break; + + const result = await this.getFileData(current); + if (HasFailed(result)) return result; + + processed++; + } + } catch (e) { + return Fail(FT.Database, e); + } + + return processed; + } } diff --git a/backend/src/managers/image/image-manager.module.ts b/backend/src/managers/image/image-manager.module.ts index 38e026a..589ba7c 100644 --- a/backend/src/managers/image/image-manager.module.ts +++ b/backend/src/managers/image/image-manager.module.ts @@ -39,7 +39,7 @@ export class ImageManagerModule implements OnModuleInit { await this.cleanupDerivatives(); await this.cleanupExpired(); await this.cleanupOrphanedFiles(); - // TODO: Auto migrate all images to S3 + await this.migrateFilesToFilekey(); } private async cleanupDerivatives() { @@ -98,4 +98,24 @@ export class ImageManagerModule implements OnModuleInit { `Cleaned up ${cleanedUpDerivatives} orphaned derivatives and ${cleanedUpFiles} orphaned files`, ); } + + private async migrateFilesToFilekey() { + const filesMigrated = await this.imageFileDB.migrateFilesToFilekey(); + if (HasFailed(filesMigrated)) { + filesMigrated.print(this.logger); + return; + } + + const derivativesMigrated = + await this.imageFileDB.migrateDerivativesToFilekey(); + if (HasFailed(derivativesMigrated)) { + derivativesMigrated.print(this.logger); + return; + } + + if (filesMigrated > 0 || derivativesMigrated > 0) + this.logger.log( + `Migrated ${filesMigrated} files and ${derivativesMigrated} derivatives to filekey`, + ); + } }