diff options
author | metamuffin <metamuffin@disroot.org> | 2023-06-13 20:13:13 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-06-13 20:13:13 +0200 |
commit | 09a9df2267a7c777b1716589d7e793a544cdbde4 (patch) | |
tree | f4bc8308b1a3be8de2443d5956f30fbde8d37851 /server/src/library.rs | |
parent | 77274b9a2af62124293b5a8a0ec0e430fa046de8 (diff) | |
download | jellything-09a9df2267a7c777b1716589d7e793a544cdbde4.tar jellything-09a9df2267a7c777b1716589d7e793a544cdbde4.tar.bz2 jellything-09a9df2267a7c777b1716589d7e793a544cdbde4.tar.zst |
allow non directory dirs
Diffstat (limited to 'server/src/library.rs')
-rw-r--r-- | server/src/library.rs | 91 |
1 files changed, 51 insertions, 40 deletions
diff --git a/server/src/library.rs b/server/src/library.rs index e810c65..464011f 100644 --- a/server/src/library.rs +++ b/server/src/library.rs @@ -47,6 +47,8 @@ impl Library { root_path: path.to_path_buf(), root: Node::from_path(path.to_path_buf(), PathBuf::new(), true) .context("indexing root")? + .into_iter() + .next() .ok_or(anyhow!("root need directory.json"))?, }) } @@ -104,51 +106,60 @@ impl Node { path: PathBuf, mut lib_path: PathBuf, root: bool, - ) -> anyhow::Result<Option<Arc<Node>>> { + ) -> anyhow::Result<Vec<Arc<Node>>> { if path.is_dir() { let mpath = path.join("directory.json"); + let children = path.read_dir()?.filter_map(|e| { + let e = e.unwrap(); + if e.path().extension() == Some(&OsStr::from_bytes(b"jelly")) + || e.metadata().unwrap().is_dir() + { + Some(e.path()) + } else { + None + } + }); if !mpath.exists() { - return Ok(None); - } - let data: DirectoryInfo = - serde_json::from_reader(File::open(mpath).context("metadata missing")?)?; + info!("flattening {path:?}"); + Ok(children + .map(|e| Node::from_path(e, lib_path.clone(), false)) + .collect::<Result<Vec<_>, _>>()? + .into_iter() + .flatten() + .collect()) + } else { + let data: DirectoryInfo = + serde_json::from_reader(File::open(mpath).context("metadata missing")?)?; - let identifier = path.file_name().unwrap().to_str().unwrap().to_string(); - if !root { - lib_path = lib_path.join(identifier.clone()); - } + let identifier = path.file_name().unwrap().to_str().unwrap().to_string(); + if !root { + lib_path = lib_path.join(identifier.clone()); + } - info!("scanning directory {:?}", path); - let children = path - .read_dir()? - .filter_map(|e| { - let e = e.unwrap(); - if e.path().extension() == Some(&OsStr::from_bytes(b"jelly")) - || e.metadata().unwrap().is_dir() - { - Some(e.path()) - } else { - None - } - }) - .filter_map(|e| { - Node::from_path(e.clone(), lib_path.clone(), false) - .context(format!("loading {e:?}")) - .transpose() - }) - .collect::<anyhow::Result<Vec<_>>>()?; + info!("scanning directory {path:?}"); - Ok(Some( - Node::Directory(Arc::new(Directory { - lib_path, - children, - info: data, - identifier, - })) - .into(), - )) + let children = children + .map(|e| { + Node::from_path(e.clone(), lib_path.clone(), false) + .context(format!("loading {e:?}")) + }) + .collect::<anyhow::Result<Vec<_>>>()? + .into_iter() + .flatten() + .collect(); + + Ok(Vec::from_iter(Some( + Node::Directory(Arc::new(Directory { + lib_path, + children, + info: data, + identifier, + })) + .into(), + ))) + } } else if path.is_file() { - info!("loading {path:?}"); + info!("loading item {path:?}"); let datafile = File::open(path.clone()).context("cant load metadata")?; let data: ItemInfo = serde_json::from_reader(datafile).context("invalid metadata")?; let identifier = path @@ -159,7 +170,7 @@ impl Node { .strip_suffix(".jelly") .unwrap() .to_string(); - Ok(Some( + Ok(Vec::from_iter(Some( Node::Item(Arc::new(Item { fs_path: path, lib_path: lib_path.join(identifier.clone()), @@ -167,7 +178,7 @@ impl Node { identifier, })) .into(), - )) + ))) } else { bail!("did somebody really put a fifo or socket in the library?!") } |