# Media

Photos and videos are added to a brand first, then used in posts by id (`media_ids`). There are two ways to add a file.
Both need `rights_confirmed: true`, which says the file is yours or you may publish it; without it the answer is 422
`rights_not_confirmed`.

Accepted files: JPEG, PNG and WebP images up to 30 MB, and MP4 or MOV videos up to 1,024 MB and 60 minutes. HEIC photos
are not supported yet. The platform pages list each platform's own limits, and a file is checked against the platform
when you validate or create a post. Storage counts toward the workspace's plan.

## Upload with a signed URL

1. Announce the file. `size` is the exact number of bytes you will send.

   ```bash
   curl https://app.postaki.io/api/v1/media/uploads \
     -H "Authorization: Bearer $POSTAKI_KEY" -H "Content-Type: application/json" \
     -H "Idempotency-Key: $(uuidgen)" \
     -d '{"brand_id": 12, "filename": "bread.jpg", "content_type": "image/jpeg", "size": 482113, "rights_confirmed": true}'
   ```

   The answer has `upload_id` (the media id), `upload_url` and `expires_at`.

2. `PUT` the raw bytes to `upload_url`. No `Authorization` header: the URL itself is the credential. It works once and for
   one hour.

   ```bash
   curl -X PUT --data-binary @bread.jpg "<upload_url>"
   ```

3. Say it is done. Processing starts.

   ```bash
   curl -X POST https://app.postaki.io/api/v1/media/uploads/<upload_id>/complete \
     -H "Authorization: Bearer $POSTAKI_KEY" -H "Idempotency-Key: $(uuidgen)"
   ```

4. Poll `GET /media/{id}` until `status` is `ready`, then use the id in `media_ids`.

Media that is still arriving or being processed holds a place until it is `ready` or `failed`: a workspace can have at
most 5 such items at a time, and the size announced for an upload is set aside against the storage allowance until the
item finishes, fails or its URL expires (403 `limit_reached` otherwise; wait for the others to finish, or let unused
upload URLs expire after an hour). A download from a URL is given up after 10 minutes.

An expired URL is 410 `upload_expired`. A URL that was already used is 409 `upload_already_used`, and completing before
sending bytes is 409 `upload_missing`. Make a new upload in each case.

## Add from a URL

If the file is on a public https address, Postaki can fetch it:

```bash
curl https://app.postaki.io/api/v1/media \
  -H "Authorization: Bearer $POSTAKI_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"brand_id": 12, "url": "https://example.com/bread.mp4", "rights_confirmed": true}'
```

A worker downloads it; poll `GET /media/{id}` as above. Only public https addresses on port 443 are fetched: private,
loopback, link-local and cloud metadata addresses are refused with 422 `url_not_allowed`, and so are redirects to them.
If the download fails, `status` is `failed` and `error` says why (`url_fetch_failed`, `media_refused`,
`processing_failed`).

## Media status

| `status` | Meaning |
|---|---|
| `awaiting_upload` | Announced, no bytes yet |
| `receiving`, `uploaded` | Bytes arriving or arrived |
| `processing` | Being checked and prepared (also while a URL is queued or downloading) |
| `ready` | Usable in `media_ids`; `kind`, `mime`, `width`, `height`, `duration_seconds` and `bytes` are filled in |
| `failed` | `error` has a `code` and a `message` |

A media item that is not ready when you use it in a post gives 422 `media_not_ready`.

## From an MCP client

An assistant cannot send file bytes as a tool argument. Use `postaki_add_media_from_url` when the file has a public https
address. Otherwise `postaki_create_media_upload` returns the single-use `upload_url`; something with the file must `PUT`
it, then call `postaki_complete_media_upload` and poll `postaki_get_media`.
