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.

Serve the share page from the extensionless asset path0e4544f
JavaScript40 lines2.8 KB
import { h, icon, ICONS, header, $, loadLinks, forgetLink, copyText } from "./common.js";import { fmtBytes, fmtCountdown } from "./upload.js";$("#hdr").innerHTML = header("my");const list = $("#list"), live = $("#live");function empty() { list.replaceChildren(h("li", { class: "empty" }, h("p", {}, "No links yet."), h("a", { class: "btn primary", href: "/" }, "Send a file"))); }function item(l) {  const counts = h("span", { class: "mono muted" }, "Loading counts…"), state = h("span", { class: "mono" });  const url = `${location.origin}/d/${l.token}`, page = `/s/${l.token}`;  const refresh = async () => {    const left = l.expiresAt - Date.now();    try {      const r = await fetch(`/api/links/${encodeURIComponent(l.token)}`);      if (r.status === 404 || r.status === 410) { state.textContent = "Revoked or gone"; state.className = "mono err"; counts.textContent = ""; return; }      if (!r.ok) throw 0;      const d = await r.json(), n = d.count ?? d.downloads ?? 0, lim = d.limit ?? l.limit;      counts.textContent = `${n} download${n === 1 ? "" : "s"}${lim ? ` of ${lim}` : ""}`;      if (d.revoked) { state.textContent = "Revoked"; state.className = "mono err"; return; }    } catch { counts.textContent = "Counts unavailable"; }    state.textContent = left > 0 ? `Expires in ${fmtCountdown(left)}` : "Expired"; state.className = "mono " + (left > 0 ? "ok" : "err");  };  const row = h("li", { class: "card" }, h("div", { class: "head" }, h("a", { class: "name", href: page }, l.name), h("span", { class: "mono muted" }, fmtBytes(l.size))),    h("div", { class: "head" }, state, counts),    h("div", { class: "actions" },      h("button", { type: "button", onclick: async () => { live.textContent = (await copyText(url)) ? "Link copied." : "Copy failed."; } }, icon(ICONS.copy), "\u00a0Copy"),      h("button", { type: "button", class: "danger", onclick: (e) => confirmRevoke(e.currentTarget) }, icon(ICONS.x), "\u00a0Revoke")));  function confirmRevoke(btn) {    const acts = btn.parentElement, yes = h("button", { type: "button", class: "danger primary" }, "Confirm revoke"), no = h("button", { type: "button" }, "Keep");    btn.hidden = true; acts.append(yes, no); yes.focus();    no.onclick = () => { yes.remove(); no.remove(); btn.hidden = false; btn.focus(); };    yes.onclick = async () => {      const r = await fetch(`/api/links/${encodeURIComponent(l.token)}`, { method: "DELETE" }).catch(() => null);      if (r && (r.ok || r.status === 404 || r.status === 410)) { forgetLink(l.token); row.remove(); live.textContent = `Revoked ${l.name}.`; if (!list.children.length) empty(); }      else { live.textContent = "Revoke failed. Try again."; no.onclick(); }    };  }  refresh(); row.dataset.t = setInterval(refresh, 15000); return row;}const links = loadLinks();if (!links.length) empty(); else list.replaceChildren(...links.map(item));