[+] Delete, edit thumbnail

This commit is contained in:
2025-10-27 14:01:57 +08:00
parent 988239caf9
commit 5cb87bb071
+48
View File
@@ -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<string, string | undefined>, 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 () => {