Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/engine/src/cmd/library/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub fn import_params(s: &Session, p: &Value) -> Result<ImportRequest> {
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);
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod job;
mod scan;
mod types;

pub(crate) use commit::start_from_file;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use commit::workers;
pub use commit::{batch_size, civil, commit_prepared, system_clock};
Expand Down
31 changes: 21 additions & 10 deletions crates/engine/src/import/commit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::defaults::apply_import_defaults;
use super::files::copy_into;
use super::job::{Prepared, PreparedItem, ReadyFile};
use super::types::{Duplicate, ImportOptions, ImportReport, Kept, Moved, OnDeleted};
use super::types::{Duplicate, FileFacts, ImportOptions, ImportReport, Kept, Moved, OnDeleted};
use super::{HashMap, Op, Path, PathBuf, Photo, PhotoId, ProbeInfo, Session, Source};

/// A batch's items in file order while its transfers are pending ([`ImportJob::run_plan`]).
Expand Down Expand Up @@ -181,6 +181,22 @@ fn trashed_file_is_gone(cat: &lightcraft_catalog::Catalog, id: PhotoId) -> bool
}
}

/// What a new record takes from its file, as a file import gives it: the file's metadata and
/// capture time, the import defaults, then its XMP sidecar. Returns the sidecar when it said
/// anything (labels read through the library's label names).
pub(crate) fn start_from_file(s: &Session, p: &mut Photo, file: FileFacts, now: &str) -> Option<crate::sidecar::SidecarData> {
let FileFacts { meta, captured, orientation, sidecar } = file;
p.meta = meta;
p.captured = captured;
apply_import_defaults(s, p);
let sc = sidecar.map(|sc| sc.resolve_label(&s.catalog)).filter(|sc| *sc != crate::sidecar::SidecarData::default())?;
// the photo is upright as the file's own orientation turns it, when the file states one
// (after the check above: an orientation alone isn't a sidecar that said something)
let sc = crate::sidecar::SidecarData { orientation: orientation.or(sc.orientation), ..sc };
crate::sidecar::merge_into(p, &sc, now);
Some(sc)
}

