Move media storage from public/assets/images/media to storage/media (outside Git and public/), so uploaded images, favicons and logos are preserved across rebuilds and git clean. Add a shared media-storage helper, update the upload/serve actions and API routes, and gitignore storage/.
25 lines
729 B
TypeScript
25 lines
729 B
TypeScript
import { existsSync, readdirSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { NextResponse } from "next/server";
|
|
import { MEDIA_ROOT } from "@/lib/media-storage";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET() {
|
|
const dir = MEDIA_ROOT;
|
|
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
if (!existsSync(dir)) {
|
|
return NextResponse.json({ files: [] });
|
|
}
|
|
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
const files = readdirSync(dir)
|
|
.filter((f) => /\.(png|jpg|jpeg|gif|webp|svg|bmp)$/i.test(f))
|
|
.map((f) => ({
|
|
name: f,
|
|
url: `/api/media/${f}`,
|
|
}))
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
return NextResponse.json({ files });
|
|
}
|