mirror of
https://github.com/CaramelFur/Picsur.git
synced 2026-05-06 20:56:34 +02:00
Add automatic file migration to filekey/s3
This commit is contained in:
@@ -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<number> {
|
||||
return this.migrateRepoToFilekey(this.imageFileRepo);
|
||||
}
|
||||
|
||||
public async migrateDerivativesToFilekey(): AsyncFailable<number> {
|
||||
return this.migrateRepoToFilekey(this.imageDerivativeRepo);
|
||||
}
|
||||
|
||||
private async migrateRepoToFilekey(
|
||||
repo: Repository<EImageFileBackend | EImageDerivativeBackend>,
|
||||
): AsyncFailable<number> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user