This commit is contained in:
@@ -1,102 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import { MediaKind } from "@prisma/client";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
import { isRedirectError } from "next/dist/client/components/redirect-error";
|
||||
|
||||
import { toInternalAdminPath } from "@/lib/admin-routing";
|
||||
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
|
||||
import { countMediaUsageReferences, getMediaAssetById } from "@/lib/media";
|
||||
import { createStandaloneMediaAsset, deleteMediaAssetAndFile } from "@/lib/media-service";
|
||||
import { isManagedMediaFilePath } from "@/lib/media-storage";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
async function ensureAdmin() {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
await clearAdminSessionCookie();
|
||||
redirect("/");
|
||||
}
|
||||
}
|
||||
|
||||
function withMessage(pathname: string, type: "success" | "error", message: string) {
|
||||
const params = new URLSearchParams();
|
||||
params.set(type, message);
|
||||
|
||||
return `${pathname}?${params.toString()}`;
|
||||
}
|
||||
|
||||
function revalidateMediaPages() {
|
||||
revalidatePath(toInternalAdminPath("/"));
|
||||
revalidatePath(toInternalAdminPath("/media"));
|
||||
revalidatePath(toInternalAdminPath("/portfolio"));
|
||||
revalidatePath(toInternalAdminPath("/portfolio/projects"));
|
||||
}
|
||||
|
||||
export async function createMediaAssetAction(formData: FormData) {
|
||||
await ensureAdmin();
|
||||
|
||||
try {
|
||||
const kindValue = String(formData.get("kind") ?? "IMAGE");
|
||||
const kind = kindValue === "DOCUMENT" ? MediaKind.DOCUMENT : MediaKind.IMAGE;
|
||||
|
||||
await createStandaloneMediaAsset({
|
||||
kind,
|
||||
label: String(formData.get("label") ?? ""),
|
||||
uploadFile: formData.get("file"),
|
||||
});
|
||||
|
||||
revalidateMediaPages();
|
||||
redirect(withMessage("/media", "success", "Datei gespeichert."));
|
||||
} catch (error) {
|
||||
if (isRedirectError(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
const message = error instanceof Error ? error.message : "Datei konnte nicht gespeichert werden.";
|
||||
redirect(withMessage("/media", "error", message));
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteMediaAssetAction(formData: FormData) {
|
||||
await ensureAdmin();
|
||||
|
||||
const assetId = String(formData.get("assetId") ?? "");
|
||||
|
||||
try {
|
||||
const asset = await getMediaAssetById(assetId);
|
||||
|
||||
if (!asset) {
|
||||
redirect(withMessage("/media", "error", "Datei nicht gefunden."));
|
||||
}
|
||||
|
||||
const usageCount = await countMediaUsageReferences(asset.id);
|
||||
|
||||
if (usageCount > 0) {
|
||||
redirect(withMessage("/media", "error", "Datei wird noch verwendet."));
|
||||
}
|
||||
|
||||
await prisma.mediaAsset.delete({
|
||||
where: {
|
||||
id: asset.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (isManagedMediaFilePath(asset.url)) {
|
||||
await deleteMediaAssetAndFile({
|
||||
assetId: asset.id,
|
||||
assetUrl: asset.url,
|
||||
});
|
||||
}
|
||||
|
||||
revalidateMediaPages();
|
||||
redirect(withMessage("/media", "success", "Datei geloescht."));
|
||||
} catch (error) {
|
||||
if (isRedirectError(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
const message = error instanceof Error ? error.message : "Datei konnte nicht geloescht werden.";
|
||||
redirect(withMessage("/media", "error", message));
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { MediaLibraryManager } from "@/components/root/media-library-manager";
|
||||
import { RootDashboardShell } from "@/components/root/root-dashboard-shell";
|
||||
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
|
||||
import { getAdminMediaAssets } from "@/lib/media";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const copy = {
|
||||
title: "Media Library",
|
||||
subtitle: "Zentrale Dateien fuer Portfolio und spaetere Inhaltsbereiche.",
|
||||
overview: "Uebersicht",
|
||||
maintenance: "Wartungsmodus",
|
||||
uiKit: "UI Kit",
|
||||
media: "Media",
|
||||
siteSettings: "Settings",
|
||||
portfolio: "Portfolio",
|
||||
logout: "Ausloggen",
|
||||
backToSite: "Zur Website",
|
||||
};
|
||||
|
||||
export default async function RootMediaPage() {
|
||||
if (!(await isAdminAuthenticated())) {
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
async function logoutAction() {
|
||||
"use server";
|
||||
|
||||
await clearAdminSessionCookie();
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
const mediaAssets = await getAdminMediaAssets();
|
||||
|
||||
return (
|
||||
<RootDashboardShell
|
||||
copy={copy}
|
||||
active="media"
|
||||
logoutAction={logoutAction}
|
||||
headerTitle={copy.title}
|
||||
headerDescription={copy.subtitle}
|
||||
>
|
||||
<div className="space-y-6">
|
||||
<MediaLibraryManager mediaAssets={mediaAssets} />
|
||||
</div>
|
||||
</RootDashboardShell>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user