push to maple-street
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { rm } from "node:fs/promises";
|
||||
import { gateOrUser } from "@/auth/api";
|
||||
import { db } from "@/db";
|
||||
import { writeAudit } from "@/db/audit";
|
||||
import { deleteEvent, getEventAccess } from "@/db/events";
|
||||
import { listEventVideos } from "@/db/videos";
|
||||
import { getStorageSettings } from "@/db/storage-settings";
|
||||
import { eventPhotoDir } from "@/lib/event-photos";
|
||||
import {
|
||||
eventVideoArchiveDir,
|
||||
eventVideoUploadDir,
|
||||
} from "@/lib/event-videos";
|
||||
import { errorResponse, jsonResponse } from "@/lib/http";
|
||||
|
||||
/**
|
||||
* DELETE /api/events/:id — manager-only. Removes the event, its folders/photos
|
||||
* and the event's directory on the uploads volume. The database cascade commits
|
||||
* first; bytes are then removed best-effort so a storage hiccup cannot leave a
|
||||
* half-deleted event row. Any orphaned bytes are reported in the server log.
|
||||
*/
|
||||
export const DELETE = async (
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) => {
|
||||
const gate = await gateOrUser("schedule.publish");
|
||||
if (gate instanceof Response) return gate;
|
||||
|
||||
const { id } = await params;
|
||||
const access = await getEventAccess(id, gate.id, gate.systemRole);
|
||||
if (!access) return errorResponse(404, "unknown event");
|
||||
const videos = await listEventVideos(id);
|
||||
|
||||
const deleted = await deleteEvent(id);
|
||||
if (!deleted) return errorResponse(404, "unknown event");
|
||||
|
||||
try {
|
||||
await rm(eventPhotoDir(id), { recursive: true, force: true });
|
||||
await rm(eventVideoUploadDir(id), { recursive: true, force: true });
|
||||
} catch (error) {
|
||||
console.error(`event ${id}: database deleted but files remain`, error);
|
||||
}
|
||||
|
||||
// Video originals are the largest files; remove each one from the archive
|
||||
// after the cascade has removed its row, so a deletion cannot orphan GBs.
|
||||
try {
|
||||
const storage = await getStorageSettings();
|
||||
for (const video of videos) {
|
||||
await rm(
|
||||
eventVideoArchiveDir(
|
||||
storage.archivePath,
|
||||
video.archiveFolder,
|
||||
id,
|
||||
video.id,
|
||||
),
|
||||
{ recursive: true, force: true },
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`event ${id}: NFS video originals were not removed`, error);
|
||||
}
|
||||
|
||||
await writeAudit(db, {
|
||||
actorId: gate.id,
|
||||
action: "event.deleted",
|
||||
entityType: "event",
|
||||
entityId: id,
|
||||
before: {
|
||||
title: deleted.event.title,
|
||||
startsAt: deleted.event.startsAt.toISOString(),
|
||||
photoCount: deleted.photoCount,
|
||||
videoCount: videos.length,
|
||||
},
|
||||
});
|
||||
|
||||
return jsonResponse({ ok: true });
|
||||
};
|
||||
Reference in New Issue
Block a user