|
| 1 | +/** |
| 2 | + * Server-side audio proxy for S3-hosted media files. |
| 3 | + * |
| 4 | + * In production the Django backend stores media on S3 and returns pre-signed |
| 5 | + * S3 URLs. Browser-side fetches (especially from Web Workers) fail because |
| 6 | + * the S3 bucket has no CORS policy for the frontend origin. This route |
| 7 | + * fetches on the server (Cloudflare Worker / Node dev server) where CORS |
| 8 | + * does not apply and streams the result back same-origin. |
| 9 | + */ |
| 10 | +export default async function handler(req, res) { |
| 11 | + if (req.method !== 'GET') { |
| 12 | + res.setHeader('Allow', 'GET'); |
| 13 | + return res.status(405).end(); |
| 14 | + } |
| 15 | + |
| 16 | + const { url } = req.query; |
| 17 | + if (!url) return res.status(400).json({ error: 'Missing url parameter' }); |
| 18 | + |
| 19 | + // Only allow known-safe destinations |
| 20 | + let parsed; |
| 21 | + try { |
| 22 | + parsed = new URL(url); |
| 23 | + } catch { |
| 24 | + return res.status(400).json({ error: 'Invalid URL' }); |
| 25 | + } |
| 26 | + |
| 27 | + const allowed = |
| 28 | + parsed.hostname.endsWith('.amazonaws.com') || |
| 29 | + parsed.hostname.endsWith('.musiccpr.org') || |
| 30 | + parsed.hostname === 'localhost'; |
| 31 | + if (!allowed) return res.status(403).json({ error: 'Domain not allowed' }); |
| 32 | + |
| 33 | + try { |
| 34 | + const upstream = await fetch(url); |
| 35 | + if (!upstream.ok) return res.status(upstream.status).end(); |
| 36 | + |
| 37 | + const arrayBuffer = await upstream.arrayBuffer(); |
| 38 | + |
| 39 | + res.setHeader( |
| 40 | + 'Content-Type', |
| 41 | + upstream.headers.get('content-type') || 'audio/mpeg', |
| 42 | + ); |
| 43 | + res.setHeader('Content-Length', arrayBuffer.byteLength); |
| 44 | + res.setHeader('Cache-Control', 'public, max-age=3600'); |
| 45 | + res.send(Buffer.from(arrayBuffer)); |
| 46 | + } catch (err) { |
| 47 | + console.error('audio-proxy: upstream fetch failed', err); |
| 48 | + res.status(502).json({ error: 'Upstream fetch failed' }); |
| 49 | + } |
| 50 | +} |
0 commit comments