feat: add image proxy route to serve Directus assets with auth token

This commit is contained in:
achmad
2026-05-29 01:31:54 +07:00
parent 69a5f5f9f5
commit 8f725d5e26
3 changed files with 40 additions and 4 deletions
+35
View File
@@ -0,0 +1,35 @@
import { NextRequest, NextResponse } from 'next/server'
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params
const { searchParams } = new URL(req.url)
const width = searchParams.get('width') || '800'
const quality = searchParams.get('quality') || '80'
const directusUrl = process.env.DIRECTUS_URL!
const token = process.env.DIRECTUS_TOKEN!
try {
const res = await fetch(
`${directusUrl}/assets/${id}?width=${width}&quality=${quality}`,
{
headers: { Authorization: `Bearer ${token}` },
},
)
if (!res.ok) {
return NextResponse.json({ error: 'Image not found' }, { status: 404 })
}
const headers = new Headers()
headers.set('Content-Type', res.headers.get('Content-Type') || 'image/jpeg')
headers.set('Cache-Control', 'public, max-age=31536000, immutable')
return new NextResponse(res.body, { status: 200, headers })
} catch {
return NextResponse.json({ error: 'Failed to fetch image' }, { status: 502 })
}
}