import { upcomingEvents } from '$lib/server/content'; import { getSettings } from '$lib/server/settings'; import { addDays, today } from '$lib/dates'; /** Kalender-Abo (iCal) – z. B. für Handy-Kalender oder Outlook. */ export const GET = ({ url }) => { const s = getSettings(); const esc = (v: string) => v.replace(/\\/g, '\\\\').replace(/;/g, '\;').replace(/,/g, '\\,').replace(/\r?\n/g, '\\n'); const d = (iso: string) => iso.replaceAll('-', ''); const t = (time: string) => time.replace(':', '') + '00'; const stamp = new Date().toISOString().replace(/[-:]/g, '').replace(/\.\d+/, ''); const lines = [ 'BEGIN:VCALENDAR', 'VERSION:2.0', `PRODID:-//${esc(s.appName)}//Termine//DE`, 'CALSCALE:GREGORIAN', `X-WR-CALNAME:${esc(s.appName)}`, 'X-WR-TIMEZONE:Europe/Berlin' ]; for (const e of upcomingEvents({ from: addDays(today(), -60) })) { const end = e.endDate || e.startDate; lines.push('BEGIN:VEVENT', `UID:event-${e.id}@${url.host}`, `DTSTAMP:${stamp}`); if (e.startTime && end === e.startDate) { lines.push(`DTSTART;TZID=Europe/Berlin:${d(e.startDate)}T${t(e.startTime)}`); lines.push(`DTEND;TZID=Europe/Berlin:${d(end)}T${t(e.endTime || e.startTime)}`); } else { lines.push(`DTSTART;VALUE=DATE:${d(e.startDate)}`, `DTEND;VALUE=DATE:${d(addDays(end, 1))}`); } lines.push(`SUMMARY:${esc(e.kind === 'closure' ? `Geschlossen: ${e.title}` : e.title)}`); if (e.location) lines.push(`LOCATION:${esc(e.location)}`); if (e.description) lines.push(`DESCRIPTION:${esc(e.description)}`); lines.push('END:VEVENT'); } lines.push('END:VCALENDAR'); return new Response(lines.join('\r\n'), { headers: { 'Content-Type': 'text/calendar; charset=utf-8', 'Content-Disposition': 'inline; filename="termine.ics"', 'Cache-Control': 'public, max-age=900' } }); };