src/lib/server/paths.ts: zentrale Auflösung der env-vars; vorher 6× IMAGE_DIR und 2× DATABASE_PATH dupliziert mit identischen Defaults. Migrierte Sites: - src/lib/server/db/index.ts (DATABASE_PATH + IMAGE_DIR) - src/routes/api/admin/backup/+server.ts - src/routes/api/domains/+server.ts - src/routes/api/domains/[id]/+server.ts - src/routes/api/recipes/import/+server.ts - src/routes/api/recipes/[id]/image/+server.ts - src/routes/images/[filename]/+server.ts ARCHITECTURE.md: - 49 Flachwitze -> 150 (waren tatsaechlich 150) - 'search/' Route entfernt — wurde nie als eigene Route gebaut, Suche laeuft direkt auf der Homepage via API-Calls Findings aus zweiter Review-Runde (siehe OPEN-ISSUES-NEXT.md)
57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import type { RequestHandler } from './$types';
|
|
import { json, error } from '@sveltejs/kit';
|
|
import { createHash } from 'node:crypto';
|
|
import { existsSync } from 'node:fs';
|
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { getDb } from '$lib/server/db';
|
|
import { parsePositiveIntParam } from '$lib/server/api-helpers';
|
|
import { getRecipeById, updateImagePath } from '$lib/server/recipes/repository';
|
|
import { IMAGE_DIR } from '$lib/server/paths';
|
|
|
|
const MAX_BYTES = 10 * 1024 * 1024;
|
|
|
|
const EXT_BY_MIME: Record<string, string> = {
|
|
'image/jpeg': '.jpg',
|
|
'image/jpg': '.jpg',
|
|
'image/png': '.png',
|
|
'image/webp': '.webp',
|
|
'image/gif': '.gif',
|
|
'image/avif': '.avif'
|
|
};
|
|
|
|
export const POST: RequestHandler = async ({ params, request }) => {
|
|
const id = parsePositiveIntParam(params.id, 'id');
|
|
const db = getDb();
|
|
if (!getRecipeById(db, id)) error(404, { message: 'Recipe not found' });
|
|
|
|
const form = await request.formData().catch(() => null);
|
|
const file = form?.get('file');
|
|
if (!(file instanceof File)) error(400, { message: 'Field "file" missing' });
|
|
if (file.size === 0) error(400, { message: 'Empty file' });
|
|
if (file.size > MAX_BYTES) error(413, { message: 'Image too large (max 10 MB)' });
|
|
|
|
const mime = file.type.toLowerCase();
|
|
const ext = EXT_BY_MIME[mime];
|
|
if (!ext) error(415, { message: `Image format ${file.type || 'unknown'} not supported` });
|
|
|
|
const buf = Buffer.from(await file.arrayBuffer());
|
|
const hash = createHash('sha256').update(buf).digest('hex');
|
|
const filename = `${hash}${ext}`;
|
|
const target = join(IMAGE_DIR, filename);
|
|
if (!existsSync(target)) {
|
|
await mkdir(IMAGE_DIR, { recursive: true });
|
|
await writeFile(target, buf);
|
|
}
|
|
updateImagePath(db, id, filename);
|
|
return json({ ok: true, image_path: filename });
|
|
};
|
|
|
|
export const DELETE: RequestHandler = ({ params }) => {
|
|
const id = parsePositiveIntParam(params.id, 'id');
|
|
const db = getDb();
|
|
if (!getRecipeById(db, id)) error(404, { message: 'Recipe not found' });
|
|
updateImagePath(db, id, null);
|
|
return json({ ok: true });
|
|
};
|