SPB Git

Repository Public License

storage.md 5.9 KB

# Storage, Library and Export

# Layout

text
Application Support/ORIXCamera/
├── Media/         every capture: ORIX_yyyyMMdd_HHmmss_SSS.(heic|jpg|dng|mov|tif)
├── Incoming/      scratch on the same volume: files being written, movies being recorded
├── Thumbnails/    <fileName>.jpg, 480 px, regenerable
├── Exports/       copies made for the share sheet, clearable from Settings
└── Library.store  SwiftData index

The root is given the file-protection class "complete until first user authentication".

# Writes are atomic

A capture is either fully on disk or reported as failed.

Operation Steps
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
CaptureStorage.adopt(file) move a finished file (a movie, a rendered multi-frame image) from Incoming/ into Media/

Both 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.

# Index

MediaLibrary holds a SwiftData container with two models.

MediaItem
identity id, createdAt, kind (photo, raw, proRAW, video, timeLapse, slowMotion, longExposure, lightTrails, hdrFusion, noiseStack)
files fileName (primary), companionFileName (HEIC of a RAW pair)
facts pixel size as displayed, duration, byte size, captureSummary (searchable text: lens, ISO, shutter, mode, format, frames…)
user state isFavorite, title, albums
edit recipeData: the EditRecipe as JSON, nil when unedited
other visualHash (difference hash), isSynthetic

MediaAlbum has a name and a many-to-many relation to items. Deleting an album leaves its items.

The container is retained by the library object for its whole life. Autosave is off; every mutation saves explicitly.

# The files are the source of truth

Situation Handling
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
Database lost or unreadable same mechanism rebuilds the index from Media/ (titles, favourites, albums and recipes are lost; captures are not)
A row whose file is gone listed by itemsMissingFiles(); the library shows a notice with an explicit action to remove the entries
Recording interrupted the fragmented movie in Incoming/ is adopted at next launch

AppEnvironment is created once per process (static let shared), so two libraries can never open the same store.

# Edits are recipes

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.

Three capture-time choices are stored the same way instead of being baked into the file:

Choice at capture Stored as
Capture look (Amber, Tide, Graphite…) lookID
Aspect ratio crop
Mirror front camera photos flippedHorizontally

So 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.

EditHistory keeps up to 100 states for undo and redo and ignores a commit identical to the current state.

# Duplicates

  • Exact: SHA-256 of the file, streamed in 1 MB chunks.
  • Visual: a 64-bit difference hash from a 9 × 8 grey thumbnail; items within 4 bits are grouped by single linkage.

# Photos

Adding 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.

# Export

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.

Policy Effect
keep all EXIF, TIFF and GPS kept; orientation reset to 1 (pixels are upright); pixel dimensions updated; containers specific to the source format removed
remove location the same, without the GPS dictionary
remove all no source metadata

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.

flowchart TD
    CAP[Capture data] --> W[CaptureStorage.write / adopt]
    W --> M[(Media/)]
    W --> IDX[MediaLibrary.add]
    IDX --> DB[(Library.store)]
    M -.orphan at launch.-> REC[reconcile]
    REC --> DB
    DB --> UI[Library · Viewer · Editor]
    UI -->|recipe JSON| DB
    UI --> EXP[Export] --> OUT[(Exports/)] --> SHARE[Share sheet · Photos]

# What is tested

Atomic 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.

Not tested: saving to Photos, the share sheet, behaviour on a nearly full device.

Files main
Menu

Theme