diff --git a/crates/engine/src/cmd/library/import.rs b/crates/engine/src/cmd/library/import.rs index 188dc6ae99..61a7ccefda 100644 --- a/crates/engine/src/cmd/library/import.rs +++ b/crates/engine/src/cmd/library/import.rs @@ -82,6 +82,8 @@ pub fn import_params(s: &Session, p: &Value) -> Result { metadata_preset, convert_dng: bool_or(p, "dng", false), local: bool_or(p, "local", false), + // (only the Lightroom import makes records of duplicates) + read_duplicates: false, recipe: crate::cmd::recipe_param(p, "library.import")?, }; let album_name = str_param(p, "albumName").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string); diff --git a/crates/engine/src/import/job.rs b/crates/engine/src/import/job.rs index 4c120acc19..847942b3cc 100644 --- a/crates/engine/src/import/job.rs +++ b/crates/engine/src/import/job.rs @@ -298,10 +298,19 @@ impl ImportJob { // The sidecar of every file that was probed, looked up for the batch at once: each is a few // names asked of the disk, which on a network share made a folder of thousands of photos wait // a round trip per name, one name after another. - // (a file whose content the library has is read too: a Lightroom import makes it a new record - // from its file and sidecar, #2589) let asked: Vec<&str> = todo.iter().zip(&probed).filter(|(_, p)| p.is_ok()).map(|(f, _)| f.as_str()).collect(); - let mut found = sidecars_of(&asked, cancel, |f| crate::sidecar::find_sidecar_for_import(&stems, self.naming, f)).into_iter(); + // (a file whose content the library has is skipped below as a duplicate: it asks the disk + // nothing, unless the import makes a record of it from its file and sidecar, #2589) + let skipped = !opts.local && !opts.read_duplicates; + let known: std::collections::HashSet<&str> = todo + .iter() + .zip(&probed) + .filter(|(_, p)| skipped && p.as_ref().is_ok_and(|i| i.content_hash.as_ref().is_some_and(|h| self.by_hash.contains_key(h)))) + .map(|(f, _)| f.as_str()) + .collect(); + let mut found = + sidecars_of(&asked, cancel, |f| if known.contains(f) { None } else { crate::sidecar::find_sidecar_for_import(&stems, self.naming, f) }) + .into_iter(); // Files are planned one by one, in order (duplicates, sidecars, names, folders); their // copies or moves — the slow part, reading and writing every byte and verifying it — are // collected and run on several threads (issue #367), then their outcomes take their places. @@ -331,13 +340,18 @@ impl ImportJob { break; } } - // same bytes as a library photo: skipped below, once it is read as a file import reads it + // same bytes as a library photo: skipped below, once its capture time is known (and, + // when the import makes a record of it, once it is read as a file import reads it) let duplicate_of = info.content_hash.as_ref().filter(|_| !opts.local).and_then(|h| self.by_hash.get(h).map(|existing| (h.clone(), *existing))); // the XMP sidecar (or a raw's embedded XMP), read before copying: its capture time files - // and names the copy when the file itself has none + // and names the copy when the file itself has none (a skipped duplicate needs only that time) let raw = info.kind == MediaKind::Raw; - let packet = found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).or_else(|| info.xmp.clone().filter(|_| raw)); + let packet = if duplicate_of.is_some() && info.captured.is_some() && !opts.read_duplicates { + None + } else { + found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).or_else(|| info.xmp.clone().filter(|_| raw)) + }; let target = crate::crs::Target::for_file(info.kind, &info.format, info.preview_only.is_some(), &info.meta.camera); let sidecar = packet.and_then(|x| match crate::sidecar::parse_sidecar(&x, target) { Ok(sc) => Some(sc), diff --git a/crates/engine/src/import/types.rs b/crates/engine/src/import/types.rs index 2964110584..298ff98746 100644 --- a/crates/engine/src/import/types.rs +++ b/crates/engine/src/import/types.rs @@ -67,6 +67,10 @@ pub struct ImportOptions { pub metadata_preset: Option, /// Copy: raws are copied as DNG (Copy as DNG). pub convert_dng: bool, + /// A duplicate by content is read as a new file is, its XMP sidecar included: the Lightroom + /// import makes a record of its own from it ([`Duplicate::file`], #2589). Any other import + /// skips it without looking up its sidecar (a round trip per name on a network share, #2954). + pub read_duplicates: bool, /// A batch-edit recipe: develop control id / value pairs (e.g. `light.exposure` → 0.5), /// applied to every imported photo *after* its XMP sidecar, camera default and import preset, /// as one more History step (`Import adjustments`). Only the named controls change; the recipe diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index cf0b2ab679..c5621821be 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -398,6 +398,44 @@ fn a_content_duplicate_keeps_what_its_sidecar_says() { assert_eq!(new.captured.as_deref(), Some("2024-05-06T07:08:09"), "the file's own capture time: {r}"); std::fs::remove_dir_all(dir).unwrap(); } +/// Only the Lightroom import reads a content duplicate's XMP sidecar: any other import skips the +/// duplicate without looking its sidecar up (#2954), so the same file beside the same sidecar +/// reaches the report with no sidecar unless the import makes a record of it (#2589). +#[test] +fn only_an_import_that_makes_a_record_of_a_content_duplicate_reads_its_sidecar() { + let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-duplicate-skip-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("test.ARW"); + std::fs::write(&file, b"same bytes").unwrap(); + std::fs::write( + dir.join("test.xmp"), + r#" + +Harbour at dawn +"#, + ) + .unwrap(); + let paths = [file.to_string_lossy().into_owned()]; + for read_duplicates in [false, true] { + let mut s = crate::Session::new(); + let other = library_photo(&mut s, "/elsewhere/a.ARW", |p| p.meta.caption = "Other photo's caption".into()); + s.media.file_probe = + Some(Arc::new(|_| Ok(crate::media::ProbeInfo { format: "ARW".into(), content_hash: Some("same-bytes".into()), ..Default::default() }))); + let opts = crate::import::ImportOptions { read_duplicates, ..Default::default() }; + let r = crate::import::import_with(&mut s, &paths, &opts).unwrap(); + assert!(r.imported.is_empty() && s.catalog.photos().count() == 1, "{read_duplicates}: {r:?}"); + let [d] = r.duplicates.as_slice() else { panic!("{read_duplicates}: {r:?}") }; + assert_eq!((d.reason, d.existing), ("content", Some(other.0)), "{read_duplicates}"); + let caption = d.file.as_ref().and_then(|f| f.sidecar.as_ref()).and_then(|sc| sc.caption.clone()); + if read_duplicates { + assert_eq!(caption.as_deref(), Some("Harbour at dawn"), "the Lightroom import reads it: {r:?}"); + } else { + assert_eq!(caption, None, "a skipped duplicate's sidecar isn't looked up: {r:?}"); + } + } + std::fs::remove_dir_all(dir).unwrap(); +} /// A catalog photo matched (by path) to a library photo in Recently Deleted leaves it there, as /// the import was set up to do (a Lightroom import doesn't restore, see `OnDeleted`), and the report /// counts it so the photos don't go missing unexplained (#2589). A new virtual copy of it is diff --git a/crates/engine/src/lightroom_job.rs b/crates/engine/src/lightroom_job.rs index defdb850f0..7a039fe0f1 100644 --- a/crates/engine/src/lightroom_job.rs +++ b/crates/engine/src/lightroom_job.rs @@ -207,8 +207,10 @@ pub struct LightroomJob { impl LightroomJob { /// Snapshot session state only. This constructor performs no filesystem work. pub fn new(s: &mut crate::Session, path: PathBuf, update_existing: bool) -> crate::Result { - // a photo deleted here stays deleted: the catalog's record is matched, not restored - let opts = ImportOptions { mode: ImportMode::Add, on_deleted: crate::import::OnDeleted::Skip, ..ImportOptions::default() }; + // a photo deleted here stays deleted: the catalog's record is matched, not restored; a + // duplicate by content becomes a record of its own, so its file and sidecar are read (#2589) + let opts = + ImportOptions { mode: ImportMode::Add, on_deleted: crate::import::OnDeleted::Skip, read_duplicates: true, ..ImportOptions::default() }; let import = ImportJob::new(s, opts)?; let archive_dir = s.library.as_ref().filter(|library| library.on_disk).map(|library| library.dir.join("Interop")); let progress = LightroomProgress {