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
JavaScript41 lines2.8 KB
import { h, icon, ICONS, header, $ } from "./common.js";import { fmtBytes, fmtCountdown } from "./upload.js";$("#hdr").innerHTML = header("");const token = location.pathname.split("/").filter(Boolean).pop();const view = $("#view"), title = $("#title"), main = $("#main");const dl = `/d/${encodeURIComponent(token)}`;function gone(code, status) {  main.removeAttribute("aria-busy");  const revoked = code === "revoked", limit = code === "limit_reached" || code === "exhausted", missing = status === 404 || status === 403;  title.textContent = revoked ? "This link was revoked" : limit ? "Download limit reached" : missing ? "Link not found" : "This link has expired";  view.replaceChildren(h("div", { class: "empty" }, h("p", {}, revoked ? "The sender turned this link off." : limit ? "It has been downloaded as many times as allowed." : missing ? "The address is wrong or has been tampered with." : "Shared links disappear on schedule."), h("a", { class: "btn", href: "/" }, "Send your own file")));}function failed(msg) {  main.removeAttribute("aria-busy"); title.textContent = "Could not load this link";  view.replaceChildren(h("div", { class: "empty err" }, h("p", {}, msg), h("button", { type: "button", onclick: () => location.reload() }, icon(ICONS.retry), "\u00a0Try again")));}function show(m) {  main.removeAttribute("aria-busy"); title.textContent = m.name;  const left = h("dd", {}), time = h("dd", {});  const tick = () => { const ms = m.expiresAt - Date.now(); time.textContent = fmtCountdown(ms); if (ms <= 0) { clearInterval(iv); gone("expired", 410); } };  const iv = setInterval(tick, 1000); tick();  const rem = m.remaining ?? m.remainingDownloads ?? null;  left.textContent = rem == null || rem < 0 ? "unlimited" : String(rem);  const type = m.type || "application/octet-stream", inline = `${dl}?inline=1`;  let pv = null;  if (type.startsWith("image/")) pv = h("img", { class: "preview", src: inline, alt: `Preview of ${m.name}` });  else if (type === "application/pdf") pv = h("embed", { class: "preview", src: inline, type, title: `Preview of ${m.name}` });  view.replaceChildren(h("dl", { class: "meta card" }, ...[["Size", fmtBytes(m.size)], ["Type", type]].map(([k, v]) => h("div", {}, h("dt", {}, k), h("dd", {}, v))),    h("div", {}, h("dt", {}, "Expires in"), time), h("div", {}, h("dt", {}, "Downloads left"), left)),    pv, h("div", { class: "actions" }, h("a", { class: "btn primary", href: dl, download: m.name }, icon(ICONS.dl), "\u00a0Download")));}(async () => {  try {    const r = await fetch(`/api/links/${encodeURIComponent(token)}/meta`);    if (r.ok) return show(await r.json());    const e = await r.json().catch(() => ({}));    if ([403, 404, 410].includes(r.status)) return gone(e?.error?.code, r.status);    failed(`The server answered ${r.status}.`);  } catch { failed("Network error. Check your connection."); }})();