feat(editor): bridge the advanced photo editor into the event workflow
CI / build (push) Has been cancelled
CI / build (push) Has been cancelled
Event photos can now be opened in Lumenroom, a WebGL2 develop module, from the photo editor's Advanced button. The dashboard signs a short-lived token scoped to one user and event; Lumenroom exchanges it, imports the event's photo list and proxies bytes on demand, so nothing is copied and the temporary catalog expires with the bridge session. Saving in Lumenroom renders the photo and writes it back as the event's edited JPEG, with the advanced recipe kept in event_photos.advanced_edit_json. Lumenroom is vendored under lumenroom/ and built as a second compose service, so one clone and `docker compose up -d --build` deploys both apps. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
import type { Edits, PhotoRecord } from '../types';
|
||||
import { getBitmap } from './images';
|
||||
import { normalizeEdits } from './edits';
|
||||
import { outputSize, Renderer } from '../gl/renderer';
|
||||
|
||||
export type ExportFormat = 'jpeg' | 'png' | 'webp';
|
||||
export type ResizeMode = 'none' | 'longEdge' | 'percent';
|
||||
|
||||
export interface ExportOptions {
|
||||
format: ExportFormat;
|
||||
quality: number;
|
||||
resize: ResizeMode;
|
||||
longEdge: number;
|
||||
percent: number;
|
||||
nameTemplate: string;
|
||||
}
|
||||
|
||||
export const DEFAULT_EXPORT: ExportOptions = {
|
||||
format: 'jpeg',
|
||||
quality: 92,
|
||||
resize: 'none',
|
||||
longEdge: 3000,
|
||||
percent: 100,
|
||||
nameTemplate: '{name}',
|
||||
};
|
||||
|
||||
const MIME: Record<ExportFormat, string> = {
|
||||
jpeg: 'image/jpeg',
|
||||
png: 'image/png',
|
||||
webp: 'image/webp',
|
||||
};
|
||||
|
||||
const EXT: Record<ExportFormat, string> = {
|
||||
jpeg: 'jpg',
|
||||
png: 'png',
|
||||
webp: 'webp',
|
||||
};
|
||||
|
||||
function download(blob: Blob, filename: string) {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 4000);
|
||||
}
|
||||
|
||||
function pad(n: number) {
|
||||
return String(n).padStart(2, '0');
|
||||
}
|
||||
|
||||
export function buildFileName(template: string, photo: PhotoRecord, index: number, opts: ExportOptions) {
|
||||
const stamp = new Date(photo.takenAt ?? photo.addedAt);
|
||||
const base = photo.name.replace(/\.[^.]+$/, '') || 'export';
|
||||
const name = (template || '{name}')
|
||||
.replace(/\{name\}/g, base)
|
||||
.replace(/\{date\}/g, `${stamp.getFullYear()}${pad(stamp.getMonth() + 1)}${pad(stamp.getDate())}`)
|
||||
.replace(/\{time\}/g, `${pad(stamp.getHours())}${pad(stamp.getMinutes())}`)
|
||||
.replace(/\{index\}/g, String(index + 1).padStart(3, '0'))
|
||||
.replace(/[\\/:*?"<>|]+/g, '-')
|
||||
.trim();
|
||||
return `${name || 'export'}.${EXT[opts.format]}`;
|
||||
}
|
||||
|
||||
function targetSize(photo: PhotoRecord, edits: Edits, bitmap: ImageBitmap, opts: ExportOptions) {
|
||||
const size = outputSize(edits, bitmap.width, bitmap.height);
|
||||
const longEdge = Math.max(size.w, size.h);
|
||||
let scale = 1;
|
||||
if (opts.resize === 'longEdge' && opts.longEdge > 0 && longEdge > opts.longEdge) {
|
||||
scale = opts.longEdge / longEdge;
|
||||
} else if (opts.resize === 'percent') {
|
||||
scale = Math.min(2, Math.max(0.05, opts.percent / 100));
|
||||
}
|
||||
return {
|
||||
w: Math.max(1, Math.round(size.w * scale)),
|
||||
h: Math.max(1, Math.round(size.h * scale)),
|
||||
};
|
||||
}
|
||||
|
||||
export async function renderPhotoBlob(photo: PhotoRecord, opts: ExportOptions): Promise<Blob> {
|
||||
const bitmap = await getBitmap(photo);
|
||||
const edits = normalizeEdits(photo.edits);
|
||||
const { w, h } = targetSize(photo, edits, bitmap, opts);
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = 1;
|
||||
canvas.height = 1;
|
||||
const renderer = new Renderer(canvas);
|
||||
try {
|
||||
renderer.setImage(bitmap);
|
||||
renderer.render(edits, { width: w, height: h });
|
||||
const mime = MIME[opts.format];
|
||||
const blob = await new Promise<Blob | null>((resolve) =>
|
||||
canvas.toBlob(resolve, mime, Math.min(1, Math.max(0.1, opts.quality / 100))),
|
||||
);
|
||||
if (!blob) throw new Error('Export failed while encoding the image');
|
||||
return blob;
|
||||
} finally {
|
||||
renderer.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
export async function exportPhotos(
|
||||
photos: PhotoRecord[],
|
||||
opts: ExportOptions,
|
||||
onProgress?: (done: number, total: number) => void,
|
||||
): Promise<{ count: number; failed: string[] }> {
|
||||
let count = 0;
|
||||
const failed: string[] = [];
|
||||
for (let i = 0; i < photos.length; i++) {
|
||||
const photo = photos[i];
|
||||
try {
|
||||
const blob = await renderPhotoBlob(photo, opts);
|
||||
download(blob, buildFileName(opts.nameTemplate, photo, i, opts));
|
||||
count++;
|
||||
} catch (err) {
|
||||
console.error('Export failed for', photo.name, err);
|
||||
failed.push(photo.name);
|
||||
}
|
||||
onProgress?.(i + 1, photos.length);
|
||||
// Give the browser a beat so long batches keep the tab responsive.
|
||||
await new Promise((r) => setTimeout(r, 0));
|
||||
}
|
||||
return { count, failed };
|
||||
}
|
||||
|
||||
/** Backwards compatible single-photo export. */
|
||||
export async function exportPhoto(photo: PhotoRecord, maxLongEdge = 0): Promise<void> {
|
||||
const opts: ExportOptions = {
|
||||
...DEFAULT_EXPORT,
|
||||
resize: maxLongEdge > 0 ? 'longEdge' : 'none',
|
||||
longEdge: maxLongEdge || DEFAULT_EXPORT.longEdge,
|
||||
};
|
||||
const blob = await renderPhotoBlob(photo, opts);
|
||||
download(blob, buildFileName(opts.nameTemplate, photo, 0, opts));
|
||||
}
|
||||
Reference in New Issue
Block a user