Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import type { PhotoRecord } from '../types';
|
||||
import { getAsset, putAsset } from './db';
|
||||
|
||||
const CACHE_LIMIT = 6;
|
||||
const cache = new Map<string, ImageBitmap>();
|
||||
|
||||
/** Where to fetch the original (or raw proxy) when it is not cached on this device. */
|
||||
export function remoteAssetUrl(photo: PhotoRecord): string {
|
||||
if (photo.raw) return photo.previewUrl || `/api/photos/${encodeURIComponent(photo.id)}/preview`;
|
||||
return `/api/photos/${encodeURIComponent(photo.id)}/original`;
|
||||
}
|
||||
|
||||
/** Virtual copies reuse the master's bytes when no local copy exists. */
|
||||
async function copyAsset(photo: PhotoRecord, id: string): Promise<Blob | undefined> {
|
||||
const own = await getAsset(photo.id);
|
||||
if (own) {
|
||||
if (photo.copyOf) await putAsset(id, own).catch(() => undefined);
|
||||
return own;
|
||||
}
|
||||
if (!photo.copyOf) return undefined;
|
||||
const master = await getAsset(photo.copyOf);
|
||||
if (master) {
|
||||
await putAsset(id, master).catch(() => undefined);
|
||||
return master;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function fetchRemoteCopy(photo: PhotoRecord): Promise<Blob | null> {
|
||||
const direct = await fetchRemote(photo);
|
||||
if (direct || !photo.copyOf) return direct;
|
||||
// A virtual copy has no server file of its own; ask for the master's proxy.
|
||||
try {
|
||||
const master = { ...photo, id: photo.copyOf, copyOf: undefined };
|
||||
const res = await fetch(remoteAssetUrl(master), { credentials: 'same-origin' });
|
||||
if (!res.ok) return null;
|
||||
const blob = await res.blob();
|
||||
await putAsset(photo.id, blob).catch(() => undefined);
|
||||
return blob;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchRemote(photo: PhotoRecord): Promise<Blob | null> {
|
||||
try {
|
||||
const res = await fetch(remoteAssetUrl(photo), { credentials: 'same-origin' });
|
||||
if (!res.ok) return null;
|
||||
const blob = await res.blob();
|
||||
// Cache it locally so the photo keeps working offline after the first look.
|
||||
await putAsset(photo.id, blob).catch(() => undefined);
|
||||
return blob;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getBitmap(photo: PhotoRecord): Promise<ImageBitmap> {
|
||||
const hit = cache.get(photo.id);
|
||||
if (hit) {
|
||||
cache.delete(photo.id);
|
||||
cache.set(photo.id, hit);
|
||||
return hit;
|
||||
}
|
||||
|
||||
let blob = await copyAsset(photo, photo.id);
|
||||
if (!blob) blob = (await fetchRemoteCopy(photo)) ?? undefined;
|
||||
if (!blob) {
|
||||
throw new Error(
|
||||
photo.raw
|
||||
? 'This raw file has no editable proxy yet. Sign in to the server and sync to fetch it.'
|
||||
: 'Original image data is missing from browser storage',
|
||||
);
|
||||
}
|
||||
|
||||
// Apply EXIF orientation so the pixels the editor works on are upright.
|
||||
const bitmap = await createImageBitmap(blob, { imageOrientation: 'from-image' });
|
||||
cache.set(photo.id, bitmap);
|
||||
while (cache.size > CACHE_LIMIT) {
|
||||
const first = cache.keys().next();
|
||||
if (first.done) break;
|
||||
const old = cache.get(first.value);
|
||||
cache.delete(first.value);
|
||||
old?.close();
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
export function evictBitmap(photoId: string) {
|
||||
const bmp = cache.get(photoId);
|
||||
if (!bmp) return;
|
||||
cache.delete(photoId);
|
||||
bmp.close();
|
||||
}
|
||||
|
||||
export function clearBitmapCache() {
|
||||
for (const bmp of cache.values()) bmp.close();
|
||||
cache.clear();
|
||||
for (const bmp of denoisedCache.values()) bmp.close();
|
||||
denoisedCache.clear();
|
||||
}
|
||||
|
||||
const denoisedCache = new Map<string, ImageBitmap>();
|
||||
|
||||
/**
|
||||
* The server-rendered denoised proxy for an edit's `aiDenoise.key`.
|
||||
*
|
||||
* These are separate files from the edit and cost a few seconds to produce, so
|
||||
* they are cached by key and never re-fetched while the key stays the same.
|
||||
* Returns null when the asset is gone (cache pruned, storage moved), which the
|
||||
* caller treats as "no denoise".
|
||||
*/
|
||||
export async function getDenoisedBitmap(photo: PhotoRecord, key: string): Promise<ImageBitmap | null> {
|
||||
const sig = `${photo.id}:${key}`;
|
||||
const hit = denoisedCache.get(sig);
|
||||
if (hit) return hit;
|
||||
try {
|
||||
const res = await fetch(
|
||||
`/api/photos/${encodeURIComponent(photo.id)}/denoised?v=${encodeURIComponent(key)}`,
|
||||
{ credentials: 'same-origin' },
|
||||
);
|
||||
if (!res.ok) return null;
|
||||
const bitmap = await createImageBitmap(await res.blob(), { imageOrientation: 'from-image' });
|
||||
denoisedCache.set(sig, bitmap);
|
||||
while (denoisedCache.size > 4) {
|
||||
const first = denoisedCache.keys().next();
|
||||
if (first.done) break;
|
||||
denoisedCache.get(first.value)?.close();
|
||||
denoisedCache.delete(first.value);
|
||||
}
|
||||
return bitmap;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user