From 156cbfe2b0383521f540109682d98d35c88d65f8 Mon Sep 17 00:00:00 2001 From: tslil Date: Tue, 14 Jul 2026 12:20:44 +0100 Subject: don't cache album list, use compression only on json for ease of inspection --- src/albums.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/albums.rs (limited to 'src/albums.rs') diff --git a/src/albums.rs b/src/albums.rs new file mode 100644 index 0000000..17966ff --- /dev/null +++ b/src/albums.rs @@ -0,0 +1,56 @@ +use std::collections::HashSet; +use std::fs; + +use expanduser::expanduser; +use walkdir::WalkDir; + +use crate::config::Config; + +pub fn load(config: &Config) -> HashSet { + let root = expanduser(&config.music_root).ok().filter(|p| p.is_dir()); + let Some(root_path) = root else { + return HashSet::new(); + }; + + let mut new_albums: HashSet = HashSet::new(); + + for entry in WalkDir::new(&root_path) { + let entry = match entry { + Ok(e) if e.path().is_dir() => e, + _ => continue, + }; + + if !contains_audio(&entry, config) { + continue; + } + + if let Some(rel_str) = entry + .path() + .strip_prefix(&root_path) + .ok() + .and_then(|p| p.to_str()) + { + new_albums.insert(rel_str.to_string()); + } + } + + new_albums +} + +fn contains_audio(entry: &walkdir::DirEntry, config: &Config) -> bool { + fs::read_dir(entry.path()).ok().is_some_and(|mut entries| { + entries.any(|e| { + e.as_ref() + .is_ok_and(|entry| is_audio_file(&entry.path(), config)) + }) + }) +} + +fn is_audio_file(path: &std::path::Path, config: &Config) -> bool { + !path.is_dir() + && path + .extension() + .and_then(|ext| ext.to_str()) + .map(str::to_lowercase) + .is_some_and(|ext| config.audio_exts.contains(&format!(".{ext}"))) +} -- cgit v1.2.3