feat: automatic preview generation on database init

Task 7: Batch-Migration Automation
- Add generateMissingPreviews() method to DatabaseManager
- Automatically runs after schema creation
- Finds all images without preview_path
- Generates previews for existing images on startup
- Graceful error handling (won't break server start)
- Progress logging: 'Found X images without preview, generating...'
- No manual script needed - fully automated

Benefits:
- Works on every backend restart
- Incremental (only missing previews)
- Non-blocking database initialization
- Perfect for deployments and updates
This commit is contained in:
Matthias Lotz 2025-10-30 20:51:35 +01:00
parent 661d6441ab
commit 170e1c20e6

View File

@ -34,6 +34,9 @@ class DatabaseManager {
// Erstelle Schema
await this.createSchema();
// Generate missing previews for existing images
await this.generateMissingPreviews();
console.log('✓ Datenbank erfolgreich initialisiert');
} catch (error) {
console.error('Fehler bei Datenbank-Initialisierung:', error);
@ -206,6 +209,68 @@ class DatabaseManager {
return false;
}
}
// Generate missing previews for existing images
async generateMissingPreviews() {
try {
console.log('🔍 Checking for images without previews...');
// Get all images that don't have a preview_path yet
const imagesWithoutPreview = await this.all(`
SELECT id, group_id, file_name, file_path
FROM images
WHERE preview_path IS NULL OR preview_path = ''
`);
if (imagesWithoutPreview.length === 0) {
console.log('✓ All images have previews');
return;
}
console.log(`📸 Found ${imagesWithoutPreview.length} image(s) without preview, generating...`);
const ImagePreviewService = require('../services/ImagePreviewService');
const fsp = require('fs').promises;
let successCount = 0;
let failCount = 0;
for (const image of imagesWithoutPreview) {
try {
// Check if original file exists
const originalPath = ImagePreviewService.getOriginalPath(image.file_name);
await fsp.access(originalPath);
// Generate preview
const previewFileName = ImagePreviewService._getPreviewFileName(image.file_name);
const previewPath = ImagePreviewService.getPreviewPath(previewFileName);
const result = await ImagePreviewService.generatePreview(originalPath, previewPath);
if (result.success) {
// Update database with preview_path
await this.run(`
UPDATE images
SET preview_path = ?
WHERE id = ?
`, [previewFileName, image.id]);
successCount++;
} else {
console.warn(` ⚠️ Preview generation failed for ${image.file_name}: ${result.error}`);
failCount++;
}
} catch (error) {
console.warn(` ⚠️ Could not process ${image.file_name}: ${error.message}`);
failCount++;
}
}
console.log(`✓ Preview generation complete: ${successCount} success, ${failCount} failed`);
} catch (error) {
console.warn('⚠️ Preview generation check failed:', error.message);
// Don't throw - this shouldn't prevent DB initialization
}
}
}
// Singleton Instance