Link-domain pool by channel + safety checks, hosted page images + provider allowlist, Reports pages and zones sections
Item 1 (domains.js): every short link domain in linkDomains gets a channel (email / messaging / social / reserve)
via the new linkDomainChannels site setting ("host:channel,..."; default order email, messaging, social, email,
reserve). Checks every 6 h + Admin > Link domains "Check now": Google Safe Browsing v4 when GSB_API_KEY is set
(unknown otherwise), URIBL + SURBL DNS lists (answer = listed, NXDOMAIN = clean, 127.0.0.1 or errors = unknown),
and a TelegramBot-UA reachability probe. Flagged or listed = BURNED: out of domains.live() (no new links or
pages on it, existing /r/ codes still resolve), admin told once via telegramAdminChatId or ADMIN_EMAIL.
State in DATA_DIR/domains.json. New: GET /api/my/link-hosts, GET+POST /api/admin/domains, admin pane.
/api/my/pages now hands out hosts from domains.live().
Item 2 (pages.js, formrestyle.js): POST /api/my/pages/upload (raw image body, multipart, or JSON base64) for
png/jpg/webp up to 2 MB, 50 per member, magic bytes checked, sharp re-encode when installed, stored under
DATA_DIR/uploads/pages/<owner-hash>/<id>.<ext>, served at /u/pages/... immutable for a year; delete via
POST /api/my/pages/images/<id> {remove}. Pages editor: Upload image button beside the image field + a strip of
the member's images. Provider allowlist (17 autoresponders, Sendy by /subscribe path) exposed as `providers`;
the editor says "Recognised: AWeber" or "Unknown provider: plain post to <host>" at paste time; pub() carries
formProvider. Unknown hosts still allowed when parse-or-refuse passes.
Item 3 (zonereport.js, pages.summary): /api/my/reports returns {spins, pages, zones} (spins still spread at the
top level) for the chosen days; Pages and Zones sections on /reports with 30-day bars, totals, per-item tables,
phone width checked. Zone numbers are a read-only view over zones.json (zones.js untouched).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -13,11 +13,11 @@ const render = require('./pagerender');
|
||||
const style = require('./pagestyle');
|
||||
const formrestyle = require('./formrestyle');
|
||||
|
||||
let DATA_DIR = null, ownHosts = () => [], keyFn = () => '';
|
||||
let DATA_DIR = null, ownHosts = () => [], keyFn = () => '', imageBase = () => '';
|
||||
const J = { db: null, FILE: () => path.join(DATA_DIR, 'pages.json'),
|
||||
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, pages: [], leads: [], views: [] }; } for (const k of ['pages', 'leads', 'views']) if (!Array.isArray(this.db[k])) this.db[k] = []; },
|
||||
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, pages: [], leads: [], views: [], images: [] }; } for (const k of ['pages', 'leads', 'views', 'images']) if (!Array.isArray(this.db[k])) this.db[k] = []; },
|
||||
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} } };
|
||||
function init(opts) { DATA_DIR = opts.dataDir; ownHosts = opts.ownHosts || (() => []); keyFn = opts.openrouterKey || (() => ''); J.load(); }
|
||||
function init(opts) { DATA_DIR = opts.dataDir; ownHosts = opts.ownHosts || (() => []); keyFn = opts.openrouterKey || (() => ''); imageBase = opts.imageBase || (() => ''); J.load(); }
|
||||
const norm = e => String(e || '').trim().toLowerCase();
|
||||
const TYPES = ['bridge', 'review', 'squeeze'];
|
||||
const cleanUrl = u => { const s = String(u || '').trim(); return /^https?:\/\/[^\s"'<>]+$/i.test(s) && s.length <= 700 ? s : ''; };
|
||||
@@ -28,7 +28,8 @@ const strs = (arr, n, len) => (Array.isArray(arr) ? arr : String(arr || '').spli
|
||||
|
||||
function pub(p) {
|
||||
const views = J.db.views.filter(v => v.p === p.id); const leads = J.db.leads.filter(l => l.p === p.id).length;
|
||||
return { id: p.id, slug: p.slug, type: p.type, title: p.title, status: p.status, design: p.design, content: p.content, scan: p.scan || null, angle: p.angle || '', created: p.created, updated: p.updated, views: views.length, uniques: new Set(views.map(v => v.v)).size, clicks: views.filter(v => v.k === 'cta').length, leads };
|
||||
return { id: p.id, slug: p.slug, type: p.type, title: p.title, status: p.status, design: p.design, content: p.content, scan: p.scan || null, angle: p.angle || '', created: p.created, updated: p.updated, views: views.length, uniques: new Set(views.map(v => v.v)).size, clicks: views.filter(v => v.k === 'cta').length, leads,
|
||||
formProvider: p.type === 'squeeze' && p.content && p.content.formEmbed ? formrestyle.provider(p.content.formEmbed) : null };
|
||||
}
|
||||
async function list(owner) { return J.db.pages.filter(p => p.owner === norm(owner)).sort((a, b) => b.updated - a.updated).map(pub); }
|
||||
async function get(owner, id) { const p = J.db.pages.find(p => p.id === String(id) && p.owner === norm(owner)); return p ? pub(p) : null; }
|
||||
@@ -171,4 +172,85 @@ async function write(owner, id, body) {
|
||||
return { ok: true, page: pub(p), by };
|
||||
}
|
||||
function totals() { return { pages: J.db.pages.length, published: J.db.pages.filter(p => p.status === 'published').length, leads: J.db.leads.length }; }
|
||||
module.exports = { init, TYPES, list, get, bySlug, create, update, html, view, lead, leads, csv, scan, write, totals };
|
||||
// -- Reports (2026-10-10): the member's page views, uniques, clicks-through and leads for the last N days, by Central day and by page
|
||||
const ctDay = ts => new Date(ts).toLocaleDateString('en-CA', { timeZone: 'America/Chicago' });
|
||||
async function summary(owner, days) {
|
||||
const mine = J.db.pages.filter(p => p.owner === norm(owner)); const ids = new Set(mine.map(p => p.id));
|
||||
const since = Date.now() - (Number(days) || 30) * 86400000;
|
||||
const views = J.db.views.filter(v => ids.has(v.p) && v.ts >= since); const leads = J.db.leads.filter(l => l.owner === norm(owner) && l.ts >= since);
|
||||
const shows = views.filter(v => v.k !== 'cta'), clicks = views.filter(v => v.k === 'cta');
|
||||
const dayMap = {}; const dayOf = k => dayMap[k] = dayMap[k] || { day: k, views: 0, u: new Set(), clicks: 0, leads: 0 };
|
||||
for (const v of shows) { const d = dayOf(ctDay(v.ts)); d.views++; d.u.add(v.v); } for (const v of clicks) dayOf(ctDay(v.ts)).clicks++; for (const l of leads) dayOf(ctDay(l.ts)).leads++;
|
||||
const byDay = Object.values(dayMap).sort((a, b) => a.day < b.day ? -1 : 1).map(d => ({ day: d.day, views: d.views, u: d.u.size, clicks: d.clicks, leads: d.leads }));
|
||||
const byPage = mine.map(p => { const vs = shows.filter(v => v.p === p.id); return { id: p.id, title: p.title, slug: p.slug, type: p.type, status: p.status, views: vs.length, uniques: new Set(vs.map(v => v.v)).size, clicks: clicks.filter(v => v.p === p.id).length, leads: leads.filter(l => l.p === p.id).length }; }).sort((a, b) => b.views - a.views);
|
||||
return { pages: mine.length, live: mine.filter(p => p.status === 'published').length, views: shows.length, uniques: new Set(shows.map(v => v.v)).size, clicks: clicks.length, leads: leads.length, byDay, byPage };
|
||||
}
|
||||
// -- hosted images (2026-10-10): png/jpg/webp up to 2 MB, 50 per member, stored under DATA_DIR/uploads/pages/<owner-hash>/<id>.<ext>
|
||||
// and served at /u/pages/<owner-hash>/<id>.<ext>. Re-encoded with sharp when that module is installed (it is not a
|
||||
// dependency today), otherwise magic bytes + size are checked and the file is stored as it came.
|
||||
const IMG_MAX = 50, IMG_BYTES = 2 * 1024 * 1024;
|
||||
const IMG_EXT = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/webp': 'webp' };
|
||||
const ownerHash = o => crypto.createHash('sha256').update(norm(o)).digest('hex').slice(0, 16);
|
||||
const imgDir = o => path.join(DATA_DIR, 'uploads', 'pages', ownerHash(o));
|
||||
let sharp = null; try { sharp = require('sharp'); } catch (e) { sharp = null; }
|
||||
function sniff(buf) {
|
||||
if (buf.length > 16 && buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) return 'image/png';
|
||||
if (buf.length > 16 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'image/jpeg';
|
||||
if (buf.length > 16 && buf.slice(0, 4).toString() === 'RIFF' && buf.slice(8, 12).toString() === 'WEBP') return 'image/webp';
|
||||
return null;
|
||||
}
|
||||
// the request body as { buf, ct }: a raw image body, multipart/form-data (first file part), or JSON { data: <base64 or data: URL> }
|
||||
function readUpload(req) {
|
||||
return new Promise((resolve) => {
|
||||
const ct = String(req.headers['content-type'] || '').toLowerCase(); const chunks = []; let n = 0; let done = false;
|
||||
const fail = msg => { if (!done) { done = true; resolve({ error: msg }); } };
|
||||
req.on('data', c => { n += c.length; if (n > IMG_BYTES * 1.4 + 4096) { req.destroy(); return fail('That image is over 2 MB. Make it smaller and try again.'); } chunks.push(c); });
|
||||
req.on('error', () => fail('Upload interrupted.'));
|
||||
req.on('end', () => {
|
||||
if (done) return; done = true; const raw = Buffer.concat(chunks);
|
||||
try {
|
||||
if (ct.startsWith('multipart/form-data')) {
|
||||
const bm = /boundary=("?)([^";]+)\1/i.exec(ct); if (!bm) return resolve({ error: 'Bad multipart body.' });
|
||||
const boundary = Buffer.from('--' + bm[2]); let pos = raw.indexOf(boundary);
|
||||
while (pos !== -1) {
|
||||
const hs = pos + boundary.length + 2; const he = raw.indexOf('\r\n\r\n', hs); if (he === -1) break;
|
||||
const head = raw.slice(hs, he).toString('latin1'); const next = raw.indexOf(boundary, he + 4); if (next === -1) break;
|
||||
const body = raw.slice(he + 4, next - 2); // strip the CRLF before the next boundary
|
||||
if (/filename=/i.test(head)) { const cm = /content-type:\s*([^\r\n;]+)/i.exec(head); return resolve({ buf: body, ct: cm ? cm[1].trim().toLowerCase() : (sniff(body) || '') }); }
|
||||
pos = next;
|
||||
}
|
||||
return resolve({ error: 'No file in the upload.' });
|
||||
}
|
||||
if (ct.startsWith('application/json')) {
|
||||
const j = JSON.parse(raw.toString('utf8') || '{}'); let data = String(j.data || j.image || ''); let mime = String(j.type || '').toLowerCase();
|
||||
const dm = /^data:([a-z0-9.+\/-]+);base64,/i.exec(data); if (dm) { mime = dm[1].toLowerCase(); data = data.slice(dm[0].length); }
|
||||
const buf = Buffer.from(data.replace(/\s+/g, ''), 'base64'); return resolve({ buf, ct: mime || sniff(buf) || '' });
|
||||
}
|
||||
return resolve({ buf: raw, ct: ct.split(';')[0].trim() });
|
||||
} catch (e) { resolve({ error: 'Could not read the upload.' }); }
|
||||
});
|
||||
});
|
||||
}
|
||||
async function uploadImage(owner, buf, ct) {
|
||||
if (!buf || !buf.length) return { error: 'No image data arrived.' };
|
||||
if (buf.length > IMG_BYTES) return { error: 'That image is over 2 MB. Make it smaller and try again.' };
|
||||
const real = sniff(buf); if (!real || !IMG_EXT[real]) return { error: 'Use a PNG, JPG or WebP image.' };
|
||||
if (ct && IMG_EXT[ct] && ct !== real) return { error: 'That file does not look like a real ' + IMG_EXT[ct].toUpperCase() + '.' };
|
||||
if (J.db.images.filter(i => i.owner === norm(owner)).length >= IMG_MAX) return { error: 'You have ' + IMG_MAX + ' images already. Delete one first.' };
|
||||
let out = buf, mime = real;
|
||||
if (sharp) { try { const img = sharp(buf, { failOn: 'error' }).rotate().resize({ width: 2000, height: 2000, fit: 'inside', withoutEnlargement: true }); out = mime === 'image/png' ? await img.png({ compressionLevel: 9 }).toBuffer() : mime === 'image/webp' ? await img.webp({ quality: 85 }).toBuffer() : await img.jpeg({ quality: 85, mozjpeg: true }).toBuffer(); } catch (e) { return { error: 'That image could not be read.' }; } }
|
||||
const id = crypto.randomBytes(6).toString('hex'); const ext = IMG_EXT[mime]; const dir = imgDir(owner);
|
||||
fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, id + '.' + ext), out);
|
||||
const rec = { id, owner: norm(owner), h: ownerHash(owner), ext, mime, size: out.length, ts: Date.now() };
|
||||
J.db.images.push(rec); J.save(); return { ok: true, image: pubImage(rec) };
|
||||
}
|
||||
function pubImage(i) { return { id: i.id, url: imageBase() + '/u/pages/' + i.h + '/' + i.id + '.' + i.ext, path: '/u/pages/' + i.h + '/' + i.id + '.' + i.ext, size: i.size, ts: i.ts, type: i.mime }; }
|
||||
async function images(owner) { return J.db.images.filter(i => i.owner === norm(owner)).sort((a, b) => b.ts - a.ts).map(pubImage); }
|
||||
async function removeImage(owner, id) {
|
||||
const i = J.db.images.find(x => x.id === String(id) && x.owner === norm(owner)); if (!i) return { error: 'No such image.' };
|
||||
try { fs.unlinkSync(path.join(imgDir(owner), i.id + '.' + i.ext)); } catch (e) {}
|
||||
J.db.images = J.db.images.filter(x => x !== i); J.save(); return { ok: true, images: await images(owner) };
|
||||
}
|
||||
// the file behind /u/pages/<hash>/<id>.<ext>, only when the index knows it (never a raw directory read)
|
||||
function imageFile(hash, id, ext) { const i = J.db.images.find(x => x.h === String(hash) && x.id === String(id) && x.ext === String(ext)); return i ? { file: path.join(DATA_DIR, 'uploads', 'pages', i.h, i.id + '.' + i.ext), mime: i.mime } : null; }
|
||||
module.exports = { init, TYPES, list, get, bySlug, create, update, html, view, lead, leads, csv, scan, write, totals, summary, readUpload, uploadImage, images, removeImage, imageFile, IMG_MAX, IMG_BYTES, providers: () => formrestyle.PROVIDERS, provider: formrestyle.provider };
|
||||
|
||||
Reference in New Issue
Block a user