/// The catalog half of an import: add the photos [`ImportJob::prepare`] readied (one undoable op;
/// a Move's sources are removed once it is saved). On a failed commit a Move's placed files are
/// taken back.
Expand Down Expand Up @@ -252,25 +268,20 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare
purge.push(*old);
}
}
let sidecar = sidecar.map(|sc| sc.resolve_label(&s.catalog)).filter(|sc| *sc != crate::sidecar::SidecarData::default());
// the photo is upright as the file's own orientation turns it, when the file states one
let sidecar = sidecar.map(|sc| crate::sidecar::SidecarData { orientation: info.orientation.or(sc.orientation), ..sc });
// a copy is catalogued under its new name
let name = Path::new(&stored).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.clone());
let mut p = Photo::new(id, Source::File { path: stored }, &name, &info.format, info.width, info.height, &now);
p.kind = info.kind;
p.file_size = info.file_size;
p.captured = info.captured;
p.meta = info.meta;
p.as_shot_wb = info.as_shot_wb;
p.content_hash = info.content_hash;
p.embedded_lens = info.embedded_lens;
p.set_preview_only(info.preview_only.clone());
apply_import_defaults(s, &mut p);
if let Some(sc) = &sidecar {
crate::sidecar::merge_into(&mut p, sc, &now);
if let Some(sc) =
start_from_file(s, &mut p, FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar }, &now)
{
report.sidecars += 1;
report.warnings.extend(sc.warnings.iter().cloned().map(|mut warning| {
report.warnings.extend(sc.warnings.into_iter().map(|mut warning| {
warning.path = path.clone();
warning
}));
Expand Down
17 changes: 10 additions & 7 deletions crates/engine/src/import/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Prepared {
existing: Some(existing),
reason: if existing_path.is_some() { "path" } else { "content" },
hash: info.content_hash,
file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured })),
file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar })),
});
} else {
items.push(PreparedItem::Ready(Box::new(ReadyFile { path, stored, info, sidecar, placed })));
Expand Down Expand Up @@ -299,11 +299,13 @@ impl ImportJob {
// 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.
let asked: Vec<&str> = todo.iter().zip(&probed).filter(|(_, p)| p.is_ok()).map(|(f, _)| f.as_str()).collect();
// (a file whose content the library has is skipped below as a duplicate: it asks the disk nothing)
// (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)| !opts.local && p.as_ref().is_ok_and(|i| i.content_hash.as_ref().is_some_and(|h| self.by_hash.contains_key(h))))
.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 =
Expand Down Expand Up @@ -338,13 +340,14 @@ impl ImportJob {
break;
}
}
// same bytes as a library photo: skipped below, once its capture time is known
// 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 (a duplicate needs only that time)
// 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 = if duplicate_of.is_some() && info.captured.is_some() {
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))
Expand All @@ -371,7 +374,7 @@ impl ImportJob {
existing,
reason: "content",
hash: Some(hash),
file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured })),
file: Some(Box::new(FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar })),
}));
continue;
}
Expand Down
9 changes: 9 additions & 0 deletions crates/engine/src/import/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub struct ImportOptions {
pub metadata_preset: Option<String>,
/// 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
Expand Down Expand Up @@ -171,6 +175,11 @@ pub struct FileFacts {
/// The capture time a file import gives it: the file's, else its XMP sidecar's, else the
/// file's modification time.
pub captured: Option<String>,
/// The file's own orientation, when it states one ([`crate::media::ProbeInfo::orientation`]): it turns what its
/// sidecar's regions were drawn on, the photo as stored, upright (#2674).
pub orientation: Option<lightcraft_geom::Orientation>,
/// Its XMP sidecar (or a raw's embedded XMP), which a file import merges into the record.
pub sidecar: Option<crate::sidecar::SidecarData>,
}

#[derive(Clone, Debug, Default, Serialize)]
Expand Down
36 changes: 19 additions & 17 deletions crates/engine/src/lightroom_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,8 +1255,8 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co
// The new record takes only what the bytes determine from the photo with the same
// bytes, never that photo's own state: not its Recently Deleted (#2589), virtual copy,
// edits, history, versions, ratings, capture time or the metadata a person writes. Its
// metadata and capture time are what its own file gives it, as a file import reads them
// (copyright, creator…); the catalog's capture time, when it has one, applies on top.
// metadata and capture time are what its own file and XMP sidecar give it, as a file
// import reads them (copyright, caption, creator…); the catalog's apply on top.
let name = Path::new(&src.path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| src.path.clone());
let mut p = Photo::new(
s.catalog.alloc_photo_id(),
Expand All @@ -1275,25 +1275,27 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co
p.embedded_lens = duplicate.embedded_lens;
p.preview_only = duplicate.preview_only.clone();
p.preview_checked = duplicate.preview_checked.clone();
if let Some(file) = file {
p.meta = file.meta.clone();
p.captured = file.captured.clone();
} else {
let file = file.cloned().unwrap_or_else(|| {
// (the file wasn't read: only the exposure description, which no command edits;
// not GPS, which placing a photo on the map does, nor the capture time, which
// Edit Capture Time does)
let m = &duplicate.meta;
p.meta.camera = m.camera.clone();
p.meta.lens = m.lens.clone();
p.meta.focal_mm = m.focal_mm;
p.meta.aperture = m.aperture;
p.meta.shutter = m.shutter.clone();
p.meta.iso = m.iso;
p.meta.focus = m.focus.clone();
}
// the settings a file import gives it (as-shot white balance, embedded lens
// corrections, the default preset); the catalog's settings apply on top
crate::import::apply_import_defaults(s, &mut p);
let meta = lightcraft_catalog::Meta {
camera: m.camera.clone(),
lens: m.lens.clone(),
focal_mm: m.focal_mm,
aperture: m.aperture,
shutter: m.shutter.clone(),
iso: m.iso,
focus: m.focus.clone(),
..Default::default()
};
crate::import::FileFacts { meta, ..Default::default() }
});
// as a file import of it: its file's metadata and capture time, the import defaults
// (as-shot white balance, embedded lens corrections, the default preset), its XMP
// sidecar; the catalog's settings and metadata apply on top
crate::import::start_from_file(s, &mut p, file, &now);
p
} else {
let id = s.catalog.alloc_photo_id();
Expand Down
Loading
Loading