From df330ffd708925910841897092044b28edf25802 Mon Sep 17 00:00:00 2001 From: philipcheung Date: Sat, 12 Sep 2026 10:08:01 +0800 Subject: [PATCH] Add cover image upload to blog posts with cleanup --- src/pages/admin/post/[id].astro | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/pages/admin/post/[id].astro b/src/pages/admin/post/[id].astro index 7fa9dcb..2ffbbf9 100644 --- a/src/pages/admin/post/[id].astro +++ b/src/pages/admin/post/[id].astro @@ -8,12 +8,15 @@ import { getEnv } from "../../../lib/env"; import { uniqueSlug } from "../../../data/content"; import { parseForm } from "../../../lib/form"; import { postInput } from "../../../schemas"; +import ImageField from "../../../components/admin/ImageField.astro"; +import { deleteMedia } from "../../../lib/media"; export const prerender = false; const { id } = Astro.params; const isNew = id === "new"; -const db = getDb(getEnv().DB); +const env = getEnv(); +const db = getDb(env.DB); let error = ""; let errors: Record = {}; @@ -31,6 +34,7 @@ if (Astro.request.method === "POST") { const action = String(form.get("action") ?? "save"); if (action === "delete" && !isNew) { + await deleteMedia(env, existing?.coverImage); await db.delete(posts).where(eq(posts.id, id!)); return Astro.redirect("/admin"); } @@ -69,6 +73,9 @@ if (Astro.request.method === "POST") { updatedAt: now, }); } else { + if (existing!.coverImage !== coverImage) { + await deleteMedia(env, existing!.coverImage); + } await db .update(posts) .set({ @@ -129,14 +136,13 @@ const post = isNew placeholder="留空會由內容自動產生" /> - {errors.coverImage && {errors.coverImage}} -