get covers from children if no images in album

This commit is contained in:
Nick Pegg 2025-05-07 16:52:33 -07:00
parent 584ec41c7a
commit 90482446b1
4 changed files with 32 additions and 17 deletions

View file

@ -25,7 +25,7 @@
<a href="{{child.name}}/"> <a href="{{child.name}}/">
<div> <div>
<img <img
src="{{child.name}}/{{child.cover_thumbnail_path}}" /> src="{{child.cover_thumbnail_path}}" />
</div> </div>
<div> <div>
{{child.name}} {{child.name}}

0
src/bin/playground.rs Normal file
View file

View file

@ -78,11 +78,16 @@ impl TryFrom<&AlbumDir> for AlbumContext {
PathBuf::new() PathBuf::new()
}; };
// Pick a cover image thumbnail and build a path to it // Make the path to the thumbnail relative to the album that we're in
// TODO: If album has no images, pick the cover from one of the albums let cover_thumbnail_path: PathBuf;
let cover_thumbnail_path = match &album.cover { if let Some(parent) = album.path.parent() {
Some(i) => Path::new("slides").join(&i.thumb_filename), cover_thumbnail_path = album.cover.thumb_path.strip_prefix(parent)?.to_path_buf()
None => PathBuf::from(""), } else {
cover_thumbnail_path = album
.cover
.thumb_path
.strip_prefix(&album.path)?
.to_path_buf()
}; };
let children: Vec<AlbumContext> = album let children: Vec<AlbumContext> = 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(); let mut all_images: Vec<&Image> = album.iter_all_images().collect();
// also resize cover image // also resize cover image
if let Some(cover) = &album.cover { all_images.push(&album.cover);
all_images.push(cover);
}
all_images.par_iter().try_for_each(|img| { all_images.par_iter().try_for_each(|img| {
let orig_image = image::open(&img.path)?; let orig_image = image::open(&img.path)?;
@ -162,14 +165,14 @@ fn generate_images(config: &Config, album: &AlbumDir) -> anyhow::Result<()> {
// image // image
// //
// TODO: Hard-link if it's supported, to save on hard drive space // 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!( log::info!(
"Copying original {} -> {}", "Copying original {} -> {}",
img.path.display(), img.path.display(),
orig_path.display() full_size_path.display()
); );
fs::create_dir_all(orig_path.parent().unwrap_or(Path::new("")))?; fs::create_dir_all(full_size_path.parent().unwrap_or(Path::new("")))?;
orig_image.save(&orig_path)?; orig_image.save(&full_size_path)?;
let thumb_path = output_path.join(&img.thumb_path); let thumb_path = output_path.join(&img.thumb_path);
log::info!( log::info!(

View file

@ -11,7 +11,7 @@ use std::slice::Iter;
pub struct AlbumDir { pub struct AlbumDir {
pub path: PathBuf, pub path: PathBuf,
pub images: Vec<Image>, pub images: Vec<Image>,
pub cover: Option<Image>, pub cover: Image,
pub children: Vec<AlbumDir>, pub children: Vec<AlbumDir>,
pub description: String, 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 // 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 { Ok(AlbumDir {
path: p.strip_prefix(root)?.to_path_buf(), path: p.strip_prefix(root)?.to_path_buf(),
images, images,