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
TypeScript26 lines1.2 KB
import { DurableObject } from "cloudflare:workers"; export class Links extends DurableObject {  constructor(ctx: DurableObjectState, env: object) {    super(ctx, env);    ctx.storage.sql.exec("CREATE TABLE IF NOT EXISTS s (k TEXT PRIMARY KEY, v INTEGER)");  }  private get(k: string): number {    const r = this.ctx.storage.sql.exec("SELECT v FROM s WHERE k = ?", k).toArray();    return r.length ? Number(r[0].v) : 0;  }  private set(k: string, v: number) {    this.ctx.storage.sql.exec("INSERT INTO s (k, v) VALUES (?, ?) ON CONFLICT(k) DO UPDATE SET v = excluded.v", k, v);  }  /** Atomic: DO methods run single-threaded without awaits between read and write. */  increment(limit: number): { ok: boolean; reason?: "revoked" | "limit"; downloads: number } {    const downloads = this.get("downloads");    if (this.get("revoked")) return { ok: false, reason: "revoked", downloads };    if (limit > 0 && downloads >= limit) return { ok: false, reason: "limit", downloads };    this.set("downloads", downloads + 1);    return { ok: true, downloads: downloads + 1 };  }  status() { return { downloads: this.get("downloads"), revoked: this.get("revoked") === 1 }; }  revoke() { this.set("revoked", 1); }}