From 1539255ae02760e18de4efbf61dd2a338b66b322 Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Wed, 17 Mar 2021 22:30:27 -0400 Subject: [PATCH] Remove and replace deleted or moved thumbnails --- src/main.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 09a40fd..fc2b790 100644 --- a/src/main.rs +++ b/src/main.rs @@ -326,7 +326,30 @@ fn main() -> std::io::Result<()> { let ev = wrx.recv(); if let Ok(event) = ev { match event { - DebouncedEvent::Create(_) | DebouncedEvent::Rename(_, _) => create_thumbnails(), + DebouncedEvent::Create(_) => create_thumbnails(), + DebouncedEvent::Rename(orig, _) | DebouncedEvent::Write(orig) => { + let image_base_path = PathBuf::from(env::var("BASE_PATH").unwrap()); + let image_relative = orig.strip_prefix(&image_base_path).unwrap(); + if let Ok(old_thumbnail) = env::var("THUMBNAILS") + .map(|base| PathBuf::from(base)) + .map(|mut base| { + base.push(image_relative); + base + }) + { + if let Err(e) = std::fs::remove_file(&old_thumbnail) { + error!( + "Error removing thumbnail: {}\n{}", + old_thumbnail.display(), + e + ); + } else { + info!("Deleted moved thumbnail: {}", old_thumbnail.display()); + + create_thumbnails(); + } + } + } _ => continue, }; }