Skip to content

gitflarePublicPartly agent-ready50

cf-r2-drop

Drag-and-drop file sharing on Cloudflare R2: multipart uploads, expiring share links, previews, download counts, lifecycle expiry.

main
Serve the share page from the extensionless asset path0e4544f
JavaScript35 lines2.7 KB
import { h, icon, ICONS, copyText, rememberLink } from "./common.js";import { toSvg } from "./qr.js";import { fmtBytes } from "./upload.js";const EXP = [["1h", "1 h"], ["24h", "24 h"], ["7d", "7 d"]], LIM = [["1", "1"], ["5", "5"], ["25", "25"], ["", "Unlimited"]];const TTL = { "1h": 36e5, "24h": 864e5, "7d": 6048e5 };export function shareCard(info) {  let expiry = "24h", limit = "", token = null;  const status = h("p", { class: "mono muted", role: "status", "aria-live": "polite" }, "Creating link…");  const input = h("input", { readonly: true, "aria-label": "Share link", class: "mono" });  const qr = h("div", { class: "qr" });  const copy = h("button", { type: "button", class: "primary", onclick: async () => {    status.textContent = (await copyText(input.value)) ? "Link copied to clipboard." : "Copy failed — select the link and copy manually."; input.select(); } }, icon(ICONS.copy), "\u00a0Copy link");  async function make() {    status.textContent = "Updating link…"; status.classList.remove("err");    try {      const body = { key: info.id, expiry }; if (limit) body.limit = Number(limit);      const r = await fetch("/api/links", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });      if (!r.ok) throw new Error(`HTTP ${r.status}`);      const d = await r.json(); token = d.token; input.value = d.url;      qr.innerHTML = toSvg(d.url, { dark: "#1a1005", light: "#fff" });      rememberLink({ token, fileId: info.id, name: info.name, size: info.size, expiresAt: d.expiresAt ?? Date.now() + TTL[expiry], limit: limit ? Number(limit) : null, createdAt: Date.now() });      status.textContent = "Link ready.";    } catch (e) { status.textContent = `Could not create link (${e.message}). `; status.classList.add("err"); status.append(h("button", { type: "button", onclick: make }, "Retry")); }  }  const seg = h("div", { class: "seg", role: "group", "aria-label": "Expires after" }, EXP.map(([v, l]) =>    h("button", { type: "button", "aria-pressed": String(v === expiry), onclick: (e) => { expiry = v; seg.querySelectorAll("button").forEach((b) => b.setAttribute("aria-pressed", String(b === e.currentTarget))); make(); } }, l)));  const sel = h("select", { "aria-label": "Download limit", onchange: (e) => { limit = e.target.value; make(); } }, LIM.map(([v, l]) => h("option", { value: v }, l)));  make();  return h("section", { class: "card", "aria-label": `Share ${info.name}` },    h("div", { class: "head" }, h("h2", { class: "name" }, info.name), h("span", { class: "mono muted" }, fmtBytes(info.size))),    h("div", { class: "linkrow" }, input, copy),    h("div", { class: "actions" }, seg, h("label", { class: "mono muted" }, "Downloads\u00a0", sel)),    qr, status);}