// Proxy obrázkov pre náhľad produktových kariet: obrázky z iných domén idú cez rovnaký pôvod, // aby sa vizuál karty dal pri exporte uložiť ako obrázok (canvas nesmie byť "tainted" cez CORS). const MAX_BYTES = 15 * 1024 * 1024; export async function GET(request: Request) { const target = new URL(request.url).searchParams.get('url'); if (!target || !/^https?:\/\//i.test(target)) { return Response.json({ error: 'Neplatná adresa obrázka.' }, { status: 400 }); } try { const res = await fetch(target, { redirect: 'follow' }); const contentType = res.headers.get('content-type') || ''; if (!res.ok || !contentType.startsWith('image/')) { return Response.json({ error: `Obrázok sa nepodarilo načítať (${res.status}).`, upstreamStatus: res.status }, { status: 502 }); } const body = await res.arrayBuffer(); if (body.byteLength > MAX_BYTES) { return Response.json({ error: 'Obrázok je príliš veľký.' }, { status: 413 }); } return new Response(body, { headers: { 'content-type': contentType, 'cache-control': 'public, max-age=86400' } }); } catch (err: unknown) { const message = err instanceof Error ? err.message : 'Chyba pri načítaní obrázka'; return Response.json({ error: message }, { status: 502 }); } }