Files
glenloch-media/src/app/api/bridge/sessions/[sessionId]/route.ts
T
bhursey ebe0c77f7e
CI / build (push) Has been cancelled
feat(editor): finish the quick/advanced editor handoff
Reopen advanced edits from their saved recipe, seed or take over the quick
editor from them, refresh thumbnails after a save, and add an edited-only
download. Also fixes the Lumenroom library collapsing to slivers on large
shoots and documents deploying both containers on a new host.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-21 06:35:24 +00:00

101 lines
3.3 KiB
TypeScript

import { NextRequest } from "next/server";
import {
authenticateBridge,
getBridgeEventSummary,
listBridgePhotos,
} from "@/db/bridge";
import { bridgeReturnUrl } from "@/lib/bridge";
import { errorResponse, jsonResponse } from "@/lib/http";
import { mediaStorage } from "@/lib/media-storage";
import { eventPhotoPath } from "@/lib/event-photos";
import sharp from "sharp";
/** The advanced editor's saved slider recipe, or null when it cannot be read. */
function savedRecipe(json: string | null): unknown {
if (!json) return null;
try {
const parsed = JSON.parse(json) as unknown;
return parsed && typeof parsed === "object" ? parsed : null;
} catch {
return null;
}
}
/** Reads the header dimensions without decoding the whole file. */
async function imageSize(
path: string,
): Promise<{ width: number; height: number } | null> {
try {
const meta = await sharp(path).metadata();
if (!meta.width || !meta.height) return null;
return { width: meta.width, height: meta.height };
} catch {
return null;
}
}
/**
* GET /api/bridge/sessions/:sessionId
*
* Everything the advanced editor needs to mirror one event: its title, dates
* and photo list. Photo bytes are fetched separately, one at a time, so opening
* a large event does not transfer gigabytes up front.
*/
export const GET = async (
req: NextRequest,
{ params }: { params: Promise<{ sessionId: string }> },
) => {
const { sessionId } = await params;
const auth = await authenticateBridge(req, sessionId);
if (!auth) return errorResponse(401, "invalid or expired bridge session");
const event = await getBridgeEventSummary(auth.session.eventId);
if (!event) return errorResponse(404, "unknown event");
const rows = await listBridgePhotos(auth.session.eventId);
const storage = await mediaStorage();
if (!storage.ok) return errorResponse(503, storage.message);
// Dimensions are only known for browser images; RAW dimensions arrive with
// the proxy the editor builds from the original.
const sizes = new Map<string, { width: number; height: number }>();
for (const row of rows) {
if (row.kind !== "image") continue;
const size = await imageSize(
eventPhotoPath(storage.root, row.storedName),
).catch(() => null);
if (size) sizes.set(row.id, size);
}
return jsonResponse({
session: { id: auth.session.id, expiresAt: auth.session.expiresAt },
event: {
id: event.id,
title: event.title,
eventType: event.eventType,
startsAt: event.startsAt,
endsAt: event.endsAt,
location: event.location,
description: event.description,
},
photos: rows.map((row) => ({
id: row.id,
filename: row.filename,
mimeType: row.mimeType,
kind: row.kind,
sizeBytes: row.sizeBytes,
folderName: row.folderName,
width: sizes.get(row.id)?.width ?? 0,
height: sizes.get(row.id)?.height ?? 0,
takenAt: row.createdAt,
// Reopening the editor restores the sliders exactly as they were saved;
// the editor falls back to neutral when this is null.
edits: savedRecipe(row.advancedEditJson),
// Stamps the thumbnail URL so a newly saved edit is not masked by a
// cached grid thumbnail from before it was rendered.
editedAt: row.editedAt,
})),
editor: { returnUrl: bridgeReturnUrl(auth.session.eventId) },
});
};