Media ingestion and storage
Media ingestion turns an owner-selected image into a verified Media Asset with a protected Original, safe catalog metadata, and public Renditions. The system keeps the source private, makes delivery files safe for visitors, and records enough durable state to resume or clean up work when storage and database actions do not complete together. This page is a child of Media management platform; see Photo selection and publication for public selection rules and Media administration and retention for review and cleanup operations.
Media ingestion boundary
Media asset lifecycle
The ingestion boundary accepts one owner-authorized image transfer and creates one Media Asset only after the Original and all required Renditions pass verification. An Upload Intent is a short-lived reservation that states what will be transferred. A Transfer Job is the owner-visible work item for that reservation. A Media Asset is the reusable catalog item that exists after verification begins and becomes visible to the library only when processing is ready.
The lifecycle separates transfer progress from catalog state and processing state. This lets the system show failed or unfinished work for retry or Discard without presenting an empty asset as usable media.
The main path from browser input to a delivery-ready asset is:
flowchart LR
A[Owner chooses image] --> B[Upload Intent]
B --> C[Bounded same-origin chunks]
C --> D[Assemble and verify Original]
D --> E[Create Media Asset]
E --> F[Process image]
F --> G[Record and verify Renditions]
G --> H[Capture safe metadata]
H --> I[Ready Media Asset]
References: lib/…/service.ts• L18-80 lib/…/service.ts• L434-666 lib/…/service.ts• L5-17 lib/…/CONTEXT.md• L17-50
Transfer jobs
The transfer path avoids direct browser writes to the Bunny storage endpoint. The browser sends bounded same-origin requests to protected application routes, and the server checks the owner session, request origin, transfer order, declared size, and per-chunk SHA-256 checksum before writing a chunk.
Each Original is limited to 50 MiB and uses chunks of up to 4 MiB. The client retries rate-limit and server errors a small number of times and honors Retry-After when the server supplies it. A completed transfer is still not a ready asset: the completion step reads the full Original, checks its content type, byte size, and checksum, then removes the temporary chunks.
Transfer state remains visible when a transfer or processing attempt fails. Discard closes the intent before it removes storage objects. This ordering prevents a late chunk request from leaving an orphaned transfer object or from creating a catalog item after the owner has discarded the work.
References: app/…/route.ts• L10-25 app/…/route.ts• L7-25 app/…/route.ts• L7-17 lib/…/upload.ts• L96-207 lib/…/transfer.ts• L1-16 lib/…/client.ts• L7-53 lib/…/service.ts• L94-146
Image processing
Original handling
The Original is the protected, full-quality source. Processing reads it without changing its bytes, checks that it is a supported still image, applies its orientation for derived output, and rejects animated, malformed, unsupported, or over-sized input.
The processor reads only an allowlist of useful image fields. It keeps dimensions, capture time, camera details, and exposure details as catalog metadata. Raw embedded metadata does not move into the public files or become a general-purpose metadata blob.
References: lib/…/image.ts• L8-48 lib/…/image.ts• L91-119 lib/…/image.ts• L194-299 lib/…/CONTEXT.md• L32-56
Rendition profiles
A Rendition is a delivery-ready image derived from the Original. The current processor creates no-upscale profiles with maximum widths of 640, 1024, 1600, and 2560 pixels. Each profile is an sRGB progressive JPEG with quality 90, 4:4:4 chroma, and stripped embedded metadata.
The system gives each Rendition a checksum-based object key and stores its manifest in the catalog before it writes the object to Bunny. It then checks the stored object again. This order preserves the exact key and expected bytes for retry, reconciliation, Discard, and Purge even if the provider write or process stops part way through.
An asset becomes ready only when the catalog contains the complete required profile set and the asset is still active. A stale or concurrent processing attempt cannot mark an asset ready after Discard or Purge has canceled it.
References: lib/…/image.ts• L8-10 lib/…/image.ts• L241-299 lib/…/service.ts• L540-665 lib/…/repository.ts• L130-206 lib/…/lifecycle-locks.ts• L33-43
Storage provider contract
Bunny Media zone
The Media Library uses one Bunny Storage Zone for Originals, Renditions, and transfer chunks. Separate object namespaces provide the logical boundary: originals/ holds protected sources, transfer-chunks/ holds incomplete uploads, and renditions/ holds files that may be delivered through the CDN.
The Pull Zone exposes only Renditions. Edge rules block the Original and transfer-chunk paths, so a public URL cannot serve raw source bytes or incomplete uploads. Storage credentials stay in server-only configuration, while browsers receive only a CDN URL for a verified Rendition.
References: docs/…/0013-one-bunny-media-zone.md• L3-13 lib/…/bunny.ts• L70-113 lib/…/bunny.ts• L220-369 lib/…/server.ts• L1-10
Provider checks
The provider boundary treats object identity and response data as contracts rather than assumptions. Original keys must use the protected namespace. Rendition keys must use the Rendition namespace, end in .jpg, and contain the Rendition checksum. Reads verify size and content type, while writes send the SHA-256 checksum to Bunny.
The account contract also checks the configured non-production zone, HTTPS CDN hostname, active Pull Zone, cache time, and blocked protected paths. A provider or account setup that does not meet these rules fails before it can silently expose Originals or serve unverified Renditions.
References: lib/…/bunny.ts• L70-134 lib/…/bunny.ts• L157-218 lib/…/bunny.ts• L220-365 lib/…/contract.ts• L93-125 lib/…/contract.ts• L183-257
Asset catalog
Catalog records
The catalog is the source of truth for Media Asset identity, storage keys, processing progress, image properties, and safe descriptive metadata. A Media Asset points to one Upload Intent and one protected Original. Its Rendition records map each profile width to one object key, checksum, size, dimensions, and delivery properties.
The catalog keeps provider listings out of the product state. It can therefore decide whether an asset is active, archived, ready, retryable, or in repair from durable records rather than from a live storage scan. Foreign-key and uniqueness rules prevent two assets from claiming one intent or one profile for the same asset.
References: db/…/0005_media_catalog.sql• L1-110 lib/…/repository.ts• L26-88 db/schema.ts• L235-350 db/schema.ts• L420-450
Processing coordination
Processing, Discard, and Purge share one per-asset lock. The lock covers the catalog manifest and the related provider action, so cleanup sees a committed Rendition key or cancels processing before a new provider write starts. Processing may reclaim work that has remained active beyond its lease, which allows a failed worker to be retried without creating a second asset.
The database records retryable provider failures separately from repair-required input or integrity failures. Retry can repeat transient work. Repair is needed when the stored Original, image data, metadata, or Rendition manifest cannot satisfy the contract.
References: lib/…/lifecycle-locks.ts• L33-43 lib/…/repository.ts• L308-381 lib/…/service.ts• L261-339 lib/…/service.ts• L395-429 lib/…/CONTEXT.md• L85-100
Location privacy
Capture location
Capture Location is the private latitude and longitude read from the Original. The image processor validates and rounds the coordinates, and ingestion seals them in an encrypted catalog envelope. The envelope uses authenticated encryption and a versioned format so the system can reject altered or unsupported location data.
Capture Location is never a public field and is not sent to a visitor. Owner-only location features may open it when needed to create a Location Label, while public Display Metadata uses only an approved, non-coordinate label. Missing or invalid location data does not stop an otherwise valid image from becoming ready.
References: lib/…/image.ts• L71-89 lib/…/image.ts• L282-297 lib/…/capture-location.ts• L5-25 lib/…/capture-location.ts• L38-99 docs/…/location-label-geocoding.md• L3-19
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the rendition profile list aligned across image processing, catalog constraints, manifest completeness checks, and published Rendition references.
- Keep the three Bunny object namespaces aligned across key validation, Pull Zone block rules, upload cleanup, and public URL generation.
- Preserve the checksum, byte-size, and content-type contract across browser chunks, Original assembly, provider writes, and provider reads.
- Preserve manifest-before-storage ordering and the shared asset lock when changing processing, Discard, or Purge behavior.
- Treat changes to the Capture Location envelope version, encryption key, or associated data as a data migration and privacy boundary change.
- Keep chunk size and request-body limits aligned with the application route and hosting platform limits.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A chunk has the wrong size, checksum, or predecessor | The chunk is rejected and the transfer cannot advance | Bounded body reads, ordered chunks, declared sizes, and SHA-256 checks |
| The assembled Original differs from its Upload Intent | No Media Asset is created or processing stops with a repair state | Full-object size, content-type, and checksum verification |
| Bunny returns a transient provider error | Processing remains retryable instead of being reported as ready | Provider error classification and durable processing state |
| A Rendition write fails after its manifest is recorded | Cleanup can still find the intended object key | Deterministic checksum-based keys and manifest-before-write ordering |
A Pull Zone rule exposes originals/ or transfer-chunks/ | Private source bytes or incomplete uploads may become public | Account contract checks for both blocked paths |
| Capture Location data is malformed or its envelope cannot be opened | Location use fails without publishing coordinates | Coordinate validation, authenticated encryption, and private-only access |
Key files
| File | Why to read it |
|---|---|
| lib/…/service.ts | Defines the end-to-end contract from Upload Intent completion through verified readiness. |
| lib/…/image.ts | Shows the supported image boundary, metadata allowlist, and Rendition profiles. |
| lib/…/upload.ts | Shows the protected same-origin transfer boundary and chunk checks. |
| lib/…/bunny.ts | Defines object namespaces, provider operations, checksum rules, and public Rendition URLs. |
| lib/…/contract.ts | Defines the Bunny account and CDN safety checks. |
| lib/…/repository.ts | Shows how processing progress and Rendition manifests become durable catalog state. |
| lib/…/capture-location.ts | Defines the private Capture Location envelope and its validation boundary. |
| db/…/0005_media_catalog.sql | Shows the catalog relationships and integrity rules for intents, assets, and Renditions. |
| docs/…/0013-one-bunny-media-zone.md | Explains the one-zone topology and the public/private namespace boundary. |