|
| 1 | +//! Structured media metadata — domain type and extractor port. |
| 2 | +//! |
| 3 | +//! Framework-free value types consumed by `perima-media` extractors |
| 4 | +//! and persisted by `perima-db`'s `SqliteMetadataRepository`. |
| 5 | +
|
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | + |
| 10 | +use crate::{BlakeHash, CoreError}; |
| 11 | + |
| 12 | +/// Structured metadata extracted from a media file. |
| 13 | +/// |
| 14 | +/// WHY optional everywhere: not every file has every field (a PNG has |
| 15 | +/// no `captured_at`; an MP4 may have no camera info). Partial |
| 16 | +/// information is the norm with EXIF / container tag extraction, so |
| 17 | +/// each field is independently nullable. |
| 18 | +/// |
| 19 | +/// WHY `captured_at: Option<String>` (ISO 8601) not `DateTime<Utc>`: |
| 20 | +/// consistency with the existing `first_seen` / `last_seen` String |
| 21 | +/// columns, and it keeps `chrono` out of `perima-core`. |
| 22 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 23 | +pub struct MediaMetadata { |
| 24 | + /// Content hash of the file this metadata describes. Matches the |
| 25 | + /// corresponding row in the `files` table. |
| 26 | + pub hash: BlakeHash, |
| 27 | + /// Pixel width (images / video). `None` for formats without a |
| 28 | + /// natural width (audio, future PDF extractors). |
| 29 | + pub width: Option<u32>, |
| 30 | + /// Pixel height (images / video). `None` for formats without a |
| 31 | + /// natural height. |
| 32 | + pub height: Option<u32>, |
| 33 | + /// Duration in milliseconds (video / audio). `None` for still |
| 34 | + /// images. |
| 35 | + pub duration_ms: Option<u64>, |
| 36 | + /// ISO 8601 UTC capture timestamp. Sourced from EXIF |
| 37 | + /// `DateTimeOriginal` for images, container tags for video. |
| 38 | + pub captured_at: Option<String>, |
| 39 | + /// Camera manufacturer (EXIF `Make`). |
| 40 | + pub camera_make: Option<String>, |
| 41 | + /// Camera model (EXIF `Model`). |
| 42 | + pub camera_model: Option<String>, |
| 43 | + /// Codec identifier (e.g. `"avc1"`, `"hevc"`). Video only. |
| 44 | + pub codec: Option<String>, |
| 45 | + /// Overall bitrate in bits per second. Video only. |
| 46 | + pub bitrate_bps: Option<u32>, |
| 47 | + /// MIME type as detected at extraction time. |
| 48 | + pub mime_type: Option<String>, |
| 49 | +} |
| 50 | + |
| 51 | +/// MIME-dispatched extractor. |
| 52 | +/// |
| 53 | +/// WHY MIME dispatch not "first-non-empty": a JPEG EXIF extractor that |
| 54 | +/// returns `{width: None, mime_type: Some(..)}` would falsely "win" |
| 55 | +/// against a video extractor that could actually extract duration. |
| 56 | +/// Dispatching by `accepts(mime)` avoids this ambiguity — each |
| 57 | +/// extractor declares the MIME families it handles, and the composite |
| 58 | +/// picks the first match. |
| 59 | +pub trait MetadataExtractor: Send + Sync { |
| 60 | + /// Whether this extractor handles the given MIME type. |
| 61 | + fn accepts(&self, mime: &str) -> bool; |
| 62 | + |
| 63 | + /// Extract metadata from the file at `absolute_path`. |
| 64 | + /// |
| 65 | + /// # Errors |
| 66 | + /// Returns `CoreError::Io` if the file cannot be read, or |
| 67 | + /// `CoreError::Internal` on decoder-level failures. Missing |
| 68 | + /// optional fields are not errors — they are `None` in the |
| 69 | + /// returned `MediaMetadata`. |
| 70 | + fn extract( |
| 71 | + &self, |
| 72 | + hash: BlakeHash, |
| 73 | + absolute_path: &Path, |
| 74 | + mime: &str, |
| 75 | + ) -> Result<MediaMetadata, CoreError>; |
| 76 | +} |
0 commit comments