2026-04-17 15:12:59 +02:00
|
|
|
import type { RequestHandler } from './$types';
|
2026-04-18 22:19:12 +02:00
|
|
|
import { json } from '@sveltejs/kit';
|
2026-04-17 15:12:59 +02:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
import { getDb } from '$lib/server/db';
|
2026-04-18 22:19:12 +02:00
|
|
|
import { validateBody } from '$lib/server/api-helpers';
|
2026-04-17 15:12:59 +02:00
|
|
|
import { importRecipe } from '$lib/server/recipes/importer';
|
|
|
|
|
import { mapImporterError } from '$lib/server/errors';
|
2026-04-18 22:41:02 +02:00
|
|
|
import { IMAGE_DIR } from '$lib/server/paths';
|
2026-04-17 15:12:59 +02:00
|
|
|
|
|
|
|
|
const ImportSchema = z.object({ url: z.string().url() });
|
|
|
|
|
|
|
|
|
|
export const POST: RequestHandler = async ({ request }) => {
|
2026-04-18 22:19:12 +02:00
|
|
|
const data = validateBody(await request.json().catch(() => null), ImportSchema);
|
2026-04-17 15:12:59 +02:00
|
|
|
try {
|
2026-04-18 22:19:12 +02:00
|
|
|
const result = await importRecipe(getDb(), IMAGE_DIR, data.url);
|
2026-04-17 15:12:59 +02:00
|
|
|
return json({ id: result.id, duplicate: result.duplicate });
|
|
|
|
|
} catch (e) {
|
|
|
|
|
mapImporterError(e);
|
|
|
|
|
}
|
|
|
|
|
};
|