# Video Upload and Encoding

## Objective

Accept a video file and turn it into versions that play in a browser.

An upload path for video files plus a background pipeline that produces web-playable renditions and a poster frame.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Extend the app's existing File Upload rather than adding a second uploader. Video differs in size and in the processing that follows, not in how a user picks a file, and two uploaders means two sets of limits to keep in sync.
2. Upload in resumable chunks straight to storage, and enforce size, duration, and type limits on the server. Client-side limits are a convenience, not a control.
3. Do the encoding in the app's existing background job system so the user is not holding a tab open, and surface its state through the app's existing Background Job Progress rather than a bespoke poller.
4. Produce the minimum coherent set of outputs: one or two web-playable renditions covering a small and a large screen, plus a poster frame. Do not build a general-purpose adaptive ladder until someone is actually watching on a bad connection.
5. Run whatever content scanning the app already applies to uploads, including Upload Malware Scanning if present, before any output becomes reachable. A video is a container format and is a perfectly good delivery mechanism for something else.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Large files will be interrupted by dropped connections, closed laptops, and mobile handoffs. Support resuming an interrupted upload from where it stopped rather than restarting a multi-gigabyte transfer.
- Upload, queue wait, and encode are three different stages with different durations, and a single bar that sits at ninety-nine percent through a long encode reads as broken. Show which stage the job is in and what is still to come.
- Plenty of common recordings use codecs or containers browsers will not play, and some files are simply corrupt. Detect this before encoding, transcode where you can, and reject with a specific message where you cannot.
- A failed or cancelled job leaves a source file and partial outputs behind. Clean both up on failure and sweep for orphans on a schedule, or storage cost grows quietly forever.
- Cap concurrent jobs per account so one person uploading fifty videos cannot starve every other tenant's queue, and make the queue position visible when a job is waiting.
- Decide who is allowed to read a finished video back, and enforce it on the delivery path. If the app has Secure File Downloads, route playback through the same signed, expiring access rather than leaving outputs on a guessable public address.
- A user who navigates away mid-upload needs a warning, and the partial upload needs an expiry so abandoned chunks do not accumulate.
- Retention matters. Decide whether the original source file is kept after encoding, say so, and apply the same deletion rules to renditions when the record is deleted.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Uploads are chunked, resumable, and validated for size, duration, and type on the server.
- [ ] Encoding runs as a background job with visible upload, queue, and encode stages.
- [ ] Unplayable formats are detected before encoding and either transcoded or rejected with a specific reason.
- [ ] Failed and cancelled jobs leave no orphaned source files or partial outputs.
- [ ] Concurrent jobs are capped per account and queue position is visible.
- [ ] Finished videos are readable only by users permitted to see the owning record.
- [ ] Every upload passes the app's existing content scanning before its outputs are reachable.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
