Files
glenloch-media/lumenroom/server/storage.mjs
T
bhursey 0cb315ea88
CI / build (push) Has been cancelled
feat(editor): bridge the advanced photo editor into the event workflow
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>
2026-11-21 01:34:20 +00:00

91 lines
2.5 KiB
JavaScript

import fs from 'node:fs';
import fsp from 'node:fs/promises';
import path from 'node:path';
import { ORIGINAL_DIR, PREVIEW_DIR, THUMB_DIR } from './config.mjs';
for (const dir of [ORIGINAL_DIR, PREVIEW_DIR, THUMB_DIR]) fs.mkdirSync(dir, { recursive: true });
const SAFE = /^[A-Za-z0-9_-]{1,80}$/;
function safeId(id) {
if (!SAFE.test(id)) throw new Error('Invalid asset id');
return id;
}
/** Original files keep their real extension so a raw can be recovered later. */
function originalPath(id, ext = 'bin') {
return path.join(ORIGINAL_DIR, `${safeId(id)}.${ext.replace(/[^a-z0-9]/gi, '') || 'bin'}`);
}
export async function writeOriginal(id, ext, data) {
const target = originalPath(id, ext);
await fsp.writeFile(target, data);
return target;
}
/**
* Reads are lookups: a malformed id simply has no file, so it must not throw a
* 500 out of the request handler. Writers keep the strict check.
*/
function lookupId(id) {
return typeof id === 'string' && SAFE.test(id) ? id : null;
}
export function findOriginal(id) {
const safe = lookupId(id);
if (!safe) return null;
const prefix = `${safe}.`;
for (const name of fs.readdirSync(ORIGINAL_DIR)) {
if (name.startsWith(prefix)) return path.join(ORIGINAL_DIR, name);
}
return null;
}
export async function writePreview(id, data, ext = 'jpg') {
const target = path.join(PREVIEW_DIR, `${safeId(id)}.${ext}`);
await fsp.writeFile(target, data);
return target;
}
export function findPreview(id) {
const safe = lookupId(id);
if (!safe) return null;
for (const ext of ['jpg', 'png']) {
const p = path.join(PREVIEW_DIR, `${safe}.${ext}`);
if (fs.existsSync(p)) return p;
}
return null;
}
export async function writeThumb(id, data) {
const target = path.join(THUMB_DIR, `${safeId(id)}.jpg`);
await fsp.writeFile(target, data);
return target;
}
export function findThumb(id) {
const safe = lookupId(id);
if (!safe) return null;
const p = path.join(THUMB_DIR, `${safe}.jpg`);
return fs.existsSync(p) ? p : null;
}
export async function removeAsset(id) {
const files = [findOriginal(id), findPreview(id), findThumb(id)];
await Promise.all(files.map((f) => (f ? fsp.rm(f, { force: true }) : Promise.resolve())));
}
export function storageBytes() {
let total = 0;
for (const dir of [ORIGINAL_DIR, PREVIEW_DIR, THUMB_DIR]) {
for (const name of fs.readdirSync(dir)) {
try {
total += fs.statSync(path.join(dir, name)).size;
} catch {
/* file vanished mid-scan */
}
}
}
return total;
}