feat: add preview_path migration and constants

Task 2: DB Migration
- Add preview_path column to images table via ALTER TABLE
- Migration runs automatically on database initialization
- Handles duplicate column gracefully

Task 3: Constants & Config
- Export PREVIEW_FS_DIR constant (data/previews)
- Export PREVIEW_CONFIG (800px, 85% quality, JPEG)
- Update ImagePreviewService to import from constants
- Update schema.sql documentation with preview_path
This commit is contained in:
Matthias Lotz 2025-10-30 20:30:56 +01:00
parent 0471830e49
commit 940144cbf5
4 changed files with 25 additions and 13 deletions

View File

@ -12,9 +12,20 @@ const endpoints = {
// Use path.join(__dirname, '..', UPLOAD_FS_DIR, fileName) in code // Use path.join(__dirname, '..', UPLOAD_FS_DIR, fileName) in code
const UPLOAD_FS_DIR = 'data/images'; const UPLOAD_FS_DIR = 'data/images';
// Filesystem directory (relative to backend/src) where preview images will be stored
// Use path.join(__dirname, '..', PREVIEW_FS_DIR, fileName) in code
const PREVIEW_FS_DIR = 'data/previews';
// Preview generation configuration
const PREVIEW_CONFIG = {
maxWidth: 800, // Maximum width in pixels
quality: 85, // JPEG quality (0-100)
format: 'jpeg' // Output format
};
const time = { const time = {
HOURS_24: 86400000, HOURS_24: 86400000,
WEEK_1: 604800000 WEEK_1: 604800000
}; };
module.exports = { endpoints, time, UPLOAD_FS_DIR }; module.exports = { endpoints, time, UPLOAD_FS_DIR, PREVIEW_FS_DIR, PREVIEW_CONFIG };

View File

@ -90,6 +90,17 @@ class DatabaseManager {
`); `);
console.log('✓ Images Tabelle erstellt'); console.log('✓ Images Tabelle erstellt');
// Migration: Füge preview_path Feld zur images Tabelle hinzu (falls nicht vorhanden)
try {
await this.run('ALTER TABLE images ADD COLUMN preview_path TEXT');
console.log('✓ preview_path Feld zur images Tabelle hinzugefügt');
} catch (error) {
// Feld existiert bereits - das ist okay
if (!error.message.includes('duplicate column')) {
console.warn('Migration Warnung:', error.message);
}
}
// Erstelle Indizes // Erstelle Indizes
await this.run('CREATE INDEX IF NOT EXISTS idx_groups_group_id ON groups(group_id)'); await this.run('CREATE INDEX IF NOT EXISTS idx_groups_group_id ON groups(group_id)');
await this.run('CREATE INDEX IF NOT EXISTS idx_groups_year ON groups(year)'); await this.run('CREATE INDEX IF NOT EXISTS idx_groups_year ON groups(year)');

View File

@ -24,6 +24,7 @@ CREATE TABLE IF NOT EXISTS images (
upload_order INTEGER NOT NULL, upload_order INTEGER NOT NULL,
file_size INTEGER, file_size INTEGER,
mime_type TEXT, mime_type TEXT,
preview_path TEXT, -- Path to preview/thumbnail image (added in migration 003)
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (group_id) REFERENCES groups(group_id) ON DELETE CASCADE FOREIGN KEY (group_id) REFERENCES groups(group_id) ON DELETE CASCADE
); );

View File

@ -1,16 +1,7 @@
const sharp = require('sharp'); const sharp = require('sharp');
const path = require('path'); const path = require('path');
const fs = require('fs').promises; const fs = require('fs').promises;
const { PREVIEW_FS_DIR, PREVIEW_CONFIG, UPLOAD_FS_DIR } = require('../constants');
// Preview configuration
const PREVIEW_CONFIG = {
maxWidth: 800,
quality: 85,
format: 'jpeg'
};
// Preview directory relative to backend/src
const PREVIEW_FS_DIR = 'data/previews';
class ImagePreviewService { class ImagePreviewService {
/** /**
@ -121,7 +112,6 @@ class ImagePreviewService {
* @returns {string} Absolute path to original file * @returns {string} Absolute path to original file
*/ */
getOriginalPath(fileName) { getOriginalPath(fileName) {
const UPLOAD_FS_DIR = require('../constants').UPLOAD_FS_DIR;
return path.join(__dirname, '..', UPLOAD_FS_DIR, fileName); return path.join(__dirname, '..', UPLOAD_FS_DIR, fileName);
} }
@ -132,7 +122,6 @@ class ImagePreviewService {
async cleanupOrphanedPreviews() { async cleanupOrphanedPreviews() {
try { try {
const previewDir = path.join(__dirname, '..', PREVIEW_FS_DIR); const previewDir = path.join(__dirname, '..', PREVIEW_FS_DIR);
const UPLOAD_FS_DIR = require('../constants').UPLOAD_FS_DIR;
const originalDir = path.join(__dirname, '..', UPLOAD_FS_DIR); const originalDir = path.join(__dirname, '..', UPLOAD_FS_DIR);
// Check if preview directory exists // Check if preview directory exists