prod
This commit is contained in:
@@ -74,11 +74,11 @@ export const submitCitizenshipApplication = formAction(citizenshipApplicationSch
|
||||
let signatureKey: string | undefined;
|
||||
|
||||
try {
|
||||
const profileKey = await uploadImage(profile, "citizenship/profiles", {
|
||||
profileKey = await uploadImage(profile, "citizenship/profiles", {
|
||||
maxWidth: 800, maxHeight: 1000, fit: "cover",
|
||||
});
|
||||
|
||||
const signatureKey = await uploadImage(signature, "citizenship/signatures", {
|
||||
signatureKey = await uploadImage(signature, "citizenship/signatures", {
|
||||
maxWidth: 1000, maxHeight: 400, fit: "inside",
|
||||
});
|
||||
|
||||
|
||||
+10
-8
@@ -8,13 +8,13 @@ const BUCKET = "uploads";
|
||||
const MAX_FILE_SIZE = 5 * 1024 * 1024;
|
||||
const ALLOWED_INPUT_FORMATS = new Set(["jpeg", "png", "webp"]);
|
||||
|
||||
export async function uploadImage(file: File, directory: string, options: { maxWidth: number; maxHeight: number; fit?: "inside" | "cover"; removeBg?: boolean }): Promise<string> {
|
||||
export async function uploadImage(file: File, directory: string, options: { maxWidth?: number; maxHeight?: number; fit?: "inside" | "cover" }): Promise<string> {
|
||||
if (file.size === 0) throw new AppError("A file is required");
|
||||
if (file.size > MAX_FILE_SIZE) throw new AppError("Image must be smaller than 5MB");
|
||||
|
||||
const input = Buffer.from(await file.arrayBuffer());
|
||||
|
||||
let image = sharp(input, {
|
||||
const image = sharp(input, {
|
||||
failOn: "error",
|
||||
limitInputPixels: 30_000_000,
|
||||
});
|
||||
@@ -28,14 +28,16 @@ export async function uploadImage(file: File, directory: string, options: { maxW
|
||||
throw new AppError("Image is too small");
|
||||
}
|
||||
|
||||
const resizeOptions: Parameters<typeof image.resize>[0] = {
|
||||
fit: options.fit ?? "inside",
|
||||
withoutEnlargement: true,
|
||||
};
|
||||
if (options.maxWidth) resizeOptions.width = options.maxWidth;
|
||||
if (options.maxHeight) resizeOptions.height = options.maxHeight;
|
||||
|
||||
const output = await image
|
||||
.rotate()
|
||||
.resize({
|
||||
width: options.maxWidth,
|
||||
height: options.maxHeight,
|
||||
fit: options.fit ?? "inside",
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.resize(resizeOptions)
|
||||
.webp({ quality: 80, effort: 4 })
|
||||
.toBuffer();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user