gitflarePublicPartly agent-ready50
cf-r2-drop
Drag-and-drop file sharing on Cloudflare R2: multipart uploads, expiring share links, previews, download counts, lifecycle expiry.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
0e4544fOct 20, 2026
Serve the share page from the extensionless asset path
0e4544fimport { h, icon, ICONS, header, $ } from "./common.js";import { Uploader, fmtBytes, fmtEta } from "./upload.js";import { shareCard } from "./share-card.js";const MAX = 5 * 1024 * 1024 * 1024;$("#hdr").innerHTML = header("/");const zone = $("#zone"), input = $("#file"), list = $("#files"), shares = $("#shares"), live = $("#live");const say = (m) => { live.textContent = m; };function ib(path, label, fn, cls = "") { return h("button", { type: "button", class: cls, onclick: fn }, icon(path), "\u00a0", label); }function addFile(file) { if (file.size === 0) return say(`${file.name} is empty and was skipped.`); if (file.size > MAX) return say(`${file.name} is over the 5 GB limit.`); const bar = h("i"), stat = h("span", { class: "mono muted" }, "Queued"), acts = h("div", { class: "actions" }); const pb = h("div", { class: "bar", role: "progressbar", "aria-label": `Upload ${file.name}`, "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": 0 }, bar); const row = h("li", { class: "row" }, h("div", { class: "head" }, h("span", { class: "name" }, file.name), h("span", { class: "mono muted" }, fmtBytes(file.size))), pb, stat, acts); const up = new Uploader(file, { onProgress: (p) => { const pct = Math.floor((p.loaded / p.total) * 100); bar.style.width = pct + "%"; pb.setAttribute("aria-valuenow", pct); stat.classList.toggle("err", p.state === "error"); stat.textContent = p.state === "uploading" ? `${pct}% · ${fmtBytes(p.speed)}/s · ${fmtEta(p.eta)} left` : p.state === "paused" ? `Paused at ${pct}%` : p.state === "error" ? "Upload failed" : p.state === "canceled" ? "Canceled" : p.state === "done" ? "Uploaded" : stat.textContent; render(p.state); } }); function render(s) { acts.replaceChildren(); if (s === "uploading") acts.append(ib(ICONS.pause, "Pause", () => up.pause())); if (s === "paused") acts.append(ib(ICONS.play, "Resume", start)); if (s === "error") acts.append(ib(ICONS.retry, "Retry", start, "primary")); if (["uploading", "paused", "error", "queued"].includes(s)) acts.append(ib(ICONS.x, "Cancel", async () => { await up.cancel(); setTimeout(() => row.remove(), 800); }, "danger")); } async function start() { try { const info = await up.run(); if (info) { say(`${file.name} uploaded.`); row.remove(); shares.prepend(shareCard(info)); } } catch { say(`${file.name} failed to upload.`); } } list.append(row); render("queued"); start();}const addAll = (fs) => [...fs].forEach(addFile);input.addEventListener("change", () => { addAll(input.files); input.value = ""; });zone.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); input.click(); } });for (const t of ["dragenter", "dragover"]) zone.addEventListener(t, (e) => { e.preventDefault(); zone.classList.add("over"); });for (const t of ["dragleave", "drop"]) zone.addEventListener(t, () => zone.classList.remove("over"));zone.addEventListener("drop", (e) => { e.preventDefault(); addAll(e.dataTransfer.files); });document.addEventListener("paste", (e) => { if (e.clipboardData?.files.length) addAll(e.clipboardData.files); });