diff options
| author | tslil <tslil@posteo.de> | 2026-07-14 12:20:44 +0100 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-07-14 13:02:51 +0100 |
| commit | 156cbfe2b0383521f540109682d98d35c88d65f8 (patch) | |
| tree | e4607f60c1c58ad566d2d86488a601d58eee4220 /src/albums.rs | |
| parent | 861fb134ad6d77faeb23e9571a31e0c6031d7374 (diff) | |
don't cache album list, use compression only on json for ease of inspection
Diffstat (limited to 'src/albums.rs')
| -rw-r--r-- | src/albums.rs | 56 |
1 files changed, 56 insertions, 0 deletions
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<String> { + 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<String> = 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}"))) +} |
