CI / quality (push) Waiting to run
- Add lib/db (schema, postgres.js client, enums, seed, migrations) on Drizzle - Rewrite all lib and admin action queries from Prisma to Drizzle - Keep existing table/column names so no data migration is needed - Preserve signed-cookie admin auth unchanged - Map unique-violation handling from Prisma P2002 to SQLSTATE 23505 - Swap deps, scripts, Makefile, and Dockerfile from Prisma to Drizzle
96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
"use server";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { isRedirectError } from "next/dist/client/components/redirect-error";
|
|
|
|
import { getAdminAppPath, toInternalAdminPath } from "@/lib/admin-routing";
|
|
import { clearAdminSessionCookie, isAdminAuthenticated } from "@/lib/admin-auth";
|
|
import { withFlash } from "@/lib/admin-feedback";
|
|
import { countMediaUsageReferences, getMediaAssetById } from "@/lib/media";
|
|
import { createStandaloneMediaAsset, deleteMediaAssetAndFile } from "@/lib/media-service";
|
|
import { isManagedMediaFilePath } from "@/lib/media-storage";
|
|
import { db } from "@/lib/db";
|
|
import { mediaAsset } from "@/lib/db/schema";
|
|
import { MediaKind } from "@/lib/db/enums";
|
|
|
|
async function ensureAdmin() {
|
|
if (!(await isAdminAuthenticated())) {
|
|
await clearAdminSessionCookie();
|
|
redirect(getAdminAppPath("/"));
|
|
}
|
|
}
|
|
|
|
|
|
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(withFlash(getAdminAppPath("/media"), { success: "Datei gespeichert." }));
|
|
} catch (error) {
|
|
if (isRedirectError(error)) {
|
|
throw error;
|
|
}
|
|
|
|
const message = error instanceof Error ? error.message : "Datei konnte nicht gespeichert werden.";
|
|
redirect(withFlash(getAdminAppPath("/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(withFlash(getAdminAppPath("/media"), { error: "Datei nicht gefunden." }));
|
|
}
|
|
|
|
const usageCount = await countMediaUsageReferences(asset.id);
|
|
|
|
if (usageCount > 0) {
|
|
redirect(withFlash(getAdminAppPath("/media"), { error: "Datei wird noch verwendet." }));
|
|
}
|
|
|
|
await db.delete(mediaAsset).where(eq(mediaAsset.id, asset.id));
|
|
|
|
if (isManagedMediaFilePath(asset.url)) {
|
|
await deleteMediaAssetAndFile({
|
|
assetId: asset.id,
|
|
assetUrl: asset.url,
|
|
});
|
|
}
|
|
|
|
revalidateMediaPages();
|
|
redirect(withFlash(getAdminAppPath("/media"), { success: "Datei geloescht." }));
|
|
} catch (error) {
|
|
if (isRedirectError(error)) {
|
|
throw error;
|
|
}
|
|
|
|
const message = error instanceof Error ? error.message : "Datei konnte nicht geloescht werden.";
|
|
redirect(withFlash(getAdminAppPath("/media"), { error: message }));
|
|
}
|
|
}
|