refactor: split into database backup controller

This commit is contained in:
izzy
2025-12-02 17:59:21 +00:00
parent a79b4bdc47
commit 9b955508e9
21 changed files with 644 additions and 550 deletions

View File

@@ -0,0 +1,50 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { basename, join } from 'node:path';
import { StorageCore } from 'src/cores/storage.core';
import { CacheControl, StorageFolder } from 'src/enum';
import { BaseService } from 'src/services/base.service';
import { deleteBackup, isValidBackupName, listBackups, uploadBackup } from 'src/utils/backups';
import { ImmichFileResponse } from 'src/utils/file';
/**
* This service is available outside of maintenance mode to manage maintenance mode
*/
@Injectable()
export class DatabaseBackupService extends BaseService {
async listBackups(): Promise<{ backups: string[] }> {
return { backups: await listBackups(this.backupRepos) };
}
async deleteBackup(filename: string): Promise<void> {
return deleteBackup(this.backupRepos, basename(filename));
}
async uploadBackup(file: Express.Multer.File): Promise<void> {
return uploadBackup(this.backupRepos, file);
}
downloadBackup(fileName: string): ImmichFileResponse {
if (!isValidBackupName(fileName)) {
throw new BadRequestException('Invalid backup name!');
}
const path = join(StorageCore.getBaseFolder(StorageFolder.Backups), fileName);
return {
path,
fileName,
cacheControl: CacheControl.PrivateWithoutCache,
contentType: fileName.endsWith('.gz') ? 'application/gzip' : 'application/sql',
};
}
private get backupRepos() {
return {
logger: this.logger,
storage: this.storageRepository,
config: this.configRepository,
process: this.processRepository,
database: this.databaseRepository,
};
}
}