feat(events): show every photo on the event page without folders
CI / build (push) Has been cancelled
CI / build (push) Has been cancelled
Folders were an implementation detail leaking into the event workflow: every upload needed a folder first and photos were split across pages. Uploads now land in a single implicit "Photos" folder and the event page renders one gallery with the uploader and an edited-only download. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -1,58 +1,14 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { createHash } from "node:crypto";
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { mkdir, unlink } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { Readable, Transform } from "node:stream";
|
||||
import { pipeline } from "node:stream/promises";
|
||||
import { gateSession } from "@/auth/api";
|
||||
import {
|
||||
addEventPhoto,
|
||||
findEventPhotoByChecksum,
|
||||
getEventFolderAccess,
|
||||
isUniqueViolation,
|
||||
} from "@/db/events";
|
||||
import {
|
||||
MAX_EVENT_PHOTO_BYTES,
|
||||
eventPhotoKindFor,
|
||||
eventPhotoMimeFor,
|
||||
eventPhotoPath,
|
||||
sanitizePhotoFilename,
|
||||
storedNameForEventPhoto,
|
||||
} from "@/lib/event-photos";
|
||||
import { errorResponse, jsonResponse } from "@/lib/http";
|
||||
import { mediaStorage } from "@/lib/media-storage";
|
||||
import { uuidv7 } from "@/lib/uuid";
|
||||
|
||||
class PhotoTooLarge extends Error {}
|
||||
|
||||
function publicPhoto(photo: {
|
||||
id: string;
|
||||
folderId: string;
|
||||
filename: string;
|
||||
mimeType: string;
|
||||
sizeBytes: number;
|
||||
kind: "raw" | "image";
|
||||
createdAt: Date;
|
||||
}) {
|
||||
return {
|
||||
id: photo.id,
|
||||
folderId: photo.folderId,
|
||||
filename: photo.filename,
|
||||
mimeType: photo.mimeType,
|
||||
sizeBytes: photo.sizeBytes,
|
||||
kind: photo.kind,
|
||||
createdAt: photo.createdAt,
|
||||
};
|
||||
}
|
||||
import { getEventFolderAccess } from "@/db/events";
|
||||
import { errorResponse } from "@/lib/http";
|
||||
import { uploadEventPhoto } from "@/lib/event-photo-upload";
|
||||
|
||||
/**
|
||||
* POST /api/events/:id/folders/:folderId/photos?name=IMG_0001.CR3
|
||||
*
|
||||
* A single photo is the raw request body (not multipart), so a 100 MB RAW file
|
||||
* streams straight to disk instead of being buffered in memory. The filename is
|
||||
* passed as a query parameter because headers cannot carry arbitrary Unicode.
|
||||
* The SHA-256 is computed while streaming and dedupes a re-upload per folder.
|
||||
* Folder-scoped upload kept for direct API use. The event page posts to
|
||||
* `/api/events/:id/photos`, which picks the event's one implicit folder.
|
||||
*/
|
||||
export const POST = async (
|
||||
req: NextRequest,
|
||||
@@ -70,93 +26,9 @@ export const POST = async (
|
||||
return errorResponse(403, "you are not assigned to this event");
|
||||
}
|
||||
|
||||
// Photos are only ever written to the configured media storage share, so refuse
|
||||
// before reading a single byte when that destination is not ready.
|
||||
const storage = await mediaStorage();
|
||||
if (!storage.ok) return errorResponse(503, storage.message);
|
||||
|
||||
const suppliedName = req.nextUrl.searchParams.get("name");
|
||||
if (!suppliedName) return errorResponse(400, "a filename is required");
|
||||
const filename = sanitizePhotoFilename(suppliedName);
|
||||
const kind = eventPhotoKindFor(filename);
|
||||
if (!kind) {
|
||||
return errorResponse(
|
||||
415,
|
||||
"only photo files are accepted (JPEG, PNG, WebP, HEIC, TIFF, PSD or camera RAW)",
|
||||
);
|
||||
}
|
||||
|
||||
const declaredLength = Number(req.headers.get("content-length") ?? 0);
|
||||
if (declaredLength > MAX_EVENT_PHOTO_BYTES) {
|
||||
return errorResponse(413, "that photo is larger than 200 MB");
|
||||
}
|
||||
if (!req.body) return errorResponse(400, "a file body is required");
|
||||
|
||||
const photoId = uuidv7();
|
||||
const storedName = storedNameForEventPhoto(id, photoId, filename);
|
||||
const destination = eventPhotoPath(storage.root, storedName);
|
||||
await mkdir(path.dirname(destination), { recursive: true });
|
||||
|
||||
const hash = createHash("sha256");
|
||||
let size = 0;
|
||||
const meter = new Transform({
|
||||
transform(chunk: Buffer, _encoding, callback) {
|
||||
size += chunk.length;
|
||||
if (size > MAX_EVENT_PHOTO_BYTES) {
|
||||
callback(new PhotoTooLarge());
|
||||
return;
|
||||
}
|
||||
hash.update(chunk);
|
||||
callback(null, chunk);
|
||||
},
|
||||
return uploadEventPhoto(req, {
|
||||
eventId: id,
|
||||
folderId,
|
||||
uploaderId: gate.id,
|
||||
});
|
||||
|
||||
try {
|
||||
await pipeline(
|
||||
Readable.from(req.body as unknown as AsyncIterable<Uint8Array>),
|
||||
meter,
|
||||
createWriteStream(destination, { flags: "wx" }),
|
||||
);
|
||||
} catch (error) {
|
||||
await unlink(destination).catch(() => undefined);
|
||||
if (error instanceof PhotoTooLarge) {
|
||||
return errorResponse(413, "that photo is larger than 200 MB");
|
||||
}
|
||||
return errorResponse(500, "the upload did not complete");
|
||||
}
|
||||
|
||||
if (size === 0) {
|
||||
await unlink(destination).catch(() => undefined);
|
||||
return errorResponse(400, "the uploaded file was empty");
|
||||
}
|
||||
|
||||
const checksumSha256 = hash.digest("hex");
|
||||
const existing = await findEventPhotoByChecksum(folderId, checksumSha256);
|
||||
if (existing) {
|
||||
await unlink(destination).catch(() => undefined);
|
||||
return jsonResponse({ photo: publicPhoto(existing), duplicate: true });
|
||||
}
|
||||
|
||||
try {
|
||||
const photo = await addEventPhoto({
|
||||
folderId,
|
||||
uploaderId: gate.id,
|
||||
filename,
|
||||
mimeType: eventPhotoMimeFor(filename),
|
||||
sizeBytes: size,
|
||||
storedName,
|
||||
checksumSha256,
|
||||
kind,
|
||||
});
|
||||
return jsonResponse({ photo: publicPhoto(photo) }, 201);
|
||||
} catch (error) {
|
||||
await unlink(destination).catch(() => undefined);
|
||||
if (isUniqueViolation(error)) {
|
||||
const raced = await findEventPhotoByChecksum(folderId, checksumSha256);
|
||||
if (raced) {
|
||||
return jsonResponse({ photo: publicPhoto(raced), duplicate: true });
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { gateSession } from "@/auth/api";
|
||||
import { ensureEventFolder, getEventAccess } from "@/db/events";
|
||||
import { errorResponse } from "@/lib/http";
|
||||
import { uploadEventPhoto } from "@/lib/event-photo-upload";
|
||||
import { mediaStorage } from "@/lib/media-storage";
|
||||
|
||||
/**
|
||||
* POST /api/events/:id/photos?name=IMG_0001.CR3
|
||||
*
|
||||
* The event page has no folder picker, so uploads land in the event's single
|
||||
* folder (created as "Photos" on the first upload). Everything else is the same
|
||||
* streamed, checksum-deduped upload the folder route uses.
|
||||
*/
|
||||
export const POST = async (
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) => {
|
||||
const gate = await gateSession();
|
||||
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");
|
||||
if (!access.canContribute) {
|
||||
return errorResponse(403, "you are not assigned to this event");
|
||||
}
|
||||
|
||||
// Check storage before creating anything, so a blocked upload leaves no rows.
|
||||
const storage = await mediaStorage();
|
||||
if (!storage.ok) return errorResponse(503, storage.message);
|
||||
|
||||
const folder = await ensureEventFolder(id, gate.id);
|
||||
return uploadEventPhoto(req, {
|
||||
eventId: id,
|
||||
folderId: folder.id,
|
||||
uploaderId: gate.id,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user