Werbefreie, barrierearme Info-App für Werkstätten (WfbM) auf Basis von SvelteKit, Tailwind, SQLite/Drizzle: - App: Startseite mit Kacheln, Meldungen, Seitenbaum, Termine inkl. Schließzeiten und iCal-Abo, Ansprechpartner, Suche, Kontaktformular, Merkliste, Vorlesen, Leichte Sprache, Schriftgrößen, Hell/Dunkel, PWA - Redaktion: Dashboard, Meldungen, Seiten, Termine, Kacheln, Ansprechpartner, Mediathek mit Bildoptimierung, Postfach, Benutzer mit Rollen, Einstellungen (Name, Logo, Farbe, Rechtstexte) - Demo-Seed, CLI zum Anlegen von Benutzern, Dockerfile und Compose Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
78 lines
2.5 KiB
TypeScript
78 lines
2.5 KiB
TypeScript
import { error } from '@sveltejs/kit';
|
|
import { and, eq, ne } from 'drizzle-orm';
|
|
import type { SQLiteColumn, SQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
import { db } from './db';
|
|
import type { SessionUser } from './auth';
|
|
import type { Attachment } from './db/schema';
|
|
|
|
/** Bequemes, sicheres Auslesen von Formularfeldern. */
|
|
export function fields(form: FormData) {
|
|
const raw = (k: string) => {
|
|
const v = form.get(k);
|
|
return typeof v === 'string' ? v : '';
|
|
};
|
|
const json = <T>(k: string, fallback: T): T => {
|
|
try {
|
|
return JSON.parse(raw(k)) ?? fallback;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
};
|
|
return {
|
|
str: (k: string, max = 200) => raw(k).trim().slice(0, max),
|
|
html: (k: string) => raw(k).slice(0, 500_000),
|
|
int: (k: string): number | null => {
|
|
const v = raw(k).trim();
|
|
const n = Number(v);
|
|
return v !== '' && Number.isFinite(n) ? Math.trunc(n) : null;
|
|
},
|
|
bool: (k: string) => ['on', 'true', '1'].includes(raw(k)),
|
|
ids: (k: string): number[] => json<unknown[]>(k, []).filter((x): x is number => Number.isInteger(x)),
|
|
attachments: (k: string): Attachment[] =>
|
|
json<unknown[]>(k, [])
|
|
.filter((a): a is Attachment => !!a && typeof a === 'object' && Number.isInteger((a as Attachment).mediaId))
|
|
.map((a) => ({ mediaId: a.mediaId, title: String(a.title ?? '').slice(0, 200) }))
|
|
};
|
|
}
|
|
|
|
export function slugify(input: string): string {
|
|
return (
|
|
input
|
|
.toLowerCase()
|
|
.replace(/ä/g, 'ae')
|
|
.replace(/ö/g, 'oe')
|
|
.replace(/ü/g, 'ue')
|
|
.replace(/ß/g, 'ss')
|
|
.normalize('NFKD')
|
|
.replace(/[̀-ͯ]/g, '')
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.slice(0, 80) || 'eintrag'
|
|
);
|
|
}
|
|
|
|
/** Macht einen Slug eindeutig (text, text-2, text-3 …). */
|
|
export function uniqueSlug(table: SQLiteTable & { slug: SQLiteColumn; id: SQLiteColumn }, wanted: string, excludeId?: number) {
|
|
const base = slugify(wanted);
|
|
for (let i = 1; ; i++) {
|
|
const candidate = i === 1 ? base : `${base}-${i}`;
|
|
const clash = db
|
|
.select({ id: table.id })
|
|
.from(table)
|
|
.where(excludeId ? and(eq(table.slug, candidate), ne(table.id, excludeId)) : eq(table.slug, candidate))
|
|
.get();
|
|
if (!clash) return candidate;
|
|
}
|
|
}
|
|
|
|
export function requireAdmin(user: SessionUser | null) {
|
|
if (user?.role !== 'admin') error(403, 'Nur für Administratoren.');
|
|
}
|
|
|
|
/** „neu“ oder numerische ID aus der URL */
|
|
export function parseId(param: string): number | 'new' {
|
|
if (param === 'neu') return 'new';
|
|
const id = Number(param);
|
|
if (!Number.isInteger(id) || id < 1) error(404);
|
|
return id;
|
|
}
|