mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-02 02:46:10 -06:00
security: importers and preset application index instead of rescanning
* security: importers and preset application index instead of rescanning * address review: keep first match when indexing names, skip used mask ids, move presets instead of cloning * address review: a dry-run import keeps its rows, not the parsed presets * merge main into fix/sec-importers-indexed * address review: key duplicate presets by the settings value, so 0.0 and -0.0 are one preset * preset import index: key numbers as f64 so 0 and 0.0 cannot collide
This commit is contained in:
@@ -455,9 +455,16 @@ fn indexed_field<'a>(s: &'a mut DevelopSettings, id: &str) -> Option<&'a mut f64
|
||||
}
|
||||
|
||||
fn indexed_get(s: &DevelopSettings, id: &str) -> Option<f64> {
|
||||
// (a clone keeps one accessor for get and set; these lists are tiny)
|
||||
let mut c = DevelopSettings { point_colors: s.point_colors.clone(), red_eye: s.red_eye.clone(), ..DevelopSettings::default() };
|
||||
indexed_field(&mut c, id).map(|v| *v)
|
||||
// one accessor for get and set: a copy of the one element read (never of the lists, which a
|
||||
// read of every indexed control would copy once per control)
|
||||
let (fam, i, field) = split_indexed(id)?;
|
||||
let mut c = DevelopSettings::default();
|
||||
match fam {
|
||||
"pointColor" => c.point_colors.push(*s.point_colors.get(i)?),
|
||||
"redEye" => c.red_eye.push(*s.red_eye.get(i)?),
|
||||
_ => return None,
|
||||
}
|
||||
indexed_field(&mut c, &format!("{fam}.0.{field}")).map(|v| *v)
|
||||
}
|
||||
|
||||
fn indexed_set(s: &mut DevelopSettings, id: &str, v: f64) -> bool {
|
||||
|
||||
@@ -313,13 +313,28 @@ impl Preset {
|
||||
/// The preset's masks, added to the photo's own (ones it already has are skipped), their effect
|
||||
/// scaled by the amount.
|
||||
fn add_masks(out: &mut DevelopSettings, masks: &[Value], amount: f64) {
|
||||
// the masks by name, kept up to date: a preset of many masks compares each with its
|
||||
// namesakes only, not with every mask added before it
|
||||
let mut by_name: std::collections::HashMap<String, Vec<usize>> = std::collections::HashMap::new();
|
||||
for (i, q) in out.masks.iter().enumerate() {
|
||||
by_name.entry(q.name.clone()).or_default().push(i);
|
||||
}
|
||||
let mut next_id = out.next_mask_id();
|
||||
let mut used: std::collections::HashSet<u32> = out.masks.iter().map(|q| q.id).collect();
|
||||
for m in masks {
|
||||
let Ok(mut m) = serde_json::from_value::<crate::Mask>(m.clone()) else { continue };
|
||||
if out.masks.iter().any(|q| q.name == m.name && q.components == m.components) {
|
||||
if by_name.get(&m.name).is_some_and(|ix| ix.iter().any(|&i| out.masks.get(i).is_some_and(|q| q.components == m.components))) {
|
||||
continue;
|
||||
}
|
||||
m.id = out.next_mask_id();
|
||||
// ids run on from the first free one, past any a mask already has (a scan for the
|
||||
// next free id per mask would be another pass over them)
|
||||
while used.contains(&next_id) {
|
||||
next_id = next_id.checked_add(1).unwrap_or(1);
|
||||
}
|
||||
m.id = next_id;
|
||||
used.insert(next_id);
|
||||
m.adjust.amount = (m.adjust.amount * amount).clamp(0.0, 200.0);
|
||||
by_name.entry(m.name.clone()).or_default().push(out.masks.len());
|
||||
out.masks.push(m);
|
||||
}
|
||||
}
|
||||
@@ -604,4 +619,22 @@ mod tests {
|
||||
ids.dedup();
|
||||
assert_eq!(ids.len(), 3, "{:?}", out.masks);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preset_masks_skip_ids_an_existing_mask_has_after_the_first_free_one() {
|
||||
// the first free id is 1, and 2 is taken: the second added mask must not get 2
|
||||
let theirs = |id| crate::Mask { id, name: format!("T{id}"), ..Default::default() };
|
||||
let s = DevelopSettings { masks: vec![theirs(u32::MAX), theirs(2)], ..Default::default() };
|
||||
let p = Preset {
|
||||
id: "p".into(),
|
||||
name: "P".into(),
|
||||
group: "User".into(),
|
||||
settings: json!({"masks": [{"name": "A"}, {"name": "B"}]}),
|
||||
favorite: false,
|
||||
builtin: false,
|
||||
};
|
||||
let mut ids: Vec<u32> = p.apply(&s, 1.0).masks.iter().map(|m| m.id).collect();
|
||||
ids.sort_unstable();
|
||||
assert_eq!(ids, [1, 2, 3, u32::MAX]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user