Add an admin action that rebuilds preview proxies

A library imported by an older pipeline keeps whatever proxy it was given, and
the editor can only show those bytes. The Storage tab now re-decodes every
camera raw from its original, writing a fresh JPEG proxy, thumbnail, 16-bit
sidecar and denoise prune, then stamps previewVersion so browsers refetch.

The decode loop runs one photo at a time on the server behind a status route:
an hour of LibRaw work would time out as a request, and parallel decodes would
starve the editor. The single-photo reprocess route shares the same helper.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 06:20:44 +00:00
parent 2bec4102a2
commit 158dccddf1
6 changed files with 342 additions and 30 deletions
+88 -30
View File
@@ -77,6 +77,7 @@ import {
writeThumb,
writeThumb16,
} from './storage.mjs';
import { previewRebuildTargets, startPreviewRebuild } from './previewRebuild.mjs';
import { decodeRaw, isRawName, rawSupport } from './raw.mjs';
import {
bridgeConfigured,
@@ -969,6 +970,48 @@ route('PUT', '/api/admin/storage', async ({ req, res }) => {
}
});
/* --------------------------------------------------------- preview rebuild */
let previewRebuild = null;
function previewRebuildStatus() {
return (
previewRebuild?.snapshot() ?? {
state: 'idle',
total: 0,
done: 0,
failed: 0,
sidecars: 0,
startedAt: null,
finishedAt: null,
errors: [],
}
);
}
route('GET', '/api/admin/previews/rebuild', async ({ req, res }) => {
if (!requireAdmin(req, res)) return;
json(res, 200, { rebuild: previewRebuildStatus() });
});
/**
* Regenerate every raw proxy from its original, which is what a library
* imported by an older pipeline needs after the decode changes. The loop is CPU
* bound and can run for an hour, so the request only starts it and the panel
* polls the status route.
*/
route('POST', '/api/admin/previews/rebuild', async ({ req, res }) => {
if (!requireAdmin(req, res)) return;
const current = previewRebuildStatus();
if (current.state === 'running') return json(res, 202, { rebuild: current });
const targets = previewRebuildTargets(listPhotos());
previewRebuild = startPreviewRebuild({
targets,
regenerate: (photo) => regenerateRawPreview(photo.id, photo, photoOwner(photo.id) ?? ''),
});
json(res, 202, { rebuild: previewRebuild.snapshot() });
});
route('GET', '/api/quota', async ({ req, res }) => {
const user = currentUser(req);
if (!user) return error(res, 401, 'Not signed in');
@@ -2039,10 +2082,48 @@ route('POST', '/api/photos/:id/original', async ({ req, res, params }) => {
});
/**
* Re-decode a stored RAW with the current pipeline settings. The original is
* never touched; only the editable proxy, its thumbnail and the cached denoise
* renders are replaced, so existing edits keep working on fresh pixels.
* Re-decode one stored RAW and replace everything derived from it. Shared by the
* single-photo route and the admin rebuild so the two cannot drift apart.
*
* The original is never touched; only the editable proxy, its thumbnail, the
* 16-bit sidecar and the cached denoise renders are replaced, so existing edits
* keep working on fresh pixels.
*/
async function regenerateRawPreview(id, photo, ownerId) {
const original = findOriginal(id);
if (!original) throw new Error('The RAW original is missing from storage');
const decoded = await decodeRaw(await fsp.readFile(original));
await writePreview(id, decoded.jpeg, decoded.ext);
if (decoded.preview16) {
await writePreview16(id, decoded.preview16.data);
// The thumbnail may not exist yet; drop any camera-native tile so the grid
// rebuilds it from the new pixels.
await removeThumb16(id);
} else {
await removePreview16(id);
await removeThumb16(id);
}
const thumb = await makeThumb(decoded.jpeg, 560);
if (thumb) await writeThumb(id, thumb);
// The old denoised renders were made from the previous proxy.
clearDenoised(id);
const info = await jpegInfo(decoded.jpeg);
const width = info?.width || decoded.width || photo.width;
const height = info?.height || decoded.height || photo.height;
// Stamp the record so cached previews and thumbnails are fetched again.
const previewVersion = Date.now();
// Pass the owner explicitly: savePhoto defaults to '' and would orphan the row.
savePhoto({ ...photo, width, height, mime: 'image/jpeg', previewVersion }, Date.now(), ownerId);
return {
width,
height,
engine: decoded.engine,
note: decoded.note ?? null,
previewVersion,
sidecar: Boolean(decoded.preview16),
};
}
route('POST', '/api/photos/:id/reprocess', async ({ req, res, params }) => {
const user = currentUser(req);
if (!user) return error(res, 401, 'Not signed in');
@@ -2052,34 +2133,11 @@ route('POST', '/api/photos/:id/reprocess', async ({ req, res, params }) => {
const original = findOriginal(params.id);
if (!original) return error(res, 404, 'The RAW original is missing from storage');
try {
const decoded = await decodeRaw(await fsp.readFile(original));
await writePreview(params.id, decoded.jpeg, decoded.ext);
if (decoded.preview16) {
await writePreview16(params.id, decoded.preview16.data);
await removeThumb16(params.id);
} else {
await removePreview16(params.id);
await removeThumb16(params.id);
}
const thumb = await makeThumb(decoded.jpeg, 560);
if (thumb) await writeThumb(params.id, thumb);
// The old denoised renders were made from the previous proxy.
clearDenoised(params.id);
const info = await jpegInfo(decoded.jpeg);
const width = info?.width || decoded.width || photo.width;
const height = info?.height || decoded.height || photo.height;
// Stamp the record so cached previews and thumbnails are fetched again.
const previewVersion = Date.now();
// Pass the owner explicitly: savePhoto defaults to '' and would orphan the row.
savePhoto({ ...photo, width, height, mime: 'image/jpeg', previewVersion }, Date.now(), user.id);
const result = await regenerateRawPreview(params.id, photo, user.id);
json(res, 200, {
width,
height,
engine: decoded.engine,
note: decoded.note ?? null,
previewVersion,
previewUrl: `/api/photos/${params.id}/preview?v=${previewVersion}`,
thumbUrl: `/api/photos/${params.id}/thumb?v=${previewVersion}`,
...result,
previewUrl: `/api/photos/${params.id}/preview?v=${result.previewVersion}`,
thumbUrl: `/api/photos/${params.id}/thumb?v=${result.previewVersion}`,
});
} catch (err) {
return error(res, 422, err instanceof Error ? err.message : 'Could not reprocess this RAW file');