diff --git a/frontend/build.rs b/frontend/build.rs new file mode 100644 index 0000000..f40b291 --- /dev/null +++ b/frontend/build.rs @@ -0,0 +1,26 @@ +use std::fs; +use std::path::Path; +use std::io; + +fn main() { + index_default_covers().expect("index default covers"); +} + +/// Index all pngs in static/images/default_covers and expose the list in the build as an +/// environment variable DEFAULT_SONG_COVERS. This list includes the path to the images so the +/// frontend can fetch them. +fn index_default_covers() -> io::Result<()> { + let mut files = vec![]; + for dir in fs::read_dir("static/images/default_covers")? { + let path = dir?.path(); + if path.extension().and_then(|s| s.to_str()) == Some("png") { + let path = Path::new("/").join(path.strip_prefix("static").unwrap()); + files.push(path.to_string_lossy().to_string()); + } + } + + let list = files.join(","); + println!("cargo:rustc-env=DEFAULT_SONG_COVERS={list}"); + + Ok(()) +} diff --git a/frontend/src/app.rs b/frontend/src/app.rs index 2b401eb..f82e8c0 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -13,6 +13,8 @@ use seed::prelude::*; use seed::{attrs, button, div, empty, img, input, p, span, C, IF}; use std::cmp::Reverse; use std::collections::HashSet; +use std::collections::hash_map::DefaultHasher; +use std::hash::{Hash, Hasher}; use web_sys::Element; pub struct Model { @@ -40,6 +42,9 @@ pub struct Model { query_placeholder_len: usize, autotyper: Option, + + /// URLs of some defaults for songs with missing cover art. + default_song_covers: Vec<&'static str>, } #[derive(Default)] @@ -94,6 +99,10 @@ pub fn init(_url: Url, orders: &mut impl Orders) -> Model { orders.perform_cmd(fetch_songs()); orders.perform_cmd(fetch_custom_song_list_index()); + // get list of default song covers. see build.rs + const DEFAULT_SONG_COVERS: &str = env!("DEFAULT_SONG_COVERS"); + let default_song_covers = DEFAULT_SONG_COVERS.split(',').collect(); + Model { songs: vec![], custom_lists: Default::default(), @@ -105,6 +114,7 @@ pub fn init(_url: Url, orders: &mut impl Orders) -> Model { query_placeholder: String::from("Sök"), query_placeholder_len: 0, autotyper: Some(orders.perform_cmd_with_handle(timeout(500, || Msg::Autotyper))), + default_song_covers, } } @@ -228,6 +238,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders) { } pub fn view(model: &Model) -> Vec> { + let song_card = |song: &Song| -> Node { div![ C![C.song_item], @@ -235,7 +246,16 @@ pub fn view(model: &Model) -> Vec> { C![C.song_item_cover], match song.cover { Some(_) => attrs! {At::Src => format!("/images/songs/{}.png", song.song_hash)}, - None => attrs! {At::Src => "/images/default_cover.png"}, + None => { + // use a DefaultHasher to turn the song_hash string into a number we can + // use to give the song a psuedo-random default cover. + let mut hasher = DefaultHasher::new(); + song.song_hash.hash(&mut hasher); + let hash = hasher.finish() as usize; + let cover_i = hash % model.default_song_covers.len(); + let cover = model.default_song_covers[cover_i]; + attrs! { At::Src => cover } + } }, ], div![ diff --git a/frontend/static/images/default_cover.png b/frontend/static/images/default_cover.png deleted file mode 100644 index f7feff4..0000000 Binary files a/frontend/static/images/default_cover.png and /dev/null differ diff --git a/frontend/static/images/default_covers/00.png b/frontend/static/images/default_covers/00.png new file mode 100644 index 0000000..ae78996 Binary files /dev/null and b/frontend/static/images/default_covers/00.png differ diff --git a/frontend/static/images/default_covers/01.png b/frontend/static/images/default_covers/01.png new file mode 100644 index 0000000..8b8f565 Binary files /dev/null and b/frontend/static/images/default_covers/01.png differ diff --git a/frontend/static/images/default_covers/02.png b/frontend/static/images/default_covers/02.png new file mode 100644 index 0000000..90947ae Binary files /dev/null and b/frontend/static/images/default_covers/02.png differ diff --git a/frontend/static/images/default_covers/03.png b/frontend/static/images/default_covers/03.png new file mode 100644 index 0000000..c8c7978 Binary files /dev/null and b/frontend/static/images/default_covers/03.png differ diff --git a/frontend/static/images/default_covers/04.png b/frontend/static/images/default_covers/04.png new file mode 100644 index 0000000..f9162db Binary files /dev/null and b/frontend/static/images/default_covers/04.png differ diff --git a/frontend/static/images/default_covers/05.png b/frontend/static/images/default_covers/05.png new file mode 100644 index 0000000..543d23b Binary files /dev/null and b/frontend/static/images/default_covers/05.png differ diff --git a/frontend/static/images/default_covers/06.png b/frontend/static/images/default_covers/06.png new file mode 100644 index 0000000..4eb0d97 Binary files /dev/null and b/frontend/static/images/default_covers/06.png differ diff --git a/frontend/static/images/default_covers/07.png b/frontend/static/images/default_covers/07.png new file mode 100644 index 0000000..c5fb9ef Binary files /dev/null and b/frontend/static/images/default_covers/07.png differ diff --git a/frontend/static/images/default_covers/08.png b/frontend/static/images/default_covers/08.png new file mode 100644 index 0000000..710c0dc Binary files /dev/null and b/frontend/static/images/default_covers/08.png differ diff --git a/frontend/static/images/default_covers/09.png b/frontend/static/images/default_covers/09.png new file mode 100644 index 0000000..b4ce437 Binary files /dev/null and b/frontend/static/images/default_covers/09.png differ diff --git a/frontend/static/images/default_covers/10.png b/frontend/static/images/default_covers/10.png new file mode 100644 index 0000000..b6a6cdd Binary files /dev/null and b/frontend/static/images/default_covers/10.png differ diff --git a/frontend/static/images/default_covers/11.png b/frontend/static/images/default_covers/11.png new file mode 100644 index 0000000..ecc666d Binary files /dev/null and b/frontend/static/images/default_covers/11.png differ diff --git a/frontend/static/images/default_covers/12.png b/frontend/static/images/default_covers/12.png new file mode 100644 index 0000000..a8072ee Binary files /dev/null and b/frontend/static/images/default_covers/12.png differ diff --git a/frontend/static/images/default_covers/13.png b/frontend/static/images/default_covers/13.png new file mode 100644 index 0000000..3a04028 Binary files /dev/null and b/frontend/static/images/default_covers/13.png differ diff --git a/frontend/static/images/default_covers/14.png b/frontend/static/images/default_covers/14.png new file mode 100644 index 0000000..630e0f0 Binary files /dev/null and b/frontend/static/images/default_covers/14.png differ diff --git a/frontend/static/images/default_covers/15.png b/frontend/static/images/default_covers/15.png new file mode 100644 index 0000000..d341483 Binary files /dev/null and b/frontend/static/images/default_covers/15.png differ diff --git a/frontend/static/images/default_covers/16.png b/frontend/static/images/default_covers/16.png new file mode 100644 index 0000000..a3cf057 Binary files /dev/null and b/frontend/static/images/default_covers/16.png differ diff --git a/frontend/static/images/default_covers/17.png b/frontend/static/images/default_covers/17.png new file mode 100644 index 0000000..b6ecbce Binary files /dev/null and b/frontend/static/images/default_covers/17.png differ diff --git a/frontend/static/images/default_covers/18.png b/frontend/static/images/default_covers/18.png new file mode 100644 index 0000000..a34e8dc Binary files /dev/null and b/frontend/static/images/default_covers/18.png differ diff --git a/frontend/static/images/default_covers/19.png b/frontend/static/images/default_covers/19.png new file mode 100644 index 0000000..1597c4f Binary files /dev/null and b/frontend/static/images/default_covers/19.png differ diff --git a/frontend/static/images/default_covers/20.png b/frontend/static/images/default_covers/20.png new file mode 100644 index 0000000..28a93dc Binary files /dev/null and b/frontend/static/images/default_covers/20.png differ diff --git a/frontend/static/images/default_covers/21.png b/frontend/static/images/default_covers/21.png new file mode 100644 index 0000000..69ebcf6 Binary files /dev/null and b/frontend/static/images/default_covers/21.png differ diff --git a/frontend/static/images/default_covers/22.png b/frontend/static/images/default_covers/22.png new file mode 100644 index 0000000..2f5cd65 Binary files /dev/null and b/frontend/static/images/default_covers/22.png differ diff --git a/frontend/static/images/default_covers/23.png b/frontend/static/images/default_covers/23.png new file mode 100644 index 0000000..7f9c105 Binary files /dev/null and b/frontend/static/images/default_covers/23.png differ diff --git a/frontend/static/images/default_covers/24.png b/frontend/static/images/default_covers/24.png new file mode 100644 index 0000000..ec629b7 Binary files /dev/null and b/frontend/static/images/default_covers/24.png differ