feat(editor): finish the quick/advanced editor handoff
CI / build (push) Has been cancelled

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>
This commit is contained in:
2026-11-21 06:35:24 +00:00
parent ab4a07a40f
commit ebe0c77f7e
35 changed files with 1844 additions and 150 deletions
@@ -2,6 +2,7 @@ import { NextRequest } from "next/server";
import { authenticateBridge } from "@/db/bridge";
import {
BRIDGE_SESSION_TTL_SECONDS,
bridgeReturnUrl,
signBridgeToken,
} from "@/lib/bridge";
import { errorResponse, jsonResponse } from "@/lib/http";
@@ -36,5 +37,9 @@ export const POST = async (
sessionId: auth.session.id,
eventId: auth.session.eventId,
expiresAt: auth.session.expiresAt,
// Carried from the browser-facing request that opened the editor so the
// editor can link back to the address the user actually reached.
returnUrl:
auth.payload.ret ?? bridgeReturnUrl(auth.session.eventId) ?? null,
});
};
@@ -4,11 +4,17 @@ import { mkdir, rename, unlink, writeFile } from "node:fs/promises";
import path from "node:path";
import { authenticateBridge, getBridgePhoto, saveBridgeRender } from "@/db/bridge";
import {
advancedStoredNameForEventPhoto,
editedStoredNameForEventPhoto,
eventPhotoPath,
} from "@/lib/event-photos";
import { errorResponse, jsonResponse } from "@/lib/http";
import { mediaStorage } from "@/lib/media-storage";
import {
defaultPhotoAdjustments,
parseStoredPhotoAdjustments,
} from "@/lib/photo-adjustments";
import { renderPhotoJpeg } from "@/lib/photo-render";
const MAX_BODY_BYTES = 64 * 1024 * 1024;
const MAX_RECIPE_BYTES = 512 * 1024;
@@ -19,9 +25,10 @@ const MAX_JPEG_BYTES = 48 * 1024 * 1024;
* Body: { jpeg: <base64>, edits: <advanced recipe>, width, height }
*
* The advanced editor renders in the browser, so the server only has to store
* the result. The JPEG becomes the photo's edited version (used by the gallery,
* thumbnails and delivery exports) and the recipe is kept so the editor can be
* reopened with its sliders intact.
* the result. The JPEG is kept as the photo's advanced render (its own file, so
* the simple editor can layer on top) and the recipe is kept so the editor can
* be reopened with its sliders intact. Any simple-editor layer is rebuilt on
* top of the new render, so the two editors never disagree.
*/
export const POST = async (
req: NextRequest,
@@ -70,11 +77,11 @@ export const POST = async (
const storage = await mediaStorage();
if (!storage.ok) return errorResponse(503, storage.message);
const storedName = editedStoredNameForEventPhoto(
const advancedStoredName = advancedStoredNameForEventPhoto(
auth.session.eventId,
photo.id,
);
const target = eventPhotoPath(storage.root, storedName);
const target = eventPhotoPath(storage.root, advancedStoredName);
const temporary = `${target}.${randomUUID()}.tmp`;
try {
await mkdir(path.dirname(target), { recursive: true });
@@ -85,10 +92,40 @@ export const POST = async (
return errorResponse(500, "the edited image could not be saved");
}
// A photo the simple editor also adjusted keeps that layer: re-render it on
// the new advanced render so the saved JPEG still shows both recipes.
const quick = parseStoredPhotoAdjustments(photo.editJson);
const hasQuickLayer =
JSON.stringify(quick) !== JSON.stringify(defaultPhotoAdjustments());
let editedStoredName = advancedStoredName;
let editedSizeBytes = data.byteLength;
if (hasQuickLayer) {
editedStoredName = editedStoredNameForEventPhoto(auth.session.eventId, photo.id);
const layered = eventPhotoPath(storage.root, editedStoredName);
try {
const rendered = await renderPhotoJpeg({
filePath: target,
kind: "image",
adjustments: quick,
});
await mkdir(path.dirname(layered), { recursive: true });
const layeredTemporary = `${layered}.${randomUUID()}.tmp`;
await writeFile(layeredTemporary, rendered.data);
await rename(layeredTemporary, layered);
editedSizeBytes = rendered.data.byteLength;
} catch (error) {
console.error("simple edit layer rebuild failed", error);
return errorResponse(
500,
"the simple editor's adjustments could not be rebuilt on the new render",
);
}
}
const editedAt = await saveBridgeRender({
photoId: photo.id,
editedStoredName: storedName,
editedSizeBytes: data.byteLength,
editedStoredName,
editedSizeBytes,
advancedEditJson: recipe,
editedBy: auth.user.id,
});
@@ -100,6 +137,6 @@ export const POST = async (
return jsonResponse({
ok: true,
editedAt,
editedSizeBytes: data.byteLength,
editedSizeBytes,
});
};
@@ -4,9 +4,9 @@ import { createReadStream } from "node:fs";
import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
import path from "node:path";
import { Readable } from "node:stream";
import sharp from "sharp";
import { authenticateBridge, getBridgePhoto } from "@/db/bridge";
import {
bridgeEditedThumbStoredNameForEventPhoto,
bridgeProxyStoredNameForEventPhoto,
bridgeThumbStoredNameForEventPhoto,
eventPhotoPath,
@@ -14,7 +14,7 @@ import {
import { errorResponse } from "@/lib/http";
import { mediaStorage } from "@/lib/media-storage";
import { defaultPhotoAdjustments } from "@/lib/photo-adjustments";
import { renderPhotoJpeg } from "@/lib/photo-render";
import { renderPhotoJpeg, renderPhotoThumbnail } from "@/lib/photo-render";
const PROXY_MAX_EDGE = 4000;
const THUMB_MAX_EDGE = 480;
@@ -31,14 +31,25 @@ function imageResponse(data: Buffer, contentType = "image/jpeg"): Response {
});
}
/** Reads a cached derivative or writes it atomically on first use. */
/**
* Reads a cached derivative or writes it atomically on first use. When
* `newerThan` is given, a cache older than that timestamp is rebuilt, so a
* thumbnail of a re-rendered edit never goes stale.
*/
async function cached(
filePath: string,
produce: () => Promise<Buffer>,
newerThan?: number,
): Promise<Buffer> {
try {
const info = await stat(filePath);
if (info.isFile() && info.size > 0) return readFile(filePath);
if (
info.isFile() &&
info.size > 0 &&
(newerThan === undefined || info.mtimeMs >= newerThan)
) {
return readFile(filePath);
}
} catch {
// Not generated yet.
}
@@ -51,24 +62,12 @@ async function cached(
}
async function thumbFor(filePath: string, kind: "raw" | "image") {
if (kind === "raw") {
const rendered = await renderPhotoJpeg({
filePath,
kind,
adjustments: defaultPhotoAdjustments(),
maxDimension: THUMB_MAX_EDGE,
});
return rendered.data;
}
return sharp(filePath)
.resize({
width: THUMB_MAX_EDGE,
height: THUMB_MAX_EDGE,
fit: "inside",
withoutEnlargement: true,
})
.jpeg({ quality: 82, mozjpeg: true })
.toBuffer();
return renderPhotoThumbnail({
filePath,
kind,
maxDimension: THUMB_MAX_EDGE,
quality: 82,
});
}
/**
@@ -126,13 +125,33 @@ export const GET = async (
}
if (variant === "thumb") {
// With a saved edit, the grid should show the photo as it was developed —
// the editor's own look — not the untouched original.
let sourcePath = originalPath;
let sourceKind: "raw" | "image" = photo.kind;
let thumbName = bridgeThumbStoredNameForEventPhoto(
auth.session.eventId,
photo.id,
);
let editedMtime: number | undefined;
if (photo.editedStoredName) {
const editedPath = eventPhotoPath(storage.root, photo.editedStoredName);
const edited = await stat(editedPath).catch(() => null);
if (edited?.isFile()) {
sourcePath = editedPath;
sourceKind = "image";
editedMtime = edited.mtimeMs;
thumbName = bridgeEditedThumbStoredNameForEventPhoto(
auth.session.eventId,
photo.id,
);
}
}
try {
const thumbPath = eventPhotoPath(
storage.root,
bridgeThumbStoredNameForEventPhoto(auth.session.eventId, photo.id),
);
const data = await cached(thumbPath, () =>
thumbFor(originalPath, photo.kind),
const data = await cached(
eventPhotoPath(storage.root, thumbName),
() => thumbFor(sourcePath, sourceKind),
editedMtime,
);
return imageResponse(data);
} catch {
@@ -10,6 +10,17 @@ 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,
@@ -77,6 +88,12 @@ export const GET = async (
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) },
});