A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1use std::{fs, thread};
2
3use anyhow::Error;
4use rockbox_sys::types::tree::Entry;
5
6use crate::{http::Context, AUDIO_EXTENSIONS};
7
8pub fn update_cache(ctx: &Context, path: &str, show_hidden: bool) {
9 let fs_cache = ctx.fs_cache.clone();
10 let path = path.to_string();
11 thread::spawn(move || {
12 let rt = tokio::runtime::Runtime::new().unwrap();
13 let mut fs_cache = rt.block_on(fs_cache.lock());
14 let mut entries = vec![];
15
16 for file in fs::read_dir(&path)? {
17 let file = file?;
18
19 if file.metadata()?.is_file()
20 && !AUDIO_EXTENSIONS.iter().any(|ext| {
21 file.path()
22 .to_string_lossy()
23 .ends_with(&format!(".{}", ext))
24 })
25 {
26 continue;
27 }
28
29 if file.file_name().to_string_lossy().starts_with(".") && !show_hidden {
30 continue;
31 }
32
33 entries.push(Entry {
34 name: file.path().to_string_lossy().to_string(),
35 time_write: file
36 .metadata()?
37 .modified()?
38 .duration_since(std::time::SystemTime::UNIX_EPOCH)?
39 .as_secs() as u32,
40 attr: match file.metadata()?.is_dir() {
41 true => 0x10,
42 false => 0,
43 },
44 ..Default::default()
45 });
46 }
47
48 fs_cache.insert(path, entries.clone());
49 Ok::<(), Error>(())
50 });
51}