2026-04-17 15:12:59 +02:00
|
|
|
import type { RequestHandler } from './$types';
|
|
|
|
|
import { json, error } from '@sveltejs/kit';
|
2026-04-18 08:28:02 +02:00
|
|
|
import { z } from 'zod';
|
2026-04-17 15:12:59 +02:00
|
|
|
import { getDb } from '$lib/server/db';
|
2026-04-18 08:28:02 +02:00
|
|
|
import {
|
|
|
|
|
removeDomain,
|
|
|
|
|
updateDomain,
|
|
|
|
|
setDomainFavicon
|
|
|
|
|
} from '$lib/server/domains/repository';
|
|
|
|
|
import { fetchAndStoreFavicon } from '$lib/server/domains/favicons';
|
2026-04-17 15:12:59 +02:00
|
|
|
|
2026-04-18 08:28:02 +02:00
|
|
|
const IMAGE_DIR = process.env.IMAGE_DIR ?? './data/images';
|
|
|
|
|
|
|
|
|
|
const UpdateSchema = z.object({
|
|
|
|
|
domain: z.string().min(3).max(253).optional(),
|
|
|
|
|
display_name: z.string().max(100).nullable().optional()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function parseId(raw: string): number {
|
|
|
|
|
const id = Number(raw);
|
2026-04-17 15:12:59 +02:00
|
|
|
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
|
2026-04-18 08:28:02 +02:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const PATCH: RequestHandler = async ({ params, request }) => {
|
|
|
|
|
const id = parseId(params.id!);
|
|
|
|
|
const body = await request.json().catch(() => null);
|
|
|
|
|
const parsed = UpdateSchema.safeParse(body);
|
|
|
|
|
if (!parsed.success) error(400, { message: 'Invalid body' });
|
|
|
|
|
try {
|
|
|
|
|
const db = getDb();
|
|
|
|
|
const updated = updateDomain(db, id, parsed.data);
|
|
|
|
|
if (!updated) error(404, { message: 'Not found' });
|
|
|
|
|
// Wenn updateDomain favicon_path genullt hat (Domain geändert), frisch laden.
|
|
|
|
|
if (updated.favicon_path === null) {
|
|
|
|
|
const path = await fetchAndStoreFavicon(updated.domain, IMAGE_DIR);
|
|
|
|
|
if (path) {
|
|
|
|
|
setDomainFavicon(db, updated.id, path);
|
|
|
|
|
updated.favicon_path = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return json(updated);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error(409, { message: (e as Error).message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const DELETE: RequestHandler = async ({ params }) => {
|
|
|
|
|
const id = parseId(params.id!);
|
2026-04-17 15:12:59 +02:00
|
|
|
removeDomain(getDb(), id);
|
|
|
|
|
return json({ ok: true });
|
|
|
|
|
};
|