From 90482446b1ef345406217b6e3bd1b3cd99ca270a Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Wed, 7 May 2025 16:52:33 -0700 Subject: [PATCH] get covers from children if no images in album --- resources/skel/_templates/album.html | 2 +- src/bin/playground.rs | 0 src/generate.rs | 27 +++++++++++++++------------ src/generate/album_dir.rs | 20 ++++++++++++++++---- 4 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 src/bin/playground.rs diff --git a/resources/skel/_templates/album.html b/resources/skel/_templates/album.html index 8a010e7..c633d2a 100644 --- a/resources/skel/_templates/album.html +++ b/resources/skel/_templates/album.html @@ -25,7 +25,7 @@
+ src="{{child.cover_thumbnail_path}}" />
{{child.name}} diff --git a/src/bin/playground.rs b/src/bin/playground.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/generate.rs b/src/generate.rs index 2ff13c7..982e9e2 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -78,11 +78,16 @@ impl TryFrom<&AlbumDir> for AlbumContext { PathBuf::new() }; - // Pick a cover image thumbnail and build a path to it - // TODO: If album has no images, pick the cover from one of the albums - let cover_thumbnail_path = match &album.cover { - Some(i) => Path::new("slides").join(&i.thumb_filename), - None => PathBuf::from(""), + // Make the path to the thumbnail relative to the album that we're in + let cover_thumbnail_path: PathBuf; + if let Some(parent) = album.path.parent() { + cover_thumbnail_path = album.cover.thumb_path.strip_prefix(parent)?.to_path_buf() + } else { + cover_thumbnail_path = album + .cover + .thumb_path + .strip_prefix(&album.path)? + .to_path_buf() }; let children: Vec = album @@ -151,9 +156,7 @@ fn generate_images(config: &Config, album: &AlbumDir) -> anyhow::Result<()> { let mut all_images: Vec<&Image> = album.iter_all_images().collect(); // also resize cover image - if let Some(cover) = &album.cover { - all_images.push(cover); - } + all_images.push(&album.cover); all_images.par_iter().try_for_each(|img| { let orig_image = image::open(&img.path)?; @@ -162,14 +165,14 @@ fn generate_images(config: &Config, album: &AlbumDir) -> anyhow::Result<()> { // image // // TODO: Hard-link if it's supported, to save on hard drive space - let orig_path = output_path.join(&img.path); + let full_size_path = output_path.join(&img.path); log::info!( "Copying original {} -> {}", img.path.display(), - orig_path.display() + full_size_path.display() ); - fs::create_dir_all(orig_path.parent().unwrap_or(Path::new("")))?; - orig_image.save(&orig_path)?; + fs::create_dir_all(full_size_path.parent().unwrap_or(Path::new("")))?; + orig_image.save(&full_size_path)?; let thumb_path = output_path.join(&img.thumb_path); log::info!( diff --git a/src/generate/album_dir.rs b/src/generate/album_dir.rs index 5340c86..e80ebbc 100644 --- a/src/generate/album_dir.rs +++ b/src/generate/album_dir.rs @@ -11,7 +11,7 @@ use std::slice::Iter; pub struct AlbumDir { pub path: PathBuf, pub images: Vec, - pub cover: Option, + pub cover: Image, pub children: Vec, pub description: String, @@ -99,11 +99,23 @@ impl AlbumDir { } } - if cover.is_none() && !images.is_empty() { - cover = Some(images[0].clone()); - } // TODO: sort children and albums alphabetically + // Find a cover image if we didn't have an explicit one. Either the first image, or the + // first image from the first album that has a cover. + if cover.is_none() { + if !images.is_empty() { + cover = Some(images[0].clone()); + } else { + // Find a cover image from one of the children + if !children.is_empty() { + cover = Some(children[0].cover.clone()); + } + } + } + let cover = cover.ok_or(anyhow!("Could not find a cover image for {}", p.display()))?; + log::debug!("Cover for {} is {cover:?}", p.display()); + Ok(AlbumDir { path: p.strip_prefix(root)?.to_path_buf(), images,