34 lines
858 B
TypeScript
34 lines
858 B
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
const ALLOWED_HOSTS = [
|
|
'firebasestorage.googleapis.com',
|
|
'storage.googleapis.com',
|
|
]
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const url = req.nextUrl.searchParams.get('url')
|
|
if (!url) return new NextResponse('Missing url', { status: 400 })
|
|
|
|
let parsed: URL
|
|
try {
|
|
parsed = new URL(url)
|
|
} catch {
|
|
return new NextResponse('Invalid url', { status: 400 })
|
|
}
|
|
|
|
if (!ALLOWED_HOSTS.includes(parsed.hostname)) {
|
|
return new NextResponse('Forbidden', { status: 403 })
|
|
}
|
|
|
|
const res = await fetch(url)
|
|
if (!res.ok) return new NextResponse('Upstream error', { status: res.status })
|
|
|
|
const body = await res.arrayBuffer()
|
|
return new NextResponse(body, {
|
|
headers: {
|
|
'Content-Type': 'application/pdf',
|
|
'Cache-Control': 'public, max-age=3600',
|
|
},
|
|
})
|
|
}
|