import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { presignAudio } from '$lib/server/minio'; import { log } from '$lib/server/logger'; /** * GET /api/presign/audio?slug=...&n=...&voice=... * Returns a presigned MinIO URL for the audio file so the browser * can stream it directly without going through the server. * Returns 404 when the audio has not been generated yet. */ export const GET: RequestHandler = async ({ url }) => { const slug = url.searchParams.get('slug'); // Accept both 'n' (web) and 'chapter' (iOS) as the chapter number param const n = parseInt(url.searchParams.get('n') ?? url.searchParams.get('chapter') ?? '', 10); const voice = url.searchParams.get('voice') ?? undefined; if (!slug || !n || n < 1) { error(400, 'Missing slug or n'); } try { const presignedUrl = await presignAudio(slug, n, voice); return json({ url: presignedUrl }); } catch (e) { const status = (e as { status?: number }).status; if (status === 404) { error(404, 'Audio not found'); } log.error('presign', 'presign audio failed', { slug, n, err: String(e) }); error(500, `Could not get presigned URL: ${e}`); } };