SPB Git

Repository Public License

storage.md 5.9 KB 117 lines markdown

1<!--2ORIX Camera3File: docs/architecture/storage.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->78# Storage, Library and Export910## Layout1112```text13Application Support/ORIXCamera/14├── Media/         every capture: ORIX_yyyyMMdd_HHmmss_SSS.(heic|jpg|dng|mov|tif)15├── Incoming/      scratch on the same volume: files being written, movies being recorded16├── Thumbnails/    <fileName>.jpg, 480 px, regenerable17├── Exports/       copies made for the share sheet, clearable from Settings18└── Library.store  SwiftData index19```2021The root is given the file-protection class "complete until first user authentication".2223## Writes are atomic2425A capture is either fully on disk or reported as failed.2627| Operation | Steps |28|---|---|29| `CaptureStorage.write(data)` | check that free space minus the data stays above the 200 MB reserve → write to `Incoming/<uuid>` → move into `Media/` under a unique name |30| `CaptureStorage.adopt(file)` | move a finished file (a movie, a rendered multi-frame image) from `Incoming/` into `Media/` |3132Both are on the same volume, so the move is a rename. Names carry the capture time to the millisecond and get a numeric suffix on collision; nothing is ever overwritten. A failure removes the temporary file and throws; the model shows the error. There is no path on which a capture disappears silently.3334## Index3536`MediaLibrary` holds a SwiftData container with two models.3738| `MediaItem` | |39|---|---|40| identity | `id`, `createdAt`, `kind` (photo, raw, proRAW, video, timeLapse, slowMotion, longExposure, lightTrails, hdrFusion, noiseStack) |41| files | `fileName` (primary), `companionFileName` (HEIC of a RAW pair) |42| facts | pixel size as displayed, duration, byte size, `captureSummary` (searchable text: lens, ISO, shutter, mode, format, frames…) |43| user state | `isFavorite`, `title`, albums |44| edit | `recipeData`: the `EditRecipe` as JSON, nil when unedited |45| other | `visualHash` (difference hash), `isSynthetic` |4647`MediaAlbum` has a name and a many-to-many relation to items. Deleting an album leaves its items.4849The container is retained by the library object for its whole life. Autosave is off; every mutation saves explicitly.5051## The files are the source of truth5253| Situation | Handling |54|---|---|55| Crash between writing a file and indexing it | `reconcile()` at launch indexes every media file the index does not know, with kind from the extension and size from the file; the app announces the count |56| Database lost or unreadable | same mechanism rebuilds the index from `Media/` (titles, favourites, albums and recipes are lost; captures are not) |57| A row whose file is gone | listed by `itemsMissingFiles()`; the library shows a notice with an explicit action to remove the entries |58| Recording interrupted | the fragmented movie in `Incoming/` is adopted at next launch |5960`AppEnvironment` is created once per process (`static let shared`), so two libraries can never open the same store.6162## Edits are recipes6364`EditRecipe` is a `Codable` struct: tone and colour values, a five-point curve, a look id and intensity, crop, quarter turns, straighten angle, flip, and optional RAW settings. An identity recipe is stored as nil.6566Three capture-time choices are stored the same way instead of being baked into the file:6768| Choice at capture | Stored as |69|---|---|70| Capture look (Amber, Tide, Graphite…) | `lookID` |71| Aspect ratio | `crop` |72| Mirror front camera photos | `flippedHorizontally` |7374So the file always holds the full, unmodified capture, and each choice can be undone later. The consequence: another app opening the file directly sees the unedited image. Exporting applies the recipe.7576`EditHistory` keeps up to 100 states for undo and redo and ignores a commit identical to the current state.7778## Duplicates7980- Exact: SHA-256 of the file, streamed in 1 MB chunks.81- Visual: a 64-bit difference hash from a 9 × 8 grey thumbnail; items within 4 bits are grouped by single linkage.8283## Photos8485Adding to Photos uses `PHPhotoLibrary.requestAuthorization(for: .addOnly)` and a `PHAssetCreationRequest`. A RAW pair is one asset: the processed image as `.photo`, the DNG as `.alternatePhoto`. Files are copied, never moved. A refusal is reported, and the capture stays in the ORIX library.8687## Export8889`ImageExporter.export` redraws the rendered image at the target size, colour space and bit depth, filters the source metadata by policy, and encodes with Image I/O.9091| Policy | Effect |92|---|---|93| keep all | EXIF, TIFF and GPS kept; orientation reset to 1 (pixels are upright); pixel dimensions updated; containers specific to the source format removed |94| remove location | the same, without the GPS dictionary |95| remove all | no source metadata |9697`exportOriginal` is a file copy. `VideoExporter` uses `AVAssetExportSession`: passthrough for Original (a plain copy when no metadata must be removed), HEVC or H.264 presets otherwise; location metadata is filtered when requested.9899```mermaid100flowchart TD101    CAP[Capture data] --> W[CaptureStorage.write / adopt]102    W --> M[(Media/)]103    W --> IDX[MediaLibrary.add]104    IDX --> DB[(Library.store)]105    M -.orphan at launch.-> REC[reconcile]106    REC --> DB107    DB --> UI[Library · Viewer · Editor]108    UI -->|recipe JSON| DB109    UI --> EXP[Export] --> OUT[(Exports/)] --> SHARE[Share sheet · Photos]110```111112## What is tested113114Atomic writes and unique names; base names sort chronologically; adoption and interrupted-recording discovery; thumbnails respect orientation; add, filter, search, sort; favourites, recipes, albums; delete removes files and row; reconcile recovers orphans and reports missing files; persistence across reopen; duplicate groups; every export format decodes back to the same picture; metadata policies; original export is byte-identical; file names are sanitised and never overwrite; 16-bit sources stay deep in TIFF and PNG; video passthrough and re-encode.115116Not tested: saving to Photos, the share sheet, behaviour on a nearly full device.117
Files main
Menu

Theme