import { Module } from '@nestjs/common'; import { MulterModule } from '@nestjs/platform-express'; import type { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface'; import { BackupController } from './backup.controller'; import { BackupService } from './backup.service'; import { AutoBackupJob } from './auto-backup.job'; import { AuditModule } from '../audit/audit.module'; import { SchedulingModule } from '../scheduling/scheduling.module'; import { AppConfigModule } from '../app-config/app-config.module'; import { StorageModule } from '../storage/storage.module'; import { StorageService } from '../storage/storage.service'; import { MAX_BACKUP_UPLOAD_SIZE } from './backup.impl'; import { DatabaseBackupModule } from './database-backup.module'; /** * Multer options for the restore upload. The uploaded zip is a restore INPUT, * not a stored object — it gets no storage category and spools to the * driver-agnostic scratch dir (data/tmp), where the handler consumes and * unlinks it. Resolved through DI so no path is computed at module load. */ export function buildBackupUploadOptions(storage: StorageService): MulterOptions { return { dest: storage.tempDir(), fileFilter: (_req: unknown, file: Express.Multer.File, cb: (err: Error | null, accept: boolean) => void) => { if (file.originalname.endsWith('.zip')) return cb(null, true); cb(new Error('Only ZIP files allowed'), false); }, limits: { fileSize: MAX_BACKUP_UPLOAD_SIZE }, }; } @Module({ imports: [ AppConfigModule, AuditModule, // Global, so the admin domain sees the port too; imported here because the // backup domain is what it belongs to. DatabaseBackupModule, SchedulingModule, StorageModule, MulterModule.registerAsync({ imports: [StorageModule], inject: [StorageService], useFactory: buildBackupUploadOptions, }), ], controllers: [BackupController], providers: [BackupService, AutoBackupJob], }) export class BackupModule {}