LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Lightroom import: skip images whose file record is missing instead of failing
Carlos Villanueva9c812b88a9aa
diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rsindex 5f39188a1..86d9d80c9 100644--- a/crates/engine/src/lightroom_catalog.rs+++ b/crates/engine/src/lightroom_catalog.rs@@ -523,12 +523,25 @@ pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize,collection_content: optional_table("AgLibraryCollectionContent", &mut warnings)?,warnings,};+ // images whose file, folder or root record is gone (Lightroom leaves these behind and ignores+ // them); they and their virtual copies are skipped with a warning instead of failing the import+ let mut orphans = HashSet::new();for image in table("Adobe_images", true)? {ensure_read_active(cancel)?;let id = number(&image, "id_local");- let file = files.get(&number(&image, "rootFile")).ok_or_else(|| format!("image {id}: missing file record"))?;- let folder = folders.get(&number(file, "folder")).ok_or_else(|| format!("image {id}: missing folder record"))?;- let root = roots.get(&number(folder, "rootFolder")).ok_or_else(|| format!("image {id}: missing root record"))?;+ let record = files.get(&number(&image, "rootFile")).ok_or("file").and_then(|file| {+ let folder = folders.get(&number(file, "folder")).ok_or("folder")?;+ let root = roots.get(&number(folder, "rootFolder")).ok_or("root")?;+ Ok((file, folder, root))+ });+ let (file, folder, root) = match record {+ Ok(found) => found,+ Err(what) => {+ out.warnings.push(format!("image {id}: missing {what} record; not imported"));+ orphans.insert(id);+ continue;+ }+ };let root_id = number(root, "id_local");let root_path = root_paths.get(&root_id).map_or("", String::as_str);let folder_path = text(folder, "pathFromRoot");@@ -562,11 +575,31 @@ pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize,original,});}+ drop_copies_of(&mut out, &orphans);refuse_non_media(&mut out);ensure_read_active(cancel)?;Ok(out)}+/// Drop the virtual copies whose master was not imported, saying so in the import's warnings: a+/// copy has no original of its own.+fn drop_copies_of(data: &mut CatalogImport, masters: &HashSet<i64>) {+ if masters.is_empty() {+ return;+ }+ let mut warnings = Vec::new();+ data.photos.retain(|p| {+ let master = number(&p.image, "masterImage");+ // 0 marks a master, never a copy+ if master == 0 || !masters.contains(&master) {+ return true;+ }+ warnings.push(format!("image {}: not imported; its original was not imported", p.source_id));+ false+ });+ data.warnings.extend(warnings);+}+/// Drop the catalog's images whose original is not a file type LightCraft reads, saying so in the/// import's warnings.///@@ -1581,6 +1614,28 @@ mod tests {std::fs::remove_file(path).unwrap();}+ #[test]+ fn images_without_a_file_record_are_skipped_not_fatal() {+ let mut tables = photo_tables(("/catalog/", ""));+ tables[3].2[2] = SqlValue::Integer(2);+ let path = std::env::temp_dir().join(format!("lightcraft-orphan-catalog-{}.lrcat", std::process::id()));+ std::fs::write(&path, native_catalog(&tables)).unwrap();+ let catalog = read(&path).unwrap();+ std::fs::remove_file(path).unwrap();+ assert!(catalog.photos.is_empty());+ assert!(catalog.warnings.iter().any(|w| w == "image 1: missing file record; not imported"), "{:?}", catalog.warnings);+ }++ #[test]+ fn copies_of_a_skipped_master_are_dropped() {+ let mut data = sample();+ data.photos.retain(|p| p.source_id != 1);+ drop_copies_of(&mut data, &HashSet::from([1]));+ assert!(data.photos.is_empty());+ assert_eq!(data.warnings, ["image 2: not imported; its original was not imported"]);+ validate(&data).unwrap();+ }+#[test]fn preparation_skips_missing_original_probes_but_keeps_catalog_records() {let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-offline-probes-{}", std::process::id()));diff --git a/docs/lightroom-catalog-import.md b/docs/lightroom-catalog-import.mdindex b6c0014f0..bc6f54358 100644--- a/docs/lightroom-catalog-import.md+++ b/docs/lightroom-catalog-import.md@@ -28,6 +28,9 @@ settings are imported. Missing originals remain catalogued for relinking, countseparately from failures reading files that are present. Known-missing originals are not probedagain. 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.+An image whose file, folder or root folder record is gone from the catalog (Lightroom leaves these+behind and ignores them) is skipped with a warning, together with its virtual copies, instead of+failing the whole import.Virtual copies count as added records too; reimport reports zero additions when allrecords already exist. Existing LightCraftedits are preserved by default; `updateExisting=true` explicitly replaces them. Persistent import