import { resolve } from 'node:path' import { z } from 'zod' import sanitizeHtml from 'sanitize-html' export const storage = resolve(process.env.DATA_DIR || './data') export const uploadLimit = Number(process.env.MAX_UPLOAD_MB || 100) * 1024 * 1024 if (!Number.isSafeInteger(uploadLimit) || uploadLimit < 1024 * 1024) { throw new Error('MAX_UPLOAD_MB muss mindestens 1 sein.') } export const layoutLimits = { single: 1, split: 2, focus: 3, grid: 8 } const title = z.string().trim().min(1).max(100) const timeZone = z.string().max(80).refine(value => { try { new Intl.DateTimeFormat('de', { timeZone: value }); return true } catch { return false } }) const schedule = { from: z.iso.datetime().nullable().default(null), until: z.iso.datetime().nullable().default(null), } const scroll = z.object({ enabled: z.boolean(), speed: z.number().int().min(10).max(220), pause: z.number().min(0).max(30), }) const htmlOptions = { allowedTags: [...sanitizeHtml.defaults.allowedTags, 'h1', 'h2', 'img', 'mark'], allowedAttributes: { '*': ['class', 'style'], img: ['src', 'alt', 'width', 'height'], td: ['colspan', 'rowspan'], th: ['colspan', 'rowspan', 'scope'] }, allowedSchemes: ['https'], allowProtocolRelative: false, allowedStyles: { '*': { color: [/^#[0-9a-f]{3,8}$/i], 'background-color': [/^#[0-9a-f]{3,8}$/i], 'text-align': [/^(left|right|center|justify)$/], 'font-size': [/^\d+(\.\d+)?(rem|em|px|%)$/], 'font-weight': [/^(normal|bold|[1-9]00)$/], }, }, transformTags: { a: 'span' }, allowedClasses: { '*': ['eyebrow'] }, } const widgetBase = { id: z.uuid(), title, showTitle: z.boolean(), ...schedule } const widget = z.discriminatedUnion('type', [ z.object({ ...widgetBase, type: z.literal('html'), html: z.string().max(100_000).transform(value => sanitizeHtml(value, htmlOptions)), scroll }), z.object({ ...widgetBase, type: z.literal('pdf'), assetId: z.uuid(), mode: z.enum(['scroll', 'pages', 'static']), seconds: z.number().int().min(2).max(120), scroll }), z.object({ ...widgetBase, type: z.literal('gallery'), assetIds: z.array(z.uuid()).min(1).max(60), seconds: z.number().int().min(2).max(120), fit: z.enum(['cover', 'contain']) }), z.object({ ...widgetBase, type: z.literal('clock'), seconds: z.boolean(), date: z.boolean() }), ]) export const boardSchema = z.object({ name: title, location: z.string().trim().max(100), layout: z.enum(['single', 'split', 'focus', 'grid']), theme: z.enum(['midnight', 'light', 'forest']), showHeader: z.boolean(), showClock: z.boolean(), timeZone, widgets: z.array(widget).max(8), }).refine(board => board.widgets.length <= layoutLimits[board.layout], { message: 'Das Layout bietet zu wenig Platz.', path: ['layout'] }) .refine(board => new Set(board.widgets.map(item => item.id)).size === board.widgets.length, { message: 'Bereichs-IDs müssen eindeutig sein.', path: ['widgets'] }) .refine(board => board.widgets.every(item => !item.from || !item.until || Date.parse(item.from) < Date.parse(item.until)), { message: 'Das Ende muss nach dem Start liegen.', path: ['widgets'] }) export const email = z.string().trim().toLowerCase().max(254).pipe(z.email()) export const password = z.string().min(12).max(256) export const userSchema = z.object({ name: title, email, role: z.enum(['admin', 'editor']), active: z.boolean().default(true) }) export function references(board) { return new Map(board.widgets.flatMap(item => { if (item.type === 'pdf') return [[item.assetId, 'pdf']] if (item.type === 'gallery') return item.assetIds.map(id => [id, 'image']) return [] })) }