Branchwell

Lumen & Gesso / website · lumengesso.com / scripts / cdn-contact-sheet.mjs

1#!/usr/bin/env node
2// Browse every portrait we have in Cloudflare Images. Lists all images in
3// the account (paginated), keeps the ones under the `portraits/` id prefix
4// (our site assets), and writes a self-contained HTML contact sheet of
5// thumbnails + ids + delivery URLs that you can open in a browser.
6//
7// node scripts/cdn-contact-sheet.mjs # → /tmp/lg-cdn-portraits.html
8// OUT=~/Desktop/portraits.html node scripts/cdn-contact-sheet.mjs
9// PREFIX=portraits/lifestyle node scripts/cdn-contact-sheet.mjs # filter
10
11import fs from 'node:fs';
12import path from 'node:path';
13import os from 'node:os';
14import { fileURLToPath } from 'node:url';
15
16const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
17function loadEnvLocal() {
18 const p = path.join(ROOT, '.env.local');
19 if (!fs.existsSync(p)) return;
20 for (const raw of fs.readFileSync(p, 'utf8').split('\n')) {
21 const line = raw.trim();
22 if (!line || line.startsWith('#')) continue;
23 const eq = line.indexOf('='); if (eq < 0) continue;
24 const k = line.slice(0, eq).trim();
25 let v = line.slice(eq + 1).trim();
26 if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1);
27 if (!(k in process.env)) process.env[k] = v;
28 }
29}
30loadEnvLocal();
31const ACCOUNT = process.env.CF_ACCOUNT_ID;
32const TOKEN = process.env.CF_IMAGES_API_TOKEN;
33const HASH = process.env.CF_IMAGES_HASH;
34if (!ACCOUNT || !TOKEN || !HASH) { console.error('Missing CF creds'); process.exit(1); }
35
36const PREFIX = process.env.PREFIX || 'portraits/';
37const OUT = (process.env.OUT || path.join(os.tmpdir(), 'lg-cdn-portraits.html')).replace(/^~/, os.homedir());
38
39// CF Images v2 list — continuation-token paginated.
40async function listAll() {
41 const all = [];
42 let token = '';
43 for (let guard = 0; guard < 100; guard++) {
44 const url = new URL(`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT}/images/v2`);
45 url.searchParams.set('per_page', '1000');
46 if (token) url.searchParams.set('continuation_token', token);
47 const r = await fetch(url, { headers: { Authorization: `Bearer ${TOKEN}` } });
48 const j = await r.json();
49 if (!j.success) throw new Error('CF list failed: ' + JSON.stringify(j.errors));
50 all.push(...(j.result.images || []));
51 token = j.result.continuation_token;
52 if (!token || (j.result.images || []).length === 0) break;
53 }
54 return all;
55}
56
57const images = (await listAll())
58 .filter((im) => im.id.startsWith(PREFIX))
59 .sort((a, b) => a.id.localeCompare(b.id));
60
61const esc = (s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
62const cards = images.map((im) => {
63 const pub = `https://imagedelivery.net/${HASH}/${im.id}/public`;
64 return `<figure><a href="${pub}" target="_blank" rel="noopener"><img loading="lazy" src="${pub}" alt="${esc(im.id)}"></a><figcaption>${esc(im.id.replace(/^portraits\//, ''))}</figcaption></figure>`;
65}).join('\n');
66
67const html = `<!doctype html><html lang="en"><head><meta charset="utf-8">
68<meta name="viewport" content="width=device-width,initial-scale=1">
69<title>Lumen &amp; Gesso — CDN portraits (${images.length})</title>
70<style>
71 body{margin:0;background:#1d1a17;color:#e9e2d8;font:14px/1.4 -apple-system,system-ui,sans-serif}
72 header{position:sticky;top:0;background:#1d1a17ee;backdrop-filter:blur(6px);padding:16px 20px;border-bottom:1px solid #3a342d;z-index:1}
73 h1{margin:0;font-size:16px;font-weight:600}
74 .sub{color:#a99;font-size:12px;margin-top:4px}
75 #q{margin-top:10px;width:min(420px,100%);padding:8px 10px;border-radius:6px;border:1px solid #4a443c;background:#26221d;color:#e9e2d8}
76 .grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(190px,1fr));gap:14px;padding:18px 20px}
77 figure{margin:0;background:#26221d;border:1px solid #3a342d;border-radius:8px;overflow:hidden}
78 figure img{display:block;width:100%;height:190px;object-fit:contain;background:#0f0d0b}
79 figcaption{padding:8px 10px;font-size:11px;color:#cfc6b8;word-break:break-all}
80 a{color:inherit;text-decoration:none}
81</style></head><body>
82<header>
83 <h1>Lumen &amp; Gesso — portraits on Cloudflare Images</h1>
84 <div class="sub">${images.length} images under <code>${esc(PREFIX)}</code> · click any tile to open the full delivery URL</div>
85 <input id="q" placeholder="filter by filename… (e.g. lifestyle, popart, before-)" oninput="for(const f of document.querySelectorAll('figure')){f.style.display=f.textContent.toLowerCase().includes(this.value.toLowerCase())?'':'none'}">
86</header>
87<div class="grid">
88${cards}
89</div></body></html>`;
90
91fs.writeFileSync(OUT, html);
92console.log(`${images.length} portraits under "${PREFIX}" → ${OUT}`);
93console.log(`open: open "${OUT}"`);