0cb315ea88
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>
162 lines
4.4 KiB
TypeScript
162 lines
4.4 KiB
TypeScript
import type { EventRecord, PhotoRecord, Preset } from '../types';
|
|
|
|
export interface Health {
|
|
ok: boolean;
|
|
app: string;
|
|
version: string;
|
|
needsSetup: boolean;
|
|
registrationOpen: boolean;
|
|
photos: number;
|
|
bytes: number;
|
|
raw: { libraw: boolean; sharp: boolean; formats: string[] };
|
|
}
|
|
|
|
export interface SessionUser {
|
|
id: string;
|
|
email: string;
|
|
name: string;
|
|
role: string;
|
|
}
|
|
|
|
export interface UploadResult {
|
|
raw: boolean;
|
|
engine?: 'libraw' | 'embedded';
|
|
width?: number;
|
|
height?: number;
|
|
mime?: string;
|
|
size?: number;
|
|
previewUrl?: string;
|
|
thumbUrl?: string;
|
|
note?: string | null;
|
|
}
|
|
|
|
export class ApiError extends Error {
|
|
status: number;
|
|
constructor(message: string, status: number) {
|
|
super(message);
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
let res: Response;
|
|
try {
|
|
res = await fetch(path, { credentials: 'same-origin', ...init });
|
|
} catch {
|
|
throw new ApiError('Cannot reach the Lumenroom server', 0);
|
|
}
|
|
if (!res.ok) {
|
|
let message = `Request failed (${res.status})`;
|
|
try {
|
|
const body = (await res.json()) as { error?: string };
|
|
if (body?.error) message = body.error;
|
|
} catch {
|
|
/* non-JSON error body */
|
|
}
|
|
throw new ApiError(message, res.status);
|
|
}
|
|
if (res.status === 204) return undefined as T;
|
|
return (await res.json()) as T;
|
|
}
|
|
|
|
function jsonBody(payload: unknown): RequestInit {
|
|
return {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
};
|
|
}
|
|
|
|
/** Returns null when the app is running without a server (pure browser library). */
|
|
export async function getHealth(): Promise<Health | null> {
|
|
try {
|
|
const res = await fetch('/api/health', { credentials: 'same-origin' });
|
|
if (!res.ok) return null;
|
|
const body = (await res.json()) as Health;
|
|
return body?.ok ? body : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function register(input: {
|
|
email: string;
|
|
password: string;
|
|
name?: string;
|
|
invite?: string;
|
|
}): Promise<SessionUser> {
|
|
const res = await request<{ user: SessionUser }>('/api/auth/register', jsonBody(input));
|
|
return res.user;
|
|
}
|
|
|
|
export async function login(email: string, password: string): Promise<SessionUser> {
|
|
const res = await request<{ user: SessionUser }>('/api/auth/login', jsonBody({ email, password }));
|
|
return res.user;
|
|
}
|
|
|
|
export async function logout(): Promise<void> {
|
|
await request('/api/auth/logout', { method: 'POST' });
|
|
}
|
|
|
|
export async function currentUser(): Promise<SessionUser | null> {
|
|
try {
|
|
const res = await request<{ user: SessionUser }>('/api/auth/me');
|
|
return res.user;
|
|
} catch (err) {
|
|
if (err instanceof ApiError && err.status === 401) return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export interface SyncPayload {
|
|
user: SessionUser;
|
|
events: EventRecord[];
|
|
photos: PhotoRecord[];
|
|
presets: Preset[];
|
|
}
|
|
|
|
export function pullAll(): Promise<SyncPayload> {
|
|
return request<SyncPayload>('/api/sync');
|
|
}
|
|
|
|
export function uploadOriginal(id: string, file: File | Blob, name: string): Promise<UploadResult> {
|
|
const query = new URLSearchParams({ name, mime: file.type || 'application/octet-stream' });
|
|
return request<UploadResult>(`/api/photos/${encodeURIComponent(id)}/original?${query}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/octet-stream' },
|
|
body: file,
|
|
});
|
|
}
|
|
|
|
export function pushPhoto(photo: PhotoRecord): Promise<PhotoRecord> {
|
|
return request<{ photo: PhotoRecord }>(`/api/photos/${encodeURIComponent(photo.id)}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(photo),
|
|
}).then((res) => res.photo);
|
|
}
|
|
|
|
export function deletePhotoRemote(id: string): Promise<void> {
|
|
return request(`/api/photos/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export function pushEvent(event: EventRecord): Promise<EventRecord> {
|
|
return request<{ event: EventRecord }>(`/api/events/${encodeURIComponent(event.id)}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(event),
|
|
}).then((res) => res.event);
|
|
}
|
|
|
|
export function deleteEventRemote(id: string): Promise<void> {
|
|
return request(`/api/events/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export function pushPresets(presets: Preset[]): Promise<void> {
|
|
return request('/api/presets', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ presets }),
|
|
});
|
|
}
|