Media upload¶
How clients (cms-admin, forge, Postman) upload images and other media into the CMS, and where to store the resulting path.
There is one upload API. Favicon, OG image, author image, and article images all use it — they only differ in which DB field you write afterward.
Flow¶
1. POST /admin/v1/media (admin JWT)
2. PUT <upload_url> (raw bytes to S3, matching Content-Type)
3. Store media_path in the CMS (settings / content / author field)
The browser/client never holds long-lived S3 write keys. cms-api issues a
short-lived presigned PUT URL; the cms-cf-media worker later serves the object
at the stored /cmp-media/... path.
Endpoint¶
POST /admin/v1/media — requires an admin (or forge) JWT, same as the rest of
/admin/v1.
Request body¶
| Field | Type | Notes |
|---|---|---|
filename |
string, optional | Used to pick a file extension when present. |
content_type |
string, optional | MIME type; should match the PUT. |
site_id |
int, optional | When set (and scope/common are not), the object key is placed under a domain slug folder (e.g. wantskicks, fashionradar) so media is shared across that site's env hosts. |
site_ref |
string, optional | Same derivation as site_id, by site ref (id / acronym / domain). |
scope |
string, optional | Explicit S3 folder prefix ([a-z0-9-]{1,64}). Overrides site_id derivation. |
common |
bool, default false |
When true, stores under common/ (shared assets). |
content_hash |
string, optional | sha256 hex digest (^[0-9a-f]{64}$) of the bytes about to be PUT. Makes the key content-addressed so a re-upload overwrites instead of creating a new object (AISD-256). |
kind |
string, optional | logo / favicon resolve to the fixed branding key the worker reads by convention. Any other value is ignored. |
variant_width |
int, optional | Responsive-variant width. Requires content_hash; must be one of 240, 480, 640, 768. |
Allowed extensions include .png, .jpg, .jpeg, .gif, .webp, .svg,
.avif, .ico, .mp4, .webm, .pdf.
Key shapes¶
| Request | Object key |
|---|---|
| plain | <scope>/<uuid4><ext> |
content_hash |
<scope>/<hash><ext> |
content_hash + variant_width |
<scope>/<hash>-w<width><ext> |
kind=logo / kind=favicon |
<scope>/logo.webp / <scope>/favicon.ico |
kind takes precedence over content_hash: a content-addressed logo would be
invisible to the fixed-path convention the worker resolves branding by (AISD-198).
For a variant the hash is the original's digest, so a variant sits beside its
source and both are overwritten in place on a re-import.
422s raised before any S3 call¶
| Code | Cause |
|---|---|
invalid_media_extension |
extension outside the allowlist |
invalid_content_hash |
content_hash is not a 64-char lowercase sha256 hex digest |
variant_requires_content_hash |
variant_width sent without content_hash |
invalid_variant_width |
width outside {240, 480, 640, 768} |
invalid_branding_content_type |
kind=logo/favicon with a non-image content_type |
The variant allowlist is one of four lists that must agree
The width is part of the object key on every side of the exchange:
cms-ai-forge writes the renditions (SRCSET_WIDTHS), the backfill script
writes them for already-imported media
(hq/scripts/generate_media_variants.py VARIANT_WIDTHS), the CMS worker
names the srcset candidates (cms-cf-worker/src/layout/index.ts
VARIANT_WIDTHS), and this presign gate decides which keys may be signed at
all. A width missing here is not a saving not taken -- it is a hard
failure: both the 640 rung and later the 240 rung (AISD-284) reached the
other three and not this gate, and every forge image task 422'd until it was
added. Adding a rung means editing four files.
Response¶
| Field | Example | Meaning |
|---|---|---|
upload_url |
https://s3…/…?X-Amz-… |
Short-lived S3 PUT URL (~15 min). |
object_key |
fashionradar/<uuid>.png |
Key inside the media bucket. |
media_path |
/cmp-media/fashionradar/<uuid>.png |
Value to store in the CMS. |
expires_in |
900 |
Seconds until upload_url expires. |
Example¶
# 1) Presign (admin token from /admin/v1/auth/login)
curl -sS -X POST "$BASE/admin/v1/media" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"filename":"hero.png","content_type":"image/png","site_id":207}'
# 2) Upload bytes to upload_url
curl -sS -X PUT "$UPLOAD_URL" \
-H "Content-Type: image/png" \
--data-binary @hero.png
# 3) Persist media_path on the relevant resource (examples below)
OpenAPI: openapi-admin.yaml. Postman: Media folder in
the API collection.
Where to store media_path¶
Same upload for every asset type — only the PATCH/POST target changes.
| Asset | Write via | Field(s) |
|---|---|---|
| Site favicon / logo | PATCH /admin/v1/sites/{site_id}/settings |
default_favicon, icon_url |
| Default OG image | same settings PATCH | default_og_image |
| Article featured / OG | POST / PATCH /admin/v1/content |
featured_image, og_image |
| Author photo | POST / PATCH /admin/v1/sites/{site_id}/authors |
image |
Public v2 and the Cloudflare worker absolutize these paths with the site's
cdn_base_url (see content-type taxonomy — /cmp-media/ contract).
Do not store the temporary upload_url or a raw S3 URL in the DB.
Serving¶
The cms-cf-media worker maps:
So a stored path /cmp-media/fashionradar/abc.png is fetchable from the site's
media CDN host as that same path.
Deduplication (content-hash)¶
Caller-supplied, opt-in. cms-api never sees the bytes -- it signs a PUT the
client performs directly against S3 -- so only the caller can hash them. Send
content_hash and identical bytes always resolve to the same key, which turns a
re-upload into an overwrite and keeps the stored media_path stable across
repeated imports (AISD-256). Omit it and behaviour is the original one: a random
<scope>/<uuid4>.<ext> key per call, which is the right property for a genuinely
new asset.
Related¶
- Implementation:
src/cms_api/admin/routes/media.py,src/cms_api/admin/media.py - Spec: AISD-149 (presign), AISD-115 (
/cmp-media/worker contract) - Path contract details: content-type-taxonomy.md