# Image, Audio, Video & File Fields

> Compound object schemas, constraints, requireAlt defaults, supported MIME types, and entry_media_refs protection for image, audio, video, and file fields.

OvertCMS models media with four dedicated compound object field types: `image`, `audio`, `video`, and `file`. Each field stores a verified `assetId` referencing a `ready` asset in the site's Media Library alongside contextual per-entry metadata (such as `alt`, `caption`, `focalPoint`, or `transcript`).

<Warning>
  Media fields reject unknown properties (`unknown_field`) and verify that every `assetId` exists for the site with `status: "ready"` (`media_asset_not_found`) and matches the expected media kind (`media_kind_mismatch`).
</Warning>

---

## Compound Object Schemas

### `image`

Matches Media Library assets of kind `"image"`.

| Property | Type | Required | Normalization & Rules |
| --- | --- | --- | --- |
| `assetId` | `string` | Yes | Trimmed non-empty media asset ID (`kind: "image"`). |
| `alt` | `string` | Yes on `publish` (by default) | Control characters stripped, whitespace trimmed. Required on publish/schedule when `requireAlt` is `true` (the default for `image` fields); fails with `image_alt_required`. |
| `caption` | `string` | No | Control characters stripped, whitespace trimmed. |
| `title` | `string` | No | Control characters stripped, whitespace trimmed. |
| `focalPoint` | `{ x: number, y: number }` | No | Both `x` and `y` are required finite numbers in `[0, 1]` (no `-0`, no extra keys). Fails with `invalid_focal_point`. |

```json
{
  "assetId": "0195c8b0-1234-7000-8000-0000000000aa",
  "alt": "Diagram showing Delivery, Agent, and Admin plane isolation",
  "caption": "Three-plane deployment topology",
  "title": "Architecture Overview",
  "focalPoint": { "x": 0.5, "y": 0.35 }
}
```

### `audio`

Matches Media Library assets of kind `"audio"`.

| Property | Type | Required | Normalization & Rules |
| --- | --- | --- | --- |
| `assetId` | `string` | Yes | Trimmed non-empty media asset ID (`kind: "audio"`). |
| `title` | `string` | No | Single-line string (control chars stripped, trimmed). |
| `caption` | `string` | No | Single-line string (control chars stripped, trimmed). |
| `transcript` | `string` | No | Multi-line string (preserves `\n` and `\t`, trimmed). |
| `durationMs` | `integer` | No | Non-negative safe integer (`invalid_duration`). Checked against `constraints.maxDurationMs` (`max_duration`). |

```json
{
  "assetId": "0195c8b0-5678-7000-8000-0000000000bb",
  "title": "Episode 12: Schema-First Content",
  "caption": "Interview with the core engineering team",
  "transcript": "Host: Welcome back...\nGuest: Thanks for having me.",
  "durationMs": 1845000
}
```

### `video`

Matches Media Library assets of kind `"video"`.

| Property | Type | Required | Normalization & Rules |
| --- | --- | --- | --- |
| `assetId` | `string` | Yes | Trimmed non-empty media asset ID (`kind: "video"`). |
| `title` | `string` | No | Single-line string (control chars stripped, trimmed). |
| `caption` | `string` | No | Single-line string (control chars stripped, trimmed). |
| `posterAssetId` | `string` | No | Trimmed non-empty media asset ID verified as `kind: "image"` and tracked in `entry_media_refs`. Fails with `invalid_media_asset` if empty/non-string. |
| `durationMs` | `integer` | No | Non-negative safe integer checked against `constraints.maxDurationMs`. |

```json
{
  "assetId": "0195c8b0-9abc-7000-8000-0000000000cc",
  "title": "Product Walkthrough",
  "caption": "2-minute demo of the Admin UI",
  "posterAssetId": "0195c8b0-1234-7000-8000-0000000000aa",
  "durationMs": 124000
}
```

### `file`

Matches Media Library assets of kind `"document"`.

| Property | Type | Required | Normalization & Rules |
| --- | --- | --- | --- |
| `assetId` | `string` | Yes | Trimmed non-empty media asset ID (`kind: "document"`). |
| `title` | `string` | No | Single-line string (control chars stripped, trimmed). |
| `description` | `string` | No | Multi-line string (preserves `\n` and `\t`, trimmed). |

```json
{
  "assetId": "0195c8b0-def0-7000-8000-0000000000dd",
  "title": "2026 Security Whitepaper (PDF)",
  "description": "Architecture details covering RBAC, WebAuthn, and SSRF controls."
}
```

