From 5cb87bb071d058cda1ea14db50293a7110b7589e Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 27 Oct 2025 14:01:57 +0800 Subject: [PATCH] [+] Delete, edit thumbnail --- src/index.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/index.ts b/src/index.ts index f465d3e..8dda6d6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,9 @@ import ExifReader from "exifreader" import { exists, mkdir } from "fs/promises" import { staticPlugin } from "@elysiajs/static" import sharp from "sharp" +import { unlink } from "fs/promises" +import path from "path" +import { readdir } from "fs/promises" // --- Configuration --- const FOLDER_BASE = "./data" @@ -94,6 +97,29 @@ function checkHeaderKey(headers: Record, expectList: if (!expectList.includes(key ?? "")) done(401, "Invalid authentication key") } + +// Retroactively generate missing edit thumbnails on startup +for (const dirent of await readdir(FOLDER_PHOTOS)) { + const photoDir = path.join(FOLDER_PHOTOS, dirent) + const thumbEditPath = path.join(photoDir, "thumb.edit.webp") + if (!(await exists(thumbEditPath))) { + const editedPhotoPath = path.join(photoDir, "edit.jpg") + if (await exists(editedPhotoPath)) { + console.log(`[+] Generating missing edited thumbnail for ${dirent}`) + await generateThumbnail(editedPhotoPath, thumbEditPath) + } + // Add metadata + const metadata = await getMetadata() + const photoIndex = metadata.findIndex((p) => p.id === dirent) + if (photoIndex !== -1) { + const photo = metadata[photoIndex] + photo.thumbnail_edited = Bun.file(thumbEditPath).name! + metadata[photoIndex] = photo + await saveMetadata(metadata) + } + } +} + export const app = new Elysia() // Error handling: Return status code and message .onError(({ error, status }) => { @@ -137,6 +163,8 @@ export const app = new Elysia() // Generate thumbnail const thumbnailPath = `${FOLDER_PHOTOS}/${id}/thumb.webp` await generateThumbnail(originalPath, thumbnailPath) + const editedThumb = `${FOLDER_PHOTOS}/${id}/thumb.edit.webp` + await generateThumbnail(editedPath, editedThumb) // 4. Create new metadata entry const newEntry: PhotoMetadata = { @@ -170,6 +198,26 @@ export const app = new Elysia() }), }) + .post("/delete", async ({ body }) => { + const { id } = body + const metadata = await getMetadata() + + const photoIndex = metadata.findIndex((p) => p.id === id) + if (photoIndex === -1) done(404, "Photo not found") + + const meta = metadata[photoIndex] + metadata.splice(photoIndex, 1) + await saveMetadata(metadata) + + // Delete from filesystem + await unlink(path.join(FOLDER_PHOTOS, id, path.basename(meta.original_photo))) + + console.log(`[+] Deleted photo ${id}`) + done(200, { success: true }) + }, { + body: t.Object({ id: t.String() }), + }) + // ----- 2. GET /photos ----- // Returns all metadata entries *without* the 'owner_key'. .get("/photos", async () => {