diff options
author | metamuffin <metamuffin@disroot.org> | 2023-07-31 19:53:01 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-07-31 19:53:01 +0200 |
commit | aeafba7847e189313df3025e6d6f291999b57350 (patch) | |
tree | bf7affdca28208695648bc9b18856cbb7049d1e8 /server/src/library.rs | |
parent | 0c651f11920350a4aa96aa24f8fe15b28390aed2 (diff) | |
download | jellything-aeafba7847e189313df3025e6d6f291999b57350.tar jellything-aeafba7847e189313df3025e6d6f291999b57350.tar.bz2 jellything-aeafba7847e189313df3025e6d6f291999b57350.tar.zst |
update server to new schema
Diffstat (limited to 'server/src/library.rs')
-rw-r--r-- | server/src/library.rs | 195 |
1 files changed, 0 insertions, 195 deletions
diff --git a/server/src/library.rs b/server/src/library.rs deleted file mode 100644 index 8606a6e..0000000 --- a/server/src/library.rs +++ /dev/null @@ -1,195 +0,0 @@ -/* - This file is part of jellything (https://codeberg.org/metamuffin/jellything) - which is licensed under the GNU Affero General Public License (version 3); see /COPYING. - Copyright (C) 2023 metamuffin <metamuffin.org> -*/ -use anyhow::{anyhow, bail, Context, Ok}; -use jellycommon::{CommmonInfo, DirectoryInfo, ItemInfo}; -use log::info; -use std::{ - ffi::OsStr, - fs::File, - os::unix::prelude::OsStrExt, - path::{Path, PathBuf}, - sync::Arc, -}; - -use crate::{routes::ui::node::AssetRole, CONF}; - -impl Library { - pub fn open(path: &Path) -> anyhow::Result<Self> { - Ok(Self { - 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"))?, - }) - } - pub fn nested_path(&self, path: &Path) -> anyhow::Result<Arc<Node>> { - self.nested(path.to_str().unwrap()) - } - pub fn nested(&self, path: &str) -> anyhow::Result<Arc<Node>> { - let mut n = self.root.clone(); - if path.is_empty() { - return Ok(n); - } - for seg in path.split('/') { - n = n - .get_directory()? - .child_by_ident(seg) - .ok_or(anyhow!("does not exist"))?; - } - Ok(n) - } -} - -impl Node { - pub fn get_directory(&self) -> anyhow::Result<Arc<Directory>> { - match self { - Node::Directory(d) => Ok(d.clone()), - Node::Item(_) => bail!("not a directory"), - } - } - pub fn get_item(&self) -> anyhow::Result<Arc<Item>> { - match self { - Node::Item(i) => Ok(i.clone()), - Node::Directory(_) => bail!("not an item"), - } - } - pub fn common(&self) -> &CommmonInfo { - match self { - Node::Directory(d) => &d.info.common, - Node::Item(i) => &i.info.common, - } - } - pub fn identifier(&self) -> &str { - match self { - Node::Directory(d) => &d.identifier, - Node::Item(i) => &i.identifier, - } - } - pub fn lib_path(&self) -> &PathBuf { - match self { - Node::Directory(d) => &d.lib_path, - Node::Item(i) => &i.lib_path, - } - } - - pub fn from_path( - path: PathBuf, - mut lib_path: PathBuf, - root: bool, - ) -> 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() { - 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()); - } - - info!("scanning directory {path:?}"); - - 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 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 - .file_name() - .unwrap() - .to_str() - .unwrap() - .strip_suffix(".jelly") - .unwrap() - .to_string(); - Ok(Vec::from_iter(Some( - Node::Item(Arc::new(Item { - fs_path: path, - lib_path: lib_path.join(identifier.clone()), - info: data, - identifier, - })) - .into(), - ))) - } else { - bail!("did somebody really put a fifo or socket in the library?!") - } - } - - pub fn get_asset(&self, library: &Library, role: AssetRole) -> PathBuf { - let path = match role { - AssetRole::Backdrop => self - .common() - .backdrop - .clone() - .or_else(|| self.common().poster.clone()), - AssetRole::Poster => self.common().poster.clone(), - }; - if let Some(p) = path { - library.root_path.join(p) - } else { - CONF.asset_path.join("fallback.jpeg") - } - } -} - -impl Item { - pub fn path(&self) -> String { - self.lib_path.to_str().unwrap().to_string() - } -} - -impl Directory { - pub fn path(&self) -> String { - self.lib_path.to_str().unwrap().to_string() - } - pub fn child_by_ident(&self, i: &str) -> Option<Arc<Node>> { - self.children - .iter() - .find(|e| e.identifier() == i) - .map(|e| e.to_owned()) - } -} |