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
0e4544fArchitecture
Signed tokens
A share token is base64url(objectKey.expiresAt.limit) plus an HMAC-SHA256 signature made with LINK_SECRET through Web Crypto. Every request verifies the signature, so a tampered token gets 403. A token past expiresAt gets 410. The signature comparison is constant-time. The code is in src/lib/token.ts.
The link id used by the Durable Object is derived from the token. The token never stores counts, because counts change after signing.
Multipart flow
POST /api/uploadsdeclares name, size and type. Small files get an id. Large files (10 MB or more) startcreateMultipartUploadand return the uploadId, the key and the 8 MiB part size.- The browser sends
PUT /api/uploads/:id/parts/:nfor each part, 4 in parallel. Each failed part is retried 3 times with backoff. Pause stops scheduling new parts. Resume continues with the missing ones. POST /api/uploads/:id/completesends the part etags, and R2 assembles the object.DELETE /api/uploads/:idaborts the upload (cancel).
Objects are written with customMetadata.expiresAt. Part planning is in src/lib/parts.ts, and size limits are in src/lib/limits.ts.
Range downloads
/d/<token> parses Range (src/lib/range.ts) and passes it to R2. It answers 206 with Content-Range, or 416 if the range is unsatisfiable. It sends ETag and Accept-Ranges: bytes, and streams object.body. ?inline=1 uses Content-Disposition: inline for previews. Otherwise it uses attachment.
Links Durable Object
One SQLite-backed Links object per link id. increment() runs atomically inside the object. It refuses when the link is revoked or the limit is reached, before any bytes stream. Metadata reads do not count as downloads.
Lifecycle
- Expiry is checked on every request from the signed token.
- A daily cron (
0 3 * * *) lists objects and deletes those whosecustomMetadata.expiresAthas passed. - The R2 lifecycle rule (
scripts/lifecycle.json) deletes after 7 days and aborts incomplete multipart uploads after 1 day, as a backstop.
See docs/API.md for the HTTP contract.