feat: add image serving endpoint for rooms gallery
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||||
|
try {
|
||||||
|
const id = parseInt(params.id, 10);
|
||||||
|
if (isNaN(id)) {
|
||||||
|
return NextResponse.json({ error: 'Invalid image ID' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { rows } = await db.query(
|
||||||
|
'SELECT filename, data, category FROM images WHERE id = $1',
|
||||||
|
[id]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
return NextResponse.json({ error: 'Image not found' }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const image = rows[0];
|
||||||
|
const buffer = Buffer.from(image.data);
|
||||||
|
const contentType = getContentType(image.category, image.filename);
|
||||||
|
|
||||||
|
return new NextResponse(buffer, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': contentType,
|
||||||
|
'Cache-Control': 'public, max-age=86400',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (err: any) {
|
||||||
|
return NextResponse.json({ error: err.message || 'Failed to fetch image' }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getContentType(category: string, filename: string): string {
|
||||||
|
if (category === 'photo') return 'image/jpeg';
|
||||||
|
if (category === 'slide') return 'image/webp';
|
||||||
|
if (filename.endsWith('.png')) return 'image/png';
|
||||||
|
if (filename.endsWith('.webp')) return 'image/webp';
|
||||||
|
return 'image/jpeg';
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user