24 lines
632 B
TypeScript
24 lines
632 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { readdirSync, existsSync } from "fs";
|
|
import path from "path";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const MEDIA_DIR = "assets/images/media";
|
|
|
|
export async function GET() {
|
|
const dir = path.join(process.cwd(), "public", MEDIA_DIR);
|
|
if (!existsSync(dir)) {
|
|
return NextResponse.json({ files: [] });
|
|
}
|
|
const files = readdirSync(dir)
|
|
.filter((f) => /\.(png|jpg|jpeg|gif|webp|svg|bmp)$/i.test(f))
|
|
.map((f) => ({
|
|
name: f,
|
|
url: `/${MEDIA_DIR}/${f}`,
|
|
}))
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
return NextResponse.json({ files });
|
|
}
|