Files
HabitTrove/app/api/avatars/[...path]/route.ts
2025-01-04 14:10:28 -05:00

27 lines
687 B
TypeScript

import { NextResponse } from 'next/server'
import fs from 'fs/promises'
import path from 'path'
export async function GET(
request: Request,
{ params }: { params: Promise<{ path: string[] }> }
) {
try {
const { path: pathSegments } = await Promise.resolve(params)
const filePath = path.join(process.cwd(), 'data', 'avatars', ...(pathSegments || []))
const file = await fs.readFile(filePath)
const ext = path.extname(filePath).slice(1)
return new NextResponse(file, {
headers: {
'Content-Type': `image/${ext}`,
},
})
} catch (error) {
return NextResponse.json(
{ error: 'File not found' },
{ status: 404 }
)
}
}