import { error } from '@sveltejs/kit'; import { getStorage } from '$lib/server/storage'; import { LocalDiskStorage } from '$lib/server/storage/local'; import type { RequestHandler } from './$types'; export const GET: RequestHandler = async ({ url }) => { const storage = getStorage(); if (!(storage instanceof LocalDiskStorage)) { throw error(501, 'Non-local storage adapter must use its own signed URL'); } let verified: { key: string; disposition: string; filename: string }; try { verified = storage.verifySignedUrl(url.searchParams); } catch { throw error(403, 'Invalid or expired link'); } const obj = await storage.get(verified.key).catch(() => null); if (!obj) throw error(404, 'Not found'); const headers = new Headers({ 'content-type': obj.contentType, 'content-length': String(obj.sizeBytes) }); if (verified.filename) { const dispHeader = verified.disposition === 'attachment' ? `attachment; filename="${verified.filename.replace(/"/g, '')}"` : `inline; filename="${verified.filename.replace(/"/g, '')}"`; headers.set('content-disposition', dispHeader); } return new Response(obj.stream as unknown as ReadableStream, { headers }); };