This commit is contained in:
Ivan Rezchikov
2026-07-11 21:21:38 +03:00
parent 18161f4604
commit 4c42b5aa1d
20 changed files with 369 additions and 260 deletions
+27
View File
@@ -9,6 +9,8 @@ import { validatePage } from "./validation";
import { requireAdmin } from "../user/require";
import { runAction } from "../run-action";
import type { ActionResult } from "../action-result";
import { uploadImage, removeImage, getImageUrl } from "../image";
import { getBlocksByPageId } from "./repository";
export async function savePageAction(params: {
id: string | null,
@@ -52,7 +54,32 @@ export async function archivePageAction(id: string, slug: string, type: PageType
export async function deletePageAction(id: string, type: PageType): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
const blocks = await getBlocksByPageId(id);
const imageKeys = blocks
.filter((b) => b.type === "IMAGE")
.map((b) => (b.content as { type: "IMAGE"; url: string }).url)
.filter(Boolean);
if (imageKeys.length) {
await Promise.allSettled(imageKeys.map(removeImage));
}
await deletePage(id);
revalidatePath(`/${type.toLowerCase()}`);
});
}
export async function uploadPageImage(file: File): Promise<string> {
await requireAdmin();
return uploadImage(file, "pages/images", { maxHeight: 800 });
}
export async function removePageImages(keys: string[]): Promise<void> {
await requireAdmin();
await Promise.allSettled(keys.map(removeImage));
}
export async function getImagePreviewUrl(key: string): Promise<string> {
return getImageUrl(key);
}
+18
View File
@@ -2,6 +2,7 @@ import { getCurrentUser } from "../user";
import type { Block, Page, PageMeta, PagePublic, PageStatus, PageType } from "./types";
import { prisma } from "../prisma";
import { Prisma } from "@/lib/generated/prisma/client";
import { removeImage } from "../image";
const publicPageSelect = {
id: true,
@@ -86,6 +87,19 @@ export async function updatePage(
const currentPage = await prisma.page.findUnique({ where: { id }, select: { status: true } });
const isNewlyPublished = status === "PUBLISHED" && currentPage?.status !== "PUBLISHED";
const oldImageBlocks = await prisma.block.findMany({
where: { pageId: id, type: "IMAGE" },
select: { content: true },
});
const oldImageKeys = oldImageBlocks
.map((b) => (b.content as { type: "IMAGE"; url: string }).url)
.filter(Boolean);
const newImageKeys = blocks
.filter((b) => b.type === "IMAGE")
.map((b) => (b.content as { type: "IMAGE"; url: string }).url)
.filter(Boolean);
const keysToRemove = oldImageKeys.filter((k) => !newImageKeys.includes(k));
const page = await prisma.page.update({
where: { id },
data: {
@@ -114,6 +128,10 @@ export async function updatePage(
: []),
]);
if (keysToRemove.length > 0) {
await Promise.allSettled(keysToRemove.map(removeImage));
}
return page;
}