0cb315ea88
CI / build (push) Has been cancelled
Event photos can now be opened in Lumenroom, a WebGL2 develop module, from the photo editor's Advanced button. The dashboard signs a short-lived token scoped to one user and event; Lumenroom exchanges it, imports the event's photo list and proxies bytes on demand, so nothing is copied and the temporary catalog expires with the bridge session. Saving in Lumenroom renders the photo and writes it back as the event's edited JPEG, with the advanced recipe kept in event_photos.advanced_edit_json. Lumenroom is vendored under lumenroom/ and built as a second compose service, so one clone and `docker compose up -d --build` deploys both apps. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
178 lines
5.2 KiB
JavaScript
178 lines
5.2 KiB
JavaScript
import fs from 'node:fs';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
import { DATA_DIR, DB_PATH } from './config.mjs';
|
|
|
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
|
|
export const db = new DatabaseSync(DB_PATH);
|
|
|
|
db.exec(`
|
|
PRAGMA journal_mode = WAL;
|
|
PRAGMA foreign_keys = ON;
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id TEXT PRIMARY KEY,
|
|
email TEXT NOT NULL UNIQUE,
|
|
name TEXT NOT NULL DEFAULT '',
|
|
pass_hash TEXT NOT NULL,
|
|
role TEXT NOT NULL DEFAULT 'member',
|
|
created_at INTEGER NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
token TEXT PRIMARY KEY,
|
|
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
created_at INTEGER NOT NULL,
|
|
expires_at INTEGER NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS events (
|
|
id TEXT PRIMARY KEY,
|
|
sort_index REAL NOT NULL DEFAULT 0,
|
|
created_at INTEGER NOT NULL,
|
|
data TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS photos (
|
|
id TEXT PRIMARY KEY,
|
|
event_id TEXT NOT NULL DEFAULT '',
|
|
sort_index REAL NOT NULL DEFAULT 0,
|
|
updated_at INTEGER NOT NULL,
|
|
raw INTEGER NOT NULL DEFAULT 0,
|
|
data TEXT NOT NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS photos_event ON photos(event_id);
|
|
CREATE INDEX IF NOT EXISTS photos_updated ON photos(updated_at);
|
|
|
|
CREATE TABLE IF NOT EXISTS kv (
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS bridge_events (
|
|
event_id TEXT PRIMARY KEY,
|
|
session_id TEXT NOT NULL,
|
|
token TEXT NOT NULL,
|
|
glenloch_url TEXT NOT NULL,
|
|
source_event_id TEXT NOT NULL,
|
|
return_url TEXT,
|
|
expires_at INTEGER NOT NULL,
|
|
created_at INTEGER NOT NULL
|
|
);
|
|
`);
|
|
|
|
const q = {
|
|
upsertEvent: db.prepare(`
|
|
INSERT INTO events (id, sort_index, created_at, data) VALUES (?, ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET sort_index = excluded.sort_index, data = excluded.data
|
|
`),
|
|
deleteEvent: db.prepare('DELETE FROM events WHERE id = ?'),
|
|
allEvents: db.prepare('SELECT data FROM events ORDER BY sort_index ASC, created_at ASC'),
|
|
|
|
upsertPhoto: db.prepare(`
|
|
INSERT INTO photos (id, event_id, sort_index, updated_at, raw, data) VALUES (?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
event_id = excluded.event_id,
|
|
sort_index = excluded.sort_index,
|
|
updated_at = excluded.updated_at,
|
|
raw = excluded.raw,
|
|
data = excluded.data
|
|
`),
|
|
deletePhoto: db.prepare('DELETE FROM photos WHERE id = ?'),
|
|
allPhotos: db.prepare('SELECT data FROM photos ORDER BY sort_index ASC, updated_at ASC'),
|
|
photo: db.prepare('SELECT data FROM photos WHERE id = ?'),
|
|
photoCount: db.prepare('SELECT COUNT(*) AS n FROM photos'),
|
|
|
|
findUserByEmail: db.prepare('SELECT * FROM users WHERE email = ?'),
|
|
insertUser: db.prepare(
|
|
'INSERT INTO users (id, email, name, pass_hash, role, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
|
),
|
|
userCount: db.prepare('SELECT COUNT(*) AS n FROM users'),
|
|
findUserById: db.prepare('SELECT id, email, name, role, created_at FROM users WHERE id = ?'),
|
|
|
|
insertSession: db.prepare(
|
|
'INSERT INTO sessions (token, user_id, created_at, expires_at) VALUES (?, ?, ?, ?)',
|
|
),
|
|
findSession: db.prepare('SELECT * FROM sessions WHERE token = ?'),
|
|
deleteSession: db.prepare('DELETE FROM sessions WHERE token = ?'),
|
|
purgeSessions: db.prepare('DELETE FROM sessions WHERE expires_at < ?'),
|
|
|
|
kvGet: db.prepare('SELECT value FROM kv WHERE key = ?'),
|
|
kvSet: db.prepare(
|
|
'INSERT INTO kv (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value',
|
|
),
|
|
};
|
|
|
|
function parse(json) {
|
|
if (!json) return null;
|
|
try {
|
|
return JSON.parse(json);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function listEvents() {
|
|
return q.allEvents.all().map((r) => parse(r.data)).filter((r) => r !== null);
|
|
}
|
|
|
|
export function saveEvent(record, sortIndex = 0) {
|
|
q.upsertEvent.run(record.id, sortIndex, record.createdAt ?? Date.now(), JSON.stringify(record));
|
|
}
|
|
|
|
export function removeEvent(id) {
|
|
q.deleteEvent.run(id);
|
|
}
|
|
|
|
export function listPhotos() {
|
|
return q.allPhotos.all().map((r) => parse(r.data)).filter((r) => r !== null);
|
|
}
|
|
|
|
export function getPhoto(id) {
|
|
const row = q.photo.get(id);
|
|
return row ? parse(row.data) : null;
|
|
}
|
|
|
|
export function savePhoto(record, updatedAt = Date.now()) {
|
|
q.upsertPhoto.run(
|
|
record.id,
|
|
record.eventId ?? '',
|
|
typeof record.order === 'number' ? record.order : 0,
|
|
updatedAt,
|
|
record.raw ? 1 : 0,
|
|
JSON.stringify(record),
|
|
);
|
|
}
|
|
|
|
export function removePhoto(id) {
|
|
q.deletePhoto.run(id);
|
|
}
|
|
|
|
export function countPhotos() {
|
|
return Number(q.photoCount.get()?.n ?? 0);
|
|
}
|
|
|
|
export const users = {
|
|
count: () => Number(q.userCount.get()?.n ?? 0),
|
|
byEmail: (email) => q.findUserByEmail.get(String(email).toLowerCase()),
|
|
byId: (id) => q.findUserById.get(id),
|
|
create: (user) =>
|
|
q.insertUser.run(user.id, user.email.toLowerCase(), user.name, user.passHash, user.role, Date.now()),
|
|
};
|
|
|
|
export const sessions = {
|
|
create: (token, userId, expiresAt) => q.insertSession.run(token, userId, Date.now(), expiresAt),
|
|
find: (token) => q.findSession.get(token),
|
|
destroy: (token) => q.deleteSession.run(token),
|
|
purge: () => q.purgeSessions.run(Date.now()),
|
|
};
|
|
|
|
export const kv = {
|
|
get: (key) => {
|
|
const row = q.kvGet.get(key);
|
|
return row ? parse(row.value) : null;
|
|
},
|
|
set: (key, value) => q.kvSet.run(key, JSON.stringify(value)),
|
|
};
|