import { z } from 'zod'; import { makePage, safeUrl } from '../shared/magazine.js'; export const idSchema = z.uuid(); const color = z.string().regex(/^#[0-9a-f]{6}$/i); const frame = z.object({ x: z.number().int().min(-5000).max(5000), y: z.number().int().min(-5000).max(5000), width: z.number().int().min(12).max(1588), height: z.number().int().min(12).max(2244), angle: z.number().min(-180).max(180) }); const appearance = z.object({ color: color.optional(), fill: color.optional(), stroke: color.optional(), strokeWidth: z.number().min(0).max(100).optional(), radius: z.number().min(0).max(800).optional(), opacity: z.number().min(0).max(100).optional(), font: z.enum(['sans', 'serif', 'mono']).optional(), fontSize: z.number().min(6).max(360).optional(), weight: z.number().int().min(100).max(900).optional(), leading: z.number().min(.5).max(4).optional(), tracking: z.number().min(-40).max(75).optional(), fit: z.enum(['cover', 'contain', 'fill']).optional(), position: z.enum(['center', 'top', 'bottom', 'left', 'right']).optional(), }); const block = z.discriminatedUnion('type', [ z.object({ type: z.literal('text'), text: z.string().max(6000), style: z.enum(['body', 'title', 'heading', 'caption']), align: z.enum(['left', 'center', 'right']) }), z.object({ type: z.literal('image'), mediaId: z.union([idSchema, z.literal('')]), alt: z.string().max(200), height: z.number().min(12).max(135) }), z.object({ type: z.literal('gallery'), mediaIds: z.array(idSchema).max(12), height: z.number().min(12).max(100) }), z.object({ type: z.literal('video'), mediaId: z.union([idSchema, z.literal('')]), posterMediaId: z.union([idSchema, z.literal('')]).optional(), text: z.string().max(200) }), z.object({ type: z.literal('shape'), shape: z.enum(['rectangle', 'ellipse', 'line']) }), z.object({ type: z.literal('html'), markup: z.string().max(60000) }), ...['button', 'qr', 'social'].map((type) => z.object({ type: z.literal(type), text: z.string().max(200), url: z.string().max(500) })), ].map((schema) => schema.extend({ id: idSchema, width: z.enum(['full', 'half']), frame: frame.optional(), appearance: appearance.optional() }))); export const metadata = z.object({ title: z.string().trim().min(1, 'Gib deinem Magazin einen Titel.').max(100), issue: z.string().trim().max(100), description: z.string().trim().max(1000), cover: z.union([idSchema, z.literal('')]), }); export const draftSchema = metadata.extend({ pages: z.array(z.object({ id: idSchema, title: z.string().trim().min(1, 'Jede Seite benötigt einen Titel.').max(100), layout: z.enum(['free', 'single', 'columns', 'cover']), background: color, color, blocks: z.array(block).max(60), })).max(120), }).refine((draft) => { const ids = draft.pages.flatMap((page) => [page.id, ...page.blocks.map((item) => item.id)]); return new Set(ids).size === ids.length; }, 'Seiten und Elemente müssen eindeutige IDs haben.'); export const baseDraft = (title, issue = '', description = '') => ({ title, issue, description, cover: '', pages: [makePage('Titelseite')], }); export function slugFor(title) { return title.toLowerCase().replaceAll('ß', 'ss').normalize('NFKD').replace(/\p{M}/gu, '') .replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '').slice(0, 65).replace(/-$/, '') || 'magazin'; } export class HttpError extends Error { constructor(status, message) { super(message); this.status = status; } } export async function validatePublication(db, draft) { const document = draftSchema.parse(draft); if (!document.pages.length) throw new HttpError(422, 'Lege mindestens eine Seite an.'); const expected = new Map(); if (document.cover) expected.set(document.cover, 'image/'); for (const page of document.pages) { const label = `„${page.title}“`; if (!page.blocks.length) throw new HttpError(422, `${label} ist leer.`); for (const item of page.blocks) { if (item.type === 'text' && !item.text.trim()) throw new HttpError(422, `${label} enthält ein leeres Textfeld.`); if (item.type === 'html' && !item.markup.trim()) throw new HttpError(422, `${label} enthält einen leeren HTML-Block.`); if (['button', 'qr', 'social'].includes(item.type) && !safeUrl(item.url, item.type === 'social')) { throw new HttpError(422, `${label} enthält einen ungültigen Link.`); } if (['button', 'social'].includes(item.type) && !item.text.trim()) { throw new HttpError(422, `${label} enthält einen Link ohne Beschriftung.`); } const ids = item.type === 'gallery' ? item.mediaIds : [item.mediaId].filter(Boolean); if (['image', 'gallery', 'video'].includes(item.type) && !ids.length) { throw new HttpError(422, `${label} enthält ein Element ohne Medium.`); } const refs = ids.map((id) => [id, item.type === 'video' ? 'video/' : 'image/']); if (item.posterMediaId) refs.push([item.posterMediaId, 'image/']); for (const [id, kind] of refs) { if (expected.has(id) && expected.get(id) !== kind) throw new HttpError(422, `${label} verwendet ein Medium im falschen Format.`); expected.set(id, kind); } } } const result = await db.query('SELECT * FROM media WHERE id = ANY($1) AND deleted_at IS NULL', [[...expected.keys()]]); if (result.rowCount !== expected.size) throw new HttpError(422, 'Ein verwendetes Medium fehlt oder liegt im Papierkorb.'); if (result.rows.some((file) => !file.mime.startsWith(expected.get(file.id)))) throw new HttpError(422, 'Ein verwendetes Medium hat das falsche Format.'); return result.rows; }