1import { createHash } from "node:crypto";2import { mkdirSync, rmSync, writeFileSync } from "node:fs";3import { join } from "node:path";4import type { Context, Hono } from "hono";5import { html } from "../pages/html.ts";6import { setting } from "../server/db.ts";7import { type AdminContext, adminPage, formField, notice, textFields } from "./console.ts";8import { saveSettings } from "./store.ts";91011const FIELDS: [key: string, label: string, max: number][] = [12 ["site_name", "site name", 100],13 ["tagline", "tagline", 300],14 ["branding", "branding text", 100],15 ["handle", "handle", 39],16 ["bio", "bio", 2000],17 ["location", "location", 100],18 ["link", "link", 500],19 ["building_since", "building since", 100],20];21const HANDLE = /^[A-Za-z0-9._-]+$/;22const HTTP_URL = /^https?:\/\/[^\s]+$/i;23const OWN_SIGNATURE = /^signature-[0-9a-f]{16}\.(png|jpg|webp)$/;242526272829function imageType(bytes: Uint8Array): "png" | "jpg" | "webp" | null {30 const starts = (sig: number[], at = 0) => sig.every((byte, i) => bytes[at + i] === byte);31 if (starts([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "png";32 if (starts([0xff, 0xd8, 0xff])) return "jpg";33 if (starts([0x52, 0x49, 0x46, 0x46]) && starts([0x57, 0x45, 0x42, 0x50], 8)) return "webp";34 return null;35}363738export function registerSite(app: Hono, ctx: AdminContext): void {39 const assets = join(ctx.dataDir, "assets");40 app.get("/admin/site", (c) => c.html(sitePage(c, ctx, "")));4142 app.post("/admin/site", async (c) => {43 const fields = await textFields(c);44 const refuse = (error: string) => c.html(sitePage(c, ctx, error), 400);45 const values: Record<string, string> = {};46 for (const [key, label, max] of FIELDS) {47 values[key] = (fields[key] ?? "").trim();48 if (values[key].length > max) return refuse(`The ${label} is over ${max} characters.`);49 }50 if (values.handle && !HANDLE.test(values.handle)) {51 return refuse("A handle is letters, digits, '.', '_' and '-'.");52 }53 if (values.link && !HTTP_URL.test(values.link)) {54 return refuse("The link must be an http or https address.");55 }56 const upload = (await c.req.parseBody()).signature;57 const previous = setting(ctx.kepr, "signature");58 if (upload instanceof File && upload.size > 0) {59 const bytes = new Uint8Array(await upload.arrayBuffer());60 const type = imageType(bytes);61 if (!type) return refuse("The signature must be a PNG, JPEG or WebP image.");62 63 const hash = createHash("sha256").update(bytes).digest("hex").slice(0, 16);64 values.signature = `signature-${hash}.${type}`;65 mkdirSync(assets, { recursive: true });66 writeFileSync(join(assets, values.signature), bytes);67 } else if (fields.remove_signature === "1") {68 values.signature = "";69 }70 saveSettings(ctx.kepr, values);71 72 if (73 previous &&74 OWN_SIGNATURE.test(previous) &&75 values.signature !== undefined &&76 previous !== values.signature77 ) {78 rmSync(join(assets, previous), { force: true });79 }80 return c.redirect("/admin/site", 303);81 });82}8384function sitePage(c: Context, ctx: AdminContext, error: string): string {85 const signature = setting(ctx.kepr, "signature");86 const input = ([key, label, max]: (typeof FIELDS)[number]) =>87 html`<label class="issues-label" for="${key}">${label}</label>88${89 key === "bio"90 ? html`<textarea class="issues-textarea" id="${key}" name="${key}" rows="4" maxlength="${max}">${setting(ctx.kepr, key) ?? ""}</textarea>`91 : html`<input class="issues-input" id="${key}" name="${key}" maxlength="${max}" value="${setting(ctx.kepr, key) ?? ""}">`92}`;93 const body = html`${notice(error)}94<form class="issues-form" method="post" action="/admin/site" enctype="multipart/form-data">95${formField(c)}96${FIELDS.map(input)}97<label class="issues-label" for="signature">signature image <span class="muted">(PNG, JPEG or WebP, under 60 KB)</span></label>98${signature && html`<img class="profile-signature" src="/static/assets/${encodeURIComponent(signature)}" alt="current signature">`}99<input class="issues-input" id="signature" name="signature" type="file" accept="image/png,image/jpeg,image/webp">100${signature && html`<label class="issues-label"><input type="checkbox" name="remove_signature" value="1"> remove the signature</label>`}101<button class="issues-submit" type="submit">save</button>102</form>`;103 return adminPage(c, ctx, "site and profile", body);104}