# TASK: patch Bluesky video upload in bluesky.provider.ts (Glass / Postiz v1.47.0)

Use FILE TOOLS ONLY. Do not run commands, do not fetch the web. Files in this folder:
- /srv/second-work/glass16-bsky-video/bluesky.provider.ts  (the current source, input)

Write two files in this same folder:
1. /srv/second-work/glass16-bsky-video/bluesky.provider.patched.ts : the whole file with the changes below.
2. /srv/second-work/glass16-bsky-video/ANSWER.md : first line exactly `RESULT: done` (or `RESULT: blocked: <why>`),
   then a short list of every change you made with line numbers.

Change ONLY the function `uploadVideo(agent, videoPath)` (lines ~65-148) and add one small helper above it.
Everything else in the file must stay byte-for-byte the same.

## Facts (measured on the live service 2026-11-13)
- POST https://video.bsky.app/xrpc/app.bsky.video.uploadVideo returns JSON. On a refusal the body is WRAPPED:
  `{"jobStatus":{"did":"","error":"unconfirmed_email","jobId":"","state":""}}` with HTTP 401.
  The current code reads `jobStatus.jobId` / `.error` at the TOP level, so the error text comes out blank.
  The lexicon output of uploadVideo is `{ jobStatus: JobStatus }`; some responses may be unwrapped. Handle both.
- If the same video was uploaded before, the service answers HTTP 409 with error `already_exists` AND a jobId: that
  is not a failure, keep polling that jobId.
- Vertical Reels (1080x1920) render letterboxed unless the embed carries `aspectRatio: { width, height }`.

## Required changes
A. After `const raw = await uploadResponse.json()`, set `const jobStatus = (raw && raw.jobStatus) ? raw.jobStatus : raw;`
   (typed as `AppBskyVideoDefs.JobStatus`). Use `jobStatus` for everything after.
B. Refusal: if `!jobStatus.jobId && !jobStatus.blob`, console.log `'Glass: Bluesky video upload refused'`, the HTTP
   status and JSON.stringify(raw), then throw
   `new BadBody('bluesky', JSON.stringify(raw), {} as any, msg)` where msg is
   `'Bluesky refused the video (' + status + '): ' + (jobStatus.error || '') + ' ' + (jobStatus.message || '')`
   and, when jobStatus.error === 'unconfirmed_email', append ' - confirm the account email in Bluesky settings to post video'.
C. Polling: keep the existing getJobStatus loop, but
   - wait 5 seconds (not 30) between polls (use the existing `timer` helper),
   - stop after 10 minutes total and throw BadBody 'Bluesky video processing timed out',
   - on JOB_STATE_FAILED include `status.jobStatus.error` and `status.jobStatus.message` in the BadBody message,
   - check FAILED before the wait, and break as soon as a blob is present (no wait after success).
D. aspectRatio: add helper `function mp4Dimensions(buf: Buffer): { width: number; height: number } | undefined`
   that walks MP4 boxes (size uint32 BE + 4-char type; size==1 means 64-bit size in the next 8 bytes; size==0 means
   to end of parent) descending into `moov` and each `trak`, reading the `tkhd` box: version byte at offset 8 of the
   box; width/height are 16.16 fixed-point uint32 at the box END (last 8 bytes of tkhd: width then height). Take the
   first tkhd with width>0 and height>0 (the video track). Return integers (value >> 16). Never throw: wrap in
   try/catch and return undefined. Do not read past the buffer.
   Call it on the downloaded buffer (`video.video`) and, when it returns a value, add `aspectRatio: { width, height }`
   to the returned embed object (keep `$type` and `video`).
E. Keep the `satisfies AppBskyEmbedVideo.Main` on the return. Do not add imports; Buffer, BadBody, timer, AtpAgent,
   BlobRef, AppBskyVideoDefs, AppBskyEmbedVideo are already in scope.
F. Leave the getServiceAuth call (aud did:web:<PDS host>, lxm com.atproto.repo.uploadBlob, exp 30 min) as is.

No placeholder code, no TODOs. If something in the file does not match this description, stop and answer RESULT: blocked.