---

## Media Constraints

Constraints are verified against the actual stored `MediaAsset` metadata during entry create, update, publish, schedule, and version restore:

| Constraint Key | Applies To | Default | Validation Error Code | Description |
| --- | --- | --- | --- | --- |
| `requireAlt` | `image` | **`true`** (for `image`) | `image_alt_required` | Enforces non-empty `alt` text on `publish` / `schedule`. Set explicitly to `false` only for decorative images. |
| `allowedMimeTypes` | All 4 | Any allowed MIME for kind | `media_mime_not_allowed` | Array of exact MIME types (e.g., `["image/webp", "image/avif"]`) or wildcard prefixes (e.g., `["image/*"]`). Normalized before comparison. |
| `maxBytes` | All 4 | Global upload limit | `media_max_bytes` | Maximum verified asset size in bytes (`>= 1`). |
| `minWidth` | `image`, `video` | — | `media_min_width` | Minimum asset width in pixels (`>= 0`). |
| `minHeight` | `image`, `video` | — | `media_min_height` | Minimum asset height in pixels (`>= 0`). |
| `aspectRatio` | `image`, `video` | — | `media_aspect_ratio` | Required `"W:H"` ratio string (e.g., `"16:9"`, `"4:3"`, `"1:1"`). Validated against `width / height` within a **±2% tolerance** (`abs(actual - want) <= 0.02 * want`). Assets missing dimensions fail. |
| `maxDurationMs` | `audio`, `video` | — | `max_duration` / `media_max_duration` | Maximum duration in milliseconds (`>= 1`). Checked on both the entry's `durationMs` property and the asset's stored `durationMs`. |

### Example Schema with Constraints

```json
{
  "key": "heroImage",
  "label": "Hero Image",
  "type": "image",
  "required": true,
  "requiredOn": "publish",
  "instruction": "Use a landscape 16:9 hero banner at least 1200x675px with descriptive alt text.",
  "constraints": {
    "requireAlt": true,
    "allowedMimeTypes": ["image/webp", "image/jpeg", "image/png"],
    "maxBytes": 2097152,
    "minWidth": 1200,
    "minHeight": 675,
    "aspectRatio": "16:9"
  }
}
```

---

## Platform MIME Allowlist & Sniffing

OvertCMS never trusts filenames or declared `Content-Type` headers alone. Every direct upload, presigned upload completion, and URL sideload is inspected via magic bytes:

| Media Kind | Allowed Canonical MIME Types |
| --- | --- |
| `image` | `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/avif` |
| `audio` | `audio/mpeg`, `audio/ogg`, `audio/wav`, `audio/mp4`, `audio/webm` |
| `video` | `video/mp4`, `video/webm` |
| `document` (`file` field) | `application/pdf`, `text/csv`, `text/plain` |

<Warning>
  Executables, scripts, HTML documents, and **SVG payloads** are unconditionally rejected by the media sniffer to prevent stored XSS attacks on the Delivery Plane.
</Warning>

---

## `entry_media_refs` Tracking & Deletion Protection

Whenever an entry is created, updated, published, scheduled, or restored, OvertCMS extracts every media asset reference into the normalized `entry_media_refs` table across **both** `draft` and `published` snapshots:

- Top-level `image`, `audio`, `video`, and `file` fields
- `video.posterAssetId` references (tracked as `kind: "image"`)
- Media fields nested inside `object`, `list`, `componentRef`, and `<cms-block data-props>`
- Inline `<img data-asset-id="...">` tags inside `rich_html` fields

### O(1) Deletion Guard (`409 media_in_use`)

When `DELETE /agent/v1/media/{id}` or `DELETE /admin-api/v1/sites/{siteSlug}/media/{id}` is called:

1. OvertCMS executes an atomic `DeleteIfUnreferenced` check against `entry_media_refs`.
2. If any active draft or published snapshot still references the asset, deletion is rejected with **`409 Conflict` (`media_in_use`)** and a `details.referencedBy` list of `{ entryId, field }` entries.
3. Human Admin users may override the guard with `force=true`. **Agents can never force-delete** referenced media assets.
4. Serving media on `GET /delivery/v1/media/{id}` streams only `status: "ready"` assets with `X-Content-Type-Options: nosniff`, a sandbox `Content-Security-Policy`, and `Content-Disposition: attachment` for documents/PDFs/text files.
