ebe0c77f7e
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>
97 lines
2.7 KiB
JavaScript
97 lines
2.7 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;
|
|
}
|
|
|
|
/** Drops the cached grid thumbnail so the next request refetches it. */
|
|
export async function removeThumb(id) {
|
|
const thumb = findThumb(id);
|
|
if (thumb) await fsp.rm(thumb, { force: true });
|
|
}
|
|
|
|
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;
|
|
}
|