Files
lightcraft/crates/engine/src/cmd/sync.rs
T
Nathaniel LedfordandClaude Opus 5.5 1259506edd Synchronize Folder: an absent folder is offline, disks only on request (review fix)
A folder that isn't there (moved whole, or on a disk that isn't
connected) used to scan as "every photo missing", one tick away from
moving a whole disk to Recently Deleted. The scan (on its worker, so an
asleep share never stalls the UI) now reports it as offline and both
commands refuse it; the dialog says so. A whole disk or a folder holding
disks needs `disk: true`, as Remove Disk from Library does; the disk rows
pass it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:08:36 -04:00

59 lines
2.9 KiB
Rust

//! Synchronize Folder commands (see [`crate::sync`]).
use serde_json::{Value, json};
use super::{CommandSpec, always, bad, bool_or, cmd, str_param};
use crate::sync::{SyncChoice, scan, synchronize};
use crate::{Result, Session};
fn path(p: &Value, c: &str) -> Result<String> {
str_param(p, "path").filter(|d| !d.trim().is_empty()).map(str::to_string).ok_or_else(|| bad(c, "missing `path`"))
}
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
query "folder.scanChanges",
"Scan Folder for Changes",
[],
None,
"{path, disk?: bool (a whole disk or share, or a folder holding disks)} — compare a folder of the library (and the folders inside it) with the disk; changes nothing → {path, new: [{path, name, format, …}] files the library doesn't have, duplicates: files whose content it already has, unreadable: [{path, error}], missing: [{id, path}] photos whose file is gone, metadata: [{id, path}] photos whose XMP sidecar was saved after they came in or were last edited here and says something the library doesn't}",
always,
|s, p| {
const C: &str = "folder.scanChanges";
let path = path(p, C)?;
let changes = scan(s, &path, bool_or(p, "disk", false)).map_err(|e| bad(C, e.to_string()))?;
let v = serde_json::to_value(&changes).unwrap_or_default();
s.folder_changes = Some(changes);
Ok(v)
}
),
cmd!(
"folder.synchronize",
"Synchronize Folder",
[],
None,
"{path, disk?: bool, importNew?: true, removeMissing?: false, readMetadata?: false} — bring a folder of the library up to date with the disk (see folder.scanChanges): import its new files in place, move photos whose file is gone to Recently Deleted, read XMP sidecars saved by other apps (the sidecar wins); acts on the last folder.scanChanges of the folder while nothing changed in the library since (else scans again); one undo step, no file is touched → {imported, removed, read, failed: [[path, error]]}",
always,
sync
),
]
}
fn sync(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "folder.synchronize";
let path = path(p, C)?;
let d = SyncChoice::default();
let choice = SyncChoice {
import_new: bool_or(p, "importNew", d.import_new),
remove_missing: bool_or(p, "removeMissing", d.remove_missing),
read_metadata: bool_or(p, "readMetadata", d.read_metadata),
};
let changes = match s.take_folder_changes(&path) {
Some(c) => c,
None => scan(s, &path, bool_or(p, "disk", false)).map_err(|e| bad(C, e.to_string()))?,
};
let r = synchronize(s, changes, choice)?;
Ok(json!({"imported": r.imported, "removed": r.removed, "read": r.read, "failed": r.failed}))
}