const SCAN_EXTS = ["jpg", "jpeg", "png", "webp"]; const SCAN_MAX = 60; function probeImage(src) { return new Promise((resolve) => { const img = new Image(); img.onload = () => resolve(src); img.onerror = () => resolve(null); img.src = src; }); } async function findFrame(dir, num) { for (const ext of SCAN_EXTS) { const hit = await probeImage(`${dir}/${num}.${ext}`); if (hit) return hit; const up = await probeImage(`${dir}/${num}.${ext.toUpperCase()}`); if (up) return up; } return null; } // A browser cannot list a directory, so the numbered scan has to guess at // 01, 02, 03 … and stops at the first gap. It only survives here as a // fallback for folders that have no manifest yet. async function scanNumbered(dir) { const first = await findFrame(dir, "01"); if (!first) return []; const found = [{ src: first, num: "01" }]; const BATCH = 8; let start = 2; while (start <= SCAN_MAX) { const nums = []; for (let n = start; n < start + BATCH && n <= SCAN_MAX; n++) nums.push(String(n).padStart(2, "0")); const hits = await Promise.all(nums.map((num) => findFrame(dir, num).then((src) => (src ? { src, num } : null)))); let stop = false; for (const h of hits) { if (h) found.push(h); else { stop = true; break; } } if (stop) break; start += BATCH; } return found; } // manifest.json is written by `python3 _tools/build-manifests.py`. It lists // every photo by its real filename, so originals keep the names the camera // and the shoot gave them, and it carries pixel sizes so the page can lay a // frame out before the image arrives. // "dir/_sizes/name@800.jpg 800w, dir/_sizes/name@1400.jpg 1400w, dir/name.jpg 2000w" function buildSrcSet(dir, frame) { if (!frame.variants || !frame.variants.length || !frame.w) return null; const stem = frame.src.replace(/\.[^.]+$/, ""); const parts = frame.variants.map((w) => `${dir}/_sizes/${stem}@${w}.jpg ${w}w`); parts.push(`${dir}/${frame.src} ${frame.w}w`); return parts.join(", "); } // A folder's manifest is the same for the whole visit, and several components // ask for the same one — the projects list and the project page both want // sven's. Holding the promise means one request instead of a fresh one per // mount, while a reload still picks up a regenerated manifest. const manifestCache = new Map(); async function readManifest(dir) { let data; try { if (!manifestCache.has(dir)) { // "no-cache" means ask the server whether this is still current, not // "do not cache": it costs a 304 of a couple of hundred bytes and keeps // a regenerated manifest from being ignored until the browser's own cache // happens to expire. The Map above is what removes the repeat fetches. manifestCache.set(dir, fetch(`${dir}/manifest.json`, { cache: "no-cache" }) .then((res) => (res.ok ? res.json() : null))); } data = await manifestCache.get(dir); if (!data) return null; } catch (err) { manifestCache.delete(dir); return null; // no manifest, or served from file:// — fall back to scanning } const chapters = (data.chapters || []) .map((chapter) => ({ title: chapter.title || null, note: chapter.note || null, frames: (chapter.frames || []).map((frame) => { const f = typeof frame === "string" ? { src: frame } : frame; // A film sits in the sequence like any other frame — it has no file in // the folder, only a YouTube id, so it is built here rather than from a // photograph on disk. `poster` names a still from this folder to show // before it plays; without one, YouTube's own thumbnail is used. if (f.youtube) { return { youtube: String(f.youtube).trim(), poster: f.poster ? `${dir}/${f.poster}` : null, src: null, srcSet: null, name: f.youtube, num: null, w: f.w || 16, h: f.h || 9, full: !!f.full, caption: f.caption || null, portrait: false, // a film is never paired into a diptych }; } return { youtube: null, poster: null, src: `${dir}/${f.src}`, // Narrow copies from _sizes/, offered alongside the full file so a // phone fetches a phone-sized photograph. Null until // optimise-images.py has cut them, and the pages fall back to `src`. srcSet: buildSrcSet(dir, f), name: f.src, // Basename without extension, so numbered folders keep working with // caption maps keyed "01", "02", … (see ROOMS in GalleryPage). num: f.src.replace(/\.[^.]+$/, ""), w: f.w || null, h: f.h || null, full: !!f.full, caption: f.caption || null, portrait: f.w && f.h ? f.h > f.w : false, }; }), })) .filter((chapter) => chapter.frames.length); if (!chapters.length) return null; const frames = chapters.flatMap((c) => c.frames); // The hero may be a file the sequence excludes, so build its path directly // rather than looking it up among the frames. Falling back, skip any film: // it has no photograph of its own to stand behind the page. const firstPhoto = frames.find((f) => f.src); const hero = data.hero ? `${dir}/${data.hero}` : (firstPhoto ? firstPhoto.src : null); return { chapters, frames, hero }; } async function scanFolder(dir) { const manifest = await readManifest(dir); if (manifest) return manifest; const numbered = await scanNumbered(dir); const frames = numbered.map((f) => ({ src: f.src, srcSet: null, name: f.num, num: f.num, w: null, h: null, full: false, caption: null, portrait: false, })); return { chapters: frames.length ? [{ title: null, note: null, frames }] : [], frames, hero: frames.length ? frames[0].src : null, }; } function useFolder(dir) { const [data, setData] = React.useState(null); React.useEffect(() => { if (!dir) { setData({ chapters: [], frames: [] }); return; } let cancelled = false; setData(null); scanFolder(dir).then((r) => { if (!cancelled) setData(r); }); return () => { cancelled = true; }; }, [dir]); return data; } Object.assign(window, { scanFolder, useFolder, probeImage });