#!/usr/bin/env node
// Browse every portrait we have in Cloudflare Images. Lists all images in
// the account (paginated), keeps the ones under the `portraits/` id prefix
// (our site assets), and writes a self-contained HTML contact sheet of
// thumbnails + ids + delivery URLs that you can open in a browser.
//
// node scripts/cdn-contact-sheet.mjs # → /tmp/lg-cdn-portraits.html
// OUT=~/Desktop/portraits.html node scripts/cdn-contact-sheet.mjs
// PREFIX=portraits/lifestyle node scripts/cdn-contact-sheet.mjs # filter
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
function loadEnvLocal() {
const p = path.join(ROOT, '.env.local');
if (!fs.existsSync(p)) return;
for (const raw of fs.readFileSync(p, 'utf8').split('\n')) {
const line = raw.trim();
if (!line || line.startsWith('#')) continue;
const eq = line.indexOf('='); if (eq < 0) continue;
const k = line.slice(0, eq).trim();
let v = line.slice(eq + 1).trim();
if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1);
if (!(k in process.env)) process.env[k] = v;
}
}
loadEnvLocal();
const ACCOUNT = process.env.CF_ACCOUNT_ID;
const TOKEN = process.env.CF_IMAGES_API_TOKEN;
const HASH = process.env.CF_IMAGES_HASH;
if (!ACCOUNT || !TOKEN || !HASH) { console.error('Missing CF creds'); process.exit(1); }
const PREFIX = process.env.PREFIX || 'portraits/';
const OUT = (process.env.OUT || path.join(os.tmpdir(), 'lg-cdn-portraits.html')).replace(/^~/, os.homedir());
// CF Images v2 list — continuation-token paginated.
async function listAll() {
const all = [];
let token = '';
for (let guard = 0; guard < 100; guard++) {
const url = new URL(`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT}/images/v2`);
url.searchParams.set('per_page', '1000');
if (token) url.searchParams.set('continuation_token', token);
const r = await fetch(url, { headers: { Authorization: `Bearer ${TOKEN}` } });
const j = await r.json();
if (!j.success) throw new Error('CF list failed: ' + JSON.stringify(j.errors));
all.push(...(j.result.images || []));
token = j.result.continuation_token;
if (!token || (j.result.images || []).length === 0) break;
}
return all;
}
const images = (await listAll())
.filter((im) => im.id.startsWith(PREFIX))
.sort((a, b) => a.id.localeCompare(b.id));
const esc = (s) => s.replace(/&/g, '&').replace(//g, '>');
const cards = images.map((im) => {
const pub = `https://imagedelivery.net/${HASH}/${im.id}/public`;
return `
${esc(PREFIX)} · click any tile to open the full delivery URL