Developer API — Image Upload (Twitter-style media)¶
Date: 2026-06-22 Status: Design approved, pending implementation plan
Goal¶
Let developers upload images through the public /api/v1/* developer API and attach
them to posts, modeled on Twitter/X’s media flow: upload returns an opaque
media_id, and the post-create call references that id. Existing posts (which
already store image URLs) must keep working with zero migration.
Background — what already exists¶
Developer API at
app/routes/api/v1/*(posts,feed,me), authenticated by API key viawithDeveloperApi(lib/api/with-developer-api.server.ts) with per-tier rate limits.POST /api/v1/postscurrently creates a text-only post.In-app image upload at
app/routes/api/rmharks/image.ts— multipart, session auth, validates magic bytes, stores viaputObject, returns/api/feed/image/<filename>URLs.Storage
lib/storage/s3.server.ts— S3-or-local-filesystem fallback;lib/storage/keys.ts(feedImageKey,feedImageUrl, key/format helpers);lib/slice-it/upload-validation.ts(validateImageBuffer,detectImageExt);lib/image-optimize.ts(Sharp, available but unused by the in-app route).RMHarkpost model already hasimageUrls String[]— posts can carry images; the v1 endpoint just doesn’t accept them yet.Posts are soft-deleted (
app/routes/api/rmharks/$id.tssetsdeletedAt).No cron framework. Cleanup idioms in the repo: lazy-on-access (
lib/discord-avatar-refresh.server.ts) andsetIntervalin long-running workers (server/recap,server/doctrine-worker).
Core decisions¶
Opaque
media_id, not a URL. Upload returns{ "id": "media_abc123" }. The developer never holds a raw origin URL, which keeps storage/serving swappable (CDN, signed URLs) without breaking clients.media_idis an API-surface abstraction only. Under the hood, posts still store resolved URLs inRMHark.imageUrls, identical in format to the in-app uploader. This is the backward-compat keystone — see below.Single-use media. One
media_idattaches to exactly one post; orphans and deleted-post media are reclaimed by a reconciling sweep.
Backward compatibility (existing posts never break)¶
Existing posts hold
/api/feed/image/<filename>URLs inimageUrls; untouched, and the/api/feed/image/...serving route stays. Zero migration.New API posts end up byte-identical in storage — a resolved URL in
imageUrls. The feed renderer never sees amedia_id; it only ever reads URLs.The
Mediatable is purely additive — it sits in front of upload only.
API surface¶
POST /api/v1/images¶
Auth:
withDeveloperApi(API key), per-tier rate limit + a tighter upload limit.Body:
multipart/form-data, singleimagefield (one media → one id).Validate magic bytes (
validateImageBuffer/detectImageExt); enforce size and format limits (below). Never trust the declared content-type.Store via
putObject(feedImageKey(filename), …)with the existing unique filename scheme<userId>-<ts>-<rand>.<ext>.Write a
Mediarow (status: PENDING), return:{ "id": "media_abc123", "type": "image", "expires_at": "2026-06-23T12:00:00.000Z" }
No URL in the response — the id is the handle.
POST /api/v1/posts (extended)¶
{ "content": "hello", "media_ids": ["media_abc123"], "audience": "PUBLIC" }
media_idsoptional, max 4.contentbecomes optional whenmedia_idsis present (image-only posts), else still required.For each id: verify ownership (
userIdmatches the API key’s user) andstatus === PENDING; otherwise400(invalid_media/ not found / already used).Resolve each id → its
url, flipstatus: ATTACHED, setpostId/attachedAt, and write the resolved URLs intoRMHark.imageUrls. Create the post.Preserve existing behavior:
awardXp,progressQuests.
Data model¶
model Media {
id String @id // "media_" + cuid
userId String // owner (from API key)
key String // storage key, e.g. rmharks/<filename>
url String // resolved serving URL written into imageUrls
contentType String
bytes Int
status MediaStatus @default(PENDING)
postId String? // set when ATTACHED
createdAt DateTime @default(now())
attachedAt DateTime?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
post RMHark? @relation(fields: [postId], references: [id], onDelete: SetNull)
@@index([status, createdAt]) // orphan sweep
@@index([postId]) // deleted-post sweep
@@index([userId])
}
enum MediaStatus { PENDING ATTACHED }
postId/onDelete: SetNull is a convenience link; the sweep keys off
post.deletedAt + grace period, not the FK. Confirm the back-relation fields on
User and RMHark during implementation.
Cleanup — reconciling sweep¶
A single periodic job, sweepUnreferencedMedia() in lib/, invoked hourly via
setInterval from an existing long-running worker (logic in lib/, testable in
isolation; worker is just the trigger). Handles all cases:
Orphaned uploads —
status = PENDINGandcreatedAt < now − 24h.Deleted-post media —
Mediawhosepost.deletedAtis set more than 7 days ago (grace period gives undelete headroom and lets CDN purges batch).Defensive backstop —
ATTACHEDmedia whose post row is gone.
Per hit: deleteObject(key) → purgeFromCdn(key) → delete the Media row.
Rationale for a sweep over hooking the delete handler: multiple delete paths (user, admin, orphan expiry), soft-delete isn’t a single hard event, and a reconciling job is self-healing if any path forgets.
CDN compatibility (future)¶
Deleting the S3 origin object does not evict CDN edge caches, so cleanup needs an explicit purge:
purgeFromCdn(key)inlib/storage/, sibling todeleteObject. No-op when no CDN is configured; calls the CDN purge API when it is. The sweep stays CDN-agnostic. Adding/swapping a CDN later touches one file.Tag objects with a surrogate/cache key =
media_id(and/or owner) at upload, so cleanup purges by tag in one call (Cloudflare cache-tags / Fastly surrogate-keys / Bunny) — cheaper than per-URL CloudFront invalidations.Moderate
Cache-Control(notimmutable) on deletable media so stale copies age out even if a purge call fails. Purge is the fast path; TTL is the safety net.Unique filenames (
<userId>-<ts>-<rand>.<ext>) mean objects are never overwritten — no “stale updated image” problem, only “deleted image still cached,” solved by purge + TTL. No cache-busting query strings needed.
Limits & validation (mirror the in-app uploader)¶
≤ 4 images per post (
MAX_IMAGES), enforced onmedia_ids.length.≤ 5 MB per image (
FEED_IMAGE_MAX_BYTES).Formats: png / jpg / webp / gif, verified by magic bytes.
Stored as-is (no Sharp re-encode), matching the in-app route.
image-optimizeremains available for future thumbnails.Reuse
feedImageKey/feedImageUrl; resolved URLs are byte-identical to in-app uploads and served by the existing/api/feed/image/<filename>route.
Abuse & rate limiting¶
Uploads are far more expensive than the existing JSON endpoints — the route buffers
the whole file into memory (Buffer.from(await file.arrayBuffer())), so the generic
per-key request budget (120/min starter, 600/min pro) is not sufficient: 120 × 5
MB = 600 MB/min of ingress per key, and a fixed-window counter lets a client burst the
whole budget instantly. Layered controls, specific to the upload route:
Tier gate — a dedicated
hasApiImageUpload(tier)capability inlib/entitlements.ts(mirrorshasApiAccess), granting starter and above (freealready has no API access). Separate fromhasApiAccessso uploads can be scoped or disabled independently of the rest of the API. Reject with403feature_not_available.Size guard before buffering — reject when
Content-Length > 5 MBwith 413 beforerequest.formData()reads the body into memory. A reverse-proxyclient_max_body_size 5mis the real backstop (app checks can be bypassed if the proxy fully buffers first) — documented, not enforced in app code.Dedicated tight per-key limit — the upload route uses its own limiter at 15 req/min per key, far below the generic 120, reusing the existing
rateLimit/redisRateLimit(fixed-window). This is what caps the acute burst. A token/leaky bucket is a noted future upgrade — not built now (YAGNI).Tier-scaled daily quota — per-user cap on uploads per rolling 24h: starter 200, pro 1 000, enterprise 5 000. A Redis counter keyed
media-quota:<userId>:<dayBucket>with a 24h TTL; when Redis is absent it degrades to an in-process counter (best-effort, per-instance) — acceptable because the dedicated 15/min limit already bounds single-instance damage. Reject with429quota_exceeded.
Considered and dropped (YAGNI): a full token-bucket limiter, a concurrent pending-media cap, and an in-flight concurrency semaphore. The 15/min limit + daily quota + size guard cover the stated threat (a targeted flood OOMing the server); revisit if upload abuse is observed in practice.
Out of scope¶
Chunked upload (Twitter’s INIT/APPEND/FINALIZE) — single-shot only; 5 MB cap.
Video/audio media — images only.
Server-side image transforms/thumbnails (infra exists; not wired here).
Reusable media handles / reference counting (single-use by design).
A real cron/k8s CronJob — the sweep function is cron-ready, but it’s triggered by a worker
setIntervalfor now (see infra-direction memory for the eventual k3s path).
Testing¶
Upload: valid image →
PENDINGMediarow + stored object +media_id; oversize body (Content-Length) → 413; wrong magic bytes → 400; missing field → 400; per-key rate limit → 429; bad/missing API key → 401/403.Abuse controls:
free/non-entitled tier → 403feature_not_available; 16th upload within a minute → 429; daily quota exceeded → 429quota_exceeded; daily quota scales by tier.Attach: valid
media_ids→ post with resolvedimageUrls, mediaATTACHED; foreign-owner id → 400; already-ATTACHEDid → 400; > 4 ids → 400; image-only post (nocontent) → 201.Backward compat: an existing post with raw
imageUrlsstill renders; feed output for an API post is identical in shape to an in-app upload.Sweep: orphaned PENDING > 24h removed (row + object + purge called); deleted-post media removed only after 7-day grace; live post’s media untouched.
CDN abstraction:
purgeFromCdnis a no-op without config and is invoked once per deleted object during a sweep.