fix(admin): complete media manager

This commit is contained in:
Schubert Ferenc 2026-07-03 23:25:41 +02:00
parent 203c94b128
commit ec30f69c72
5 changed files with 417 additions and 96 deletions

View file

@ -15,10 +15,12 @@ const allowedMimeTypes = new Map([
export type MediaFile = {
name: string;
url: string;
type: string;
type: "image" | "pdf";
extension: string;
size: number;
uploadedAt: string;
isImage: boolean;
isPdf: boolean;
};
export async function ensureUploadDirectory() {
@ -72,21 +74,31 @@ export function resolveUploadTarget(fileName: string) {
return target;
}
function isAllowedStoredFile(fileName: string) {
const extension = path.extname(fileName).toLowerCase();
const allowedExtensions = [...allowedMimeTypes.values()].flat();
return !fileName.startsWith(".") && allowedExtensions.includes(extension);
}
export async function listMediaFiles(): Promise<MediaFile[]> {
await mkdir(uploadDirectory, { recursive: true });
const entries = await readdir(uploadDirectory);
const visibleEntries = entries.filter((entry) => !entry.startsWith("."));
const visibleEntries = entries.filter(isAllowedStoredFile);
const files = await Promise.all(visibleEntries.map(async (entry) => {
const fileStat = await stat(path.join(uploadDirectory, entry));
const extension = path.extname(entry).toLowerCase();
const isImage = [".jpg", ".jpeg", ".png", ".webp"].includes(extension);
const isPdf = extension === ".pdf";
return {
name: entry,
url: `/uploads/images/${encodeURIComponent(entry)}`,
type: extension.replace(".", "").toUpperCase() || "Datei",
type: isPdf ? "pdf" as const : "image" as const,
extension: extension.replace(".", "").toUpperCase() || "Datei",
size: fileStat.size,
uploadedAt: fileStat.birthtime.toISOString(),
isImage: [".jpg", ".jpeg", ".png", ".webp"].includes(extension),
isImage,
isPdf,
};
}));
@ -104,3 +116,12 @@ export async function saveMediaFile(file: File) {
return { fileName: safeName };
}
export async function deleteMediaFile(fileName: string) {
if (!isAllowedStoredFile(fileName) || path.basename(fileName) !== fileName) {
return { error: "Ungültiger Dateiname." };
}
await rm(resolveUploadTarget(fileName), { force: true });
return {};
}