diff --git a/crates/engine/src/lightroom_archive.rs b/crates/engine/src/lightroom_archive.rs index 4af75bb1a98..3baf6662268 100644 --- a/crates/engine/src/lightroom_archive.rs +++ b/crates/engine/src/lightroom_archive.rs @@ -530,7 +530,7 @@ impl Serialize for PhotoRef<'_> { fn serialize(&self, serializer: S) -> Result { let p = self.0; let skip = (!self.1).then_some("text"); - let mut out = serializer.serialize_struct("CatalogPhotoArchive", 8)?; + let mut out = serializer.serialize_struct("CatalogPhotoArchive", 9)?; out.serialize_field("source_id", &p.source_id)?; out.serialize_field("uuid", &p.uuid)?; out.serialize_field("path", &p.path)?; @@ -540,6 +540,8 @@ impl Serialize for PhotoRef<'_> { out.serialize_field("keywords", &p.keywords)?; out.serialize_field("history", &RowSeq(&p.history, skip))?; out.serialize_field("snapshots", &RowSeq(&p.snapshots, skip))?; + // a caption only Lightroom's IPTC table holds is in no other column: the recovery copy keeps it (#4043) + out.serialize_field("caption", &p.caption)?; out.end() } } @@ -607,6 +609,7 @@ mod tests { history: vec![BTreeMap::from([("text".into(), serde_json::json!("step")), ("curve".into(), serde_json::json!([0, 128, 255]))])], snapshots: vec![BTreeMap::from([("name".into(), serde_json::json!("before"))])], original: crate::lightroom_catalog::Original::Local, + caption: "Only in IPTC".into(), }; CatalogImport { source: "source.lrcat".into(), @@ -698,6 +701,7 @@ mod tests { ])], snapshots: vec![BTreeMap::from([("name".into(), serde_json::json!("Before")), ("text".into(), serde_json::json!("s = {}"))])], original: crate::lightroom_catalog::Original::Local, + caption: String::new(), } }) .collect(); @@ -767,6 +771,9 @@ mod tests { assert_eq!(decoded["photos"][0]["history"].as_array().unwrap().len(), 1); assert_eq!(decoded["photos"][0]["history"][0]["curve"], serde_json::json!([0, 128, 255])); assert_eq!(decoded["photos"][0]["snapshots"].as_array().unwrap().len(), 1); + // the caption from Lightroom's IPTC table, read back as the photo had it (review of #4293) + let photo: CatalogPhoto = serde_json::from_value(decoded["photos"][0].clone()).unwrap(); + assert_eq!(photo.caption, "Only in IPTC"); assert_eq!(decoded["collections"].as_array().unwrap().len(), 1); assert_eq!(decoded["collections"][0]["name"], serde_json::json!("Keepers")); assert_eq!(decoded["members"].as_array().unwrap().len(), 1); diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rs index 914c70c7c98..322fa2afdd5 100644 --- a/crates/engine/src/lightroom_catalog.rs +++ b/crates/engine/src/lightroom_catalog.rs @@ -210,6 +210,10 @@ pub struct CatalogPhoto { pub keywords: Vec, pub history: Vec, pub snapshots: Vec, + /// The caption Lightroom shows, from its `AgLibraryIPTC` table: it can be missing from the cached XMP packet + /// ([`CatalogPhoto::xmp`]) Lightroom keeps for the image (issue #4043). Empty when the table has none. + #[serde(default)] + pub caption: String, /// Whether `path` may be looked at (derived when the catalog is read; not archived). #[serde(skip)] pub original: Original, @@ -504,8 +508,9 @@ pub fn read_source( total: &AtomicUsize, done: &AtomicUsize, ) -> Result { - // 13 catalog tables plus one validation stage; ImportJob adds one unit per original path. - total.store(14, Ordering::Relaxed); + // 13 catalog tables, the optional IPTC table (captions, #4043) and one validation stage; ImportJob adds one unit + // per original path. + total.store(15, Ordering::Relaxed); done.store(0, Ordering::Relaxed); ensure_read_active(cancel)?; if crate::lightroom_backup::is_backup(path) { @@ -562,6 +567,12 @@ fn read_file(path: &Path, document: &Path, source: &Path, cancel: &AtomicBool, d let roots: HashMap<_, _> = table("AgLibraryRootFolder", true, &mut warnings)?.into_iter().map(|r| (number(&r, "id_local"), r)).collect(); let folders: HashMap<_, _> = table("AgLibraryFolder", true, &mut warnings)?.into_iter().map(|r| (number(&r, "id_local"), r)).collect(); let files: HashMap<_, _> = table("AgLibraryFile", true, &mut warnings)?.into_iter().map(|r| (number(&r, "id_local"), r)).collect(); + // the caption each image has in Lightroom's own IPTC table (the cached XMP below may not restate it) + let mut captions: HashMap<_, _> = optional_table("AgLibraryIPTC", &mut warnings)? + .into_iter() + .filter(|r| !text(r, "caption").trim().is_empty()) + .map(|r| (number(&r, "image"), text(&r, "caption").to_string())) + .collect(); let develops: HashMap<_, _> = optional_table("Adobe_imageDevelopSettings", &mut warnings)? .into_iter() .map(|r| (number(&r, "image"), text(&r, "text").to_string())) @@ -693,6 +704,7 @@ fn read_file(path: &Path, document: &Path, source: &Path, cancel: &AtomicBool, d keywords: keywords.remove(&id).unwrap_or_default(), history: history.remove(&id).unwrap_or_default(), snapshots: snapshots.remove(&id).unwrap_or_default(), + caption: captions.remove(&id).unwrap_or_default(), original, }); } @@ -1404,6 +1416,10 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co Err(e) => warnings.push(format!("image {} XMP: {e}", src.source_id)), } } + // Lightroom's own caption, over the cached packet's (which can lack it or be older) + if !src.caption.is_empty() { + p.meta.caption = src.caption.clone(); + } p.rating = number(&src.image, "rating").clamp(0, 5) as u8; p.flag = match number(&src.image, "pick") { 1 => Flag::Pick, diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index 3503960858e..40d5785cb77 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -15,6 +15,7 @@ fn sample() -> CatalogImport { history: vec![row(json!({"text":"s = { Exposure2012 = 0.5 }"}))], snapshots: Vec::new(), original: Original::Local, + caption: String::new(), }; let mut copy = photo.clone(); copy.source_id = 2; @@ -1625,6 +1626,43 @@ fn photo_tables(root: (&str, &str)) -> Vec<(&'static str, &'static str, VecFrom the packet"#; + let import_with = |iptc: Option>, cached: bool| { + let path = dir.join("captions.lrcat"); + let mut tables = photo_tables((dir.to_str().unwrap(), "")); + if cached { + tables.push(("Adobe_AdditionalMetadata", "id_local INTEGER PRIMARY KEY, image, xmp", vec![N, I(1), T(packet.into())])); + } + if let Some(row) = iptc { + tables.push(("AgLibraryIPTC", "id_local INTEGER PRIMARY KEY, caption, copyright, image", row)); + } + std::fs::write(&path, native_catalog(&tables)).unwrap(); + let mut s = crate::Session::new(); + let imported = s.execute("library.importLightroom", &json!({"path": path})).unwrap(); + let id = PhotoId(imported["mapping"]["1"].as_u64().unwrap()); + s.catalog.photo(id).unwrap().meta.caption.clone() + }; + let import = |iptc| import_with(iptc, false); + assert_eq!(import(Some(vec![N, T("Harbour at dawn".into()), N, I(1)])), "Harbour at dawn"); + assert_eq!(import(Some(vec![N, T(" ".into()), N, I(1)])), "", "a blank caption isn't one"); + assert_eq!(import(Some(vec![N, T("Another image's".into()), N, I(2)])), "", "only the image's own row"); + assert_eq!(import(None), "", "a catalog without the table imports as before"); + let cached = |iptc| import_with(iptc, true); + assert_eq!(cached(None), "From the packet", "the packet's caption imports as before"); + assert_eq!(cached(Some(vec![N, T("Harbour at dawn".into()), N, I(1)])), "Harbour at dawn", "the table's caption wins"); + assert_eq!(cached(Some(vec![N, T(" ".into()), N, I(1)])), "From the packet", "a blank caption keeps the packet's"); + assert_eq!(cached(Some(vec![N, T("Another image's".into()), N, I(2)])), "From the packet", "another image's row keeps it"); + std::fs::remove_dir_all(dir).unwrap(); +} + #[test] fn native_video_import_is_visible_in_video_filter() { let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-video-filter-{}", std::process::id())); diff --git a/crates/engine/src/lightroom_job.rs b/crates/engine/src/lightroom_job.rs index 695398bcc69..434d2a2b36a 100644 --- a/crates/engine/src/lightroom_job.rs +++ b/crates/engine/src/lightroom_job.rs @@ -416,6 +416,7 @@ mod tests { history: Vec::new(), snapshots: Vec::new(), original: lightroom_catalog::Original::Local, + caption: String::new(), }], collections: Vec::new(), members: Vec::new(), @@ -564,6 +565,7 @@ mod tests { )])], snapshots: Vec::new(), original: lightroom_catalog::Original::Local, + caption: String::new(), } }) .collect(); diff --git a/docs/lightroom-catalog-import.md b/docs/lightroom-catalog-import.md index 87d17c999ef..ecfbb10e316 100644 --- a/docs/lightroom-catalog-import.md +++ b/docs/lightroom-catalog-import.md @@ -62,7 +62,8 @@ is also available through `library.importLightroom {"status":true}` during that Original files are referenced in place. Ratings, picks/rejects, color labels, XMP metadata, hierarchical keywords, regular collections/collection sets, virtual copies and mapped develop -settings are imported. Missing originals remain catalogued for relinking, count as added records and are reported +settings are imported. An image's caption is taken from Lightroom's own IPTC table (`AgLibraryIPTC`) when it has +one there, since the XMP packet Lightroom caches for the image can lack it (issue #4043). Missing originals remain catalogued for relinking, count as added records and are reported separately from failures reading files that are present. Known-missing originals are not probed again. Previously browsed Local records join All Photos even when their originals are offline; their promotion is counted once and undone together with the catalog import. diff --git a/docs/whats-new.md b/docs/whats-new.md index cfc166465f2..b0dee416abb 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -2,6 +2,11 @@ ## Unreleased +### Captions from a Lightroom catalog +- A Lightroom catalog import brings across every photo's caption. It read captions only from the XMP Lightroom + caches inside the catalog, which often doesn't have them, so most photos arrived without the caption Lightroom + showed. It now reads Lightroom's own IPTC table too (issue #4043). + ### Export subfolder variables - The Export dialog's Subfolder field now supports `{album}` (active album name sanitized for filesystem safety, empty outside an album view), `{date}` (batch export date in `YYYYMMDD`, local time), and `{hour}` (batch export time in `HHMM`, local time). An **Insert…** dropdown next to the field inserts tokens at the cursor. Tokens are stored in presets and expanded at export time, so the same preset produces dated subfolders on every use. ### Heal meets its surroundings (process 14)