Files
Epicnabbo-Catalogus-Updated…/src/app/api/admin/catalog/images/route.ts
T

58 lines
1.5 KiB
TypeScript

import { promises as fs } from "node:fs";
import path from "node:path";
import { withAdmin } from "@/lib/api-handler";
import { apiOk } from "@/lib/api-response";
import { PERMS } from "@/lib/permissions";
const CATALOGUE_DIR = path.join(
/*turbopackIgnore: true*/ process.cwd(),
"public",
"swf",
"c_images",
"catalogue",
);
export const GET = withAdmin(
{ permission: PERMS.CATALOG_VIEW },
async (request) => {
const sp = request.nextUrl.searchParams;
const limit = Math.min(
Math.max(Number(sp.get("limit") || 80) || 80, 1),
500,
);
const offset = Math.max(Number(sp.get("offset") || 0) || 0, 0);
const search = (sp.get("search") || "").trim().toLowerCase();
const type = sp.get("type") || "all";
let names: string[] = [];
try {
const files = await fs.readdir(CATALOGUE_DIR);
const seen = new Set<string>();
for (const file of files) {
if (/^icon_\d+\./i.test(file)) continue;
const base = file.replace(/\.(png|gif|jpg|jpeg|webp)$/i, "");
if (!base || seen.has(base)) continue;
seen.add(base);
names.push(base);
}
names.sort((a, b) => a.localeCompare(b));
} catch {
names = [];
}
if (type === "header") {
names = names.filter((n) => /header/i.test(n));
} else if (type === "teaser") {
names = names.filter((n) => /teaser/i.test(n));
}
if (search) {
names = names.filter((n) => n.toLowerCase().includes(search));
}
const total = names.length;
const images = names.slice(offset, offset + limit);
return apiOk({ images, total });
},
);