- entrypoint.sh: ImageMagick 7 nutzt 'magick' statt 'convert', Auto-Detection - database.js: Migration fuer role-Spalte bei bestehender DB (ALTER TABLE) - main.js: Fallback auf Vollbild wenn Thumbnail fehlt (error handler) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
const sqlite3 = require('sqlite3').verbose();
|
|
const path = require('path');
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
const dbPath = process.env.DB_PATH || path.join(__dirname, '../data/database.sqlite');
|
|
const db = new sqlite3.Database(dbPath);
|
|
|
|
console.log('Using database at', dbPath);
|
|
|
|
db.serialize(() => {
|
|
// Users: mit created_at fuer Client-Ablauf (30 Tage)
|
|
db.run(`CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT UNIQUE NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
role TEXT NOT NULL DEFAULT 'client',
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
)`);
|
|
|
|
// Migration: Spalten hinzufuegen falls Tabelle schon existiert (alte DB ohne role/created_at)
|
|
db.run(`ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'client'`, (err) => {
|
|
// Ignoriere Fehler wenn Spalte schon existiert
|
|
if (!err) {
|
|
// Alte Admins (die einzigen User vor dem Update) als admin markieren
|
|
db.run(`UPDATE users SET role = 'admin' WHERE id = 1`);
|
|
console.log('Migration: role-Spalte hinzugefuegt, User ID 1 als admin gesetzt');
|
|
}
|
|
});
|
|
db.run(`ALTER TABLE users ADD COLUMN created_at DATETIME DEFAULT CURRENT_TIMESTAMP`, (err) => {
|
|
// Ignoriere Fehler wenn Spalte schon existiert
|
|
});
|
|
|
|
// Ordner-Zuweisung (neu)
|
|
db.run(`CREATE TABLE IF NOT EXISTS user_folders (
|
|
user_id INTEGER,
|
|
folder_name TEXT,
|
|
FOREIGN KEY(user_id) REFERENCES users(id),
|
|
UNIQUE(user_id, folder_name)
|
|
)`);
|
|
|
|
// Alte Tabelle behalten fuer Kompatibilitaet, aber nicht mehr nutzen
|
|
db.run(`CREATE TABLE IF NOT EXISTS user_images (
|
|
user_id INTEGER,
|
|
image_name TEXT,
|
|
FOREIGN KEY(user_id) REFERENCES users(id),
|
|
UNIQUE(user_id, image_name)
|
|
)`);
|
|
|
|
// Default-Admin erstellen wenn keiner existiert
|
|
db.get('SELECT * FROM users WHERE username = ?', ['admin'], (err, row) => {
|
|
if (!row) {
|
|
const hash = bcrypt.hashSync('admin123', 10);
|
|
db.run('INSERT INTO users (username, password_hash, role) VALUES (?, ?, ?)', ['admin', hash, 'admin']);
|
|
console.log('Default admin created: admin / admin123');
|
|
}
|
|
});
|
|
});
|
|
|
|
module.exports = db;
|