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
14 changes: 10 additions & 4 deletions crates/engine/src/cmd/library/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,16 +1325,22 @@ pub fn specs() -> Vec<CommandSpec> {
"Review Import",
[],
None,
"{paths: [file or folder (recursive)]} → {candidates: [{path, name, format, kind, width, height, fileSize, captured, duplicate?: path|content, existing?, error?, previewOnly?: why a raw can only be shown from its embedded preview}], duplicates, scanned} — nothing is added",
"{paths: [file or folder (recursive)]} → {candidates: [{path, name, format, kind, width, height, fileSize, captured, duplicate?: path|content, existing?, error?, previewOnly?: why a raw can only be shown from its embedded preview}], duplicates, scanned, scanLimited: bool (the folder walk stopped at its scan limit: there may be more photos), skipped: {unreadable (folders that could not be listed), unsupported (files in the folders that are not a supported photo type, XMP sidecars not counted; a file named in paths is always tried)}} — nothing is added",
always,
|s, p| {
let paths = strs(p, "paths");
if paths.is_empty() {
return Err(bad("library.importPreview", "no paths"));
}
let c = crate::import::scan(s, &paths);
let dups = c.iter().filter(|c| c.duplicate.is_some()).count();
Ok(json!({"scanned": c.len(), "duplicates": dups, "candidates": c}))
let out = crate::import::scan_report(s, &paths);
let dups = out.candidates.iter().filter(|c| c.duplicate.is_some()).count();
Ok(json!({
"scanned": out.candidates.len(),
"duplicates": dups,
"candidates": out.candidates,
"scanLimited": out.truncated,
"skipped": {"unreadable": out.unreadable, "unsupported": out.unsupported},
}))
}
),
cmd!(
Expand Down
6 changes: 4 additions & 2 deletions crates/engine/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ pub(crate) use commit::workers;
pub use commit::{batch_size, civil, commit_prepared, system_clock};
pub use defaults::{CameraDefault, ImportDefaults, apply_import_defaults, has_import_look, import_defaults};
pub use files::{
EXTENSIONS, LAUNCH_PROBE_LIMIT, LaunchPath, accept_list, expand, expand_within, is_supported, launch_deadline, launch_path, launch_path_until,
EXTENSIONS, Expansion, LAUNCH_PROBE_LIMIT, LaunchPath, accept_list, expand, expand_report, expand_within, is_supported, launch_deadline,
launch_path, launch_path_until,
};
pub(crate) use files::{expand_noting, probe_all, probe_paths, probe_with};
#[cfg(test)]
use job::PreparedItem;
pub use job::{ImportJob, Prepared, candidate_thumb_key, import, import_with};
pub(crate) use scan::scan_files;
pub use scan::{
ScanInput, ScanOutput, ScanProgress, browse_files, browse_order, browse_path, browse_queue, browse_queue_until, browse_spelling, scan, scan_with,
ScanInput, ScanOutput, ScanProgress, browse_files, browse_order, browse_path, browse_queue, browse_queue_until, browse_spelling, scan,
scan_report, scan_with,
};
#[cfg(test)]
use scan::{library_files, library_hashes};
Expand Down
65 changes: 55 additions & 10 deletions crates/engine/src/import/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub fn is_supported(path: &Path) -> bool {
path.extension().is_some_and(|e| EXTENSIONS.contains(&e.to_string_lossy().to_lowercase().as_str()))
}

/// An XMP sidecar (`.xmp`, any letter case): read with its photo on import, never a photo itself.
fn is_xmp_sidecar(path: &Path) -> bool {
path.extension().is_some_and(|e| e.eq_ignore_ascii_case("xmp"))
}

/// The web file picker's accept list, derived from [`EXTENSIONS`]. Each extension
/// is listed in both cases: browsers' `accept` matching is case-insensitive per
/// spec, but some implementations (e.g. Windows/Edge) match case-sensitively and
Expand Down Expand Up @@ -78,19 +83,57 @@ pub(crate) fn expand_noting(
paths: &[String],
skip: Option<&Path>,
limits: crate::walk::Limits,
mut note: Option<crate::walk::NoteFolder>,
note: Option<crate::walk::NoteFolder>,
) -> (Vec<String>, bool) {
let mut out = Vec::new();
let mut truncated = false;
let e = expand_report_noting(paths, skip, limits, note);
(e.files, e.truncated)
}

/// What expanding import sources found, and what the walks left out ([`expand_report`]), so a
/// review can say that it did not see everything.
#[derive(Clone, Debug, Default)]
pub struct Expansion {
/// Supported files, folders walked recursively and deduplicated by path, in walk order.
pub files: Vec<String>,
/// A walk stopped at a depth or entry bound: there may be more files.
pub truncated: bool,
/// Folders whose contents couldn't be listed (permissions, a folder that vanished or went offline).
pub unreadable: usize,
/// Files found inside the folders walked that are not a supported type (see [`is_supported`]),
/// not counting XMP sidecars, which the import reads with their photos. A file named directly
/// is attempted whatever its extension and is never counted here. Both counts add up over the
/// sources, so a folder named twice (or inside another source) counts twice.
pub unsupported: usize,
}

/// [`expand_within`] keeping the walks' diagnostics. A walk that stops at a bound is logged here
/// (once per folder), as it always was.
pub fn expand_report(paths: &[String], skip: Option<&Path>, limits: crate::walk::Limits) -> Expansion {
expand_report_noting(paths, skip, limits, None)
}

/// ([`expand_report`], handing the listing of every folder that is walked to `note`, as
/// [`expand_noting`] does.)
fn expand_report_noting(paths: &[String], skip: Option<&Path>, limits: crate::walk::Limits, mut note: Option<crate::walk::NoteFolder>) -> Expansion {
let mut out = Expansion::default();
for p in paths {
let p = Path::new(p);
if skip.is_some_and(|s| p == s) {
continue;
}
if p.is_dir() {
// XMP sidecars are read with their photos, not skipped: they are left out of the count
let mut sidecars = 0usize;
let keep = |f: &Path| {
let keep = is_supported(f);
if !keep && is_xmp_sidecar(f) {
sidecars = sidecars.saturating_add(1);
}
keep
};
let w = match note.as_mut() {
Some(note) => crate::walk::files_in_noting(p, skip, limits, is_supported, &mut **note),
None => crate::walk::files_in(p, skip, limits, is_supported),
Some(note) => crate::walk::files_in_noting(p, skip, limits, keep, &mut **note),
None => crate::walk::files_in(p, skip, limits, keep),
};
if w.truncated {
log::warn!(
Expand All @@ -101,16 +144,18 @@ pub(crate) fn expand_noting(
limits.max_depth
);
}
truncated |= w.truncated;
out.extend(w.files.into_iter().map(|f| f.to_string_lossy().to_string()));
out.truncated |= w.truncated;
out.unreadable = out.unreadable.saturating_add(w.unreadable);
out.unsupported = out.unsupported.saturating_add(w.skipped.saturating_sub(sidecars));
out.files.extend(w.files.into_iter().map(|f| f.to_string_lossy().to_string()));
} else {
// explicitly named files are attempted even with an unknown extension (sniffed)
out.push(p.to_string_lossy().to_string());
out.files.push(p.to_string_lossy().to_string());
}
}
let mut seen = std::collections::HashSet::new();
out.retain(|p| seen.insert(p.clone()));
(out, truncated)
out.files.retain(|p| seen.insert(p.clone()));
out
}

/// What a path given on the command line at launch becomes ([`launch_path`]).
Expand Down
35 changes: 27 additions & 8 deletions crates/engine/src/import/scan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::files::{Seen, expand, is_supported, probe_all, reusable_probes};
use super::files::{Seen, expand, expand_report, is_supported, probe_all, reusable_probes};
use super::types::ImportCandidate;
use super::{HashMap, Path, PathBuf, PhotoId, ProbeInfo, Session, Source};

Expand Down Expand Up @@ -77,6 +77,12 @@ pub struct ScanProgress {
pub struct ScanOutput {
pub candidates: Vec<ImportCandidate>,
pub probes: HashMap<String, ProbeInfo>,
/// A folder walk stopped at a depth or entry bound: there may be more photos ([`Expansion`]).
pub truncated: bool,
/// Folders whose contents couldn't be listed.
pub unreadable: usize,
/// Files inside the folders that are not a supported photo type (XMP sidecars not counted).
pub unsupported: usize,
}

impl ScanInput {
Expand Down Expand Up @@ -283,20 +289,32 @@ fn browse_order_until(files: &mut [String], sort: &lightcraft_catalog::Sort, can
/// against the library and earlier candidates). Nothing is added; the probes are kept for the
/// import that follows.
pub fn scan(s: &mut Session, paths: &[String]) -> Vec<ImportCandidate> {
scan_report(s, paths).candidates
}

/// [`scan`] keeping what the walks left out. The probes are already kept in the session for the
/// import that follows, so `probes` of the output is empty.
pub fn scan_report(s: &mut Session, paths: &[String]) -> ScanOutput {
let (input, paths) = ScanInput::new(s, paths);
let out = scan_with(input, &paths, &ScanProgress::default());
s.import_probes = out.probes;
out.candidates
let mut out = scan_with(input, &paths, &ScanProgress::default());
s.import_probes = std::mem::take(&mut out.probes);
out
}

/// [`scan`] without the session, so it can run on a worker thread. Stops early (returning what it
/// has) when `progress.cancel` is set.
pub fn scan_with(input: ScanInput, paths: &[String], progress: &ScanProgress) -> ScanOutput {
let files = expand(paths, input.skip.as_deref());
scan_files(input, files, progress)
let expansion = expand_report(paths, input.skip.as_deref(), crate::walk::Limits::default());
let mut out = scan_files(input, expansion.files, progress);
out.truncated = expansion.truncated;
out.unreadable = expansion.unreadable;
out.unsupported = expansion.unsupported;
out
}

/// [`scan_with`] for files that were found already (`paths` expanded, [`expand`]).
/// [`scan_with`] for files that were found already (`paths` expanded, [`expand`]). The walks'
/// diagnostics ([`ScanOutput::truncated`] and friends) are left empty: who expanded the paths has
/// them ([`expand_report`]).
pub(crate) fn scan_files(mut input: ScanInput, files: Vec<String>, progress: &ScanProgress) -> ScanOutput {
use std::sync::atomic::Ordering::Relaxed;
let by_path = library_files(&input.library, &files);
Expand Down Expand Up @@ -357,5 +375,6 @@ pub(crate) fn scan_files(mut input: ScanInput, files: Vec<String>, progress: &Sc
}
out.push(c);
}
ScanOutput { candidates: out, probes: kept }
// (the walks' diagnostics are [`scan_with`]'s: these files were found already)
ScanOutput { candidates: out, probes: kept, truncated: false, unreadable: 0, unsupported: 0 }
}
Loading
Loading