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/import.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/import.rs')
-rw-r--r-- | server/src/import.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/server/src/import.rs b/server/src/import.rs new file mode 100644 index 0000000..06d32c3 --- /dev/null +++ b/server/src/import.rs @@ -0,0 +1,68 @@ +/* + 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 crate::{database::Database, CONF}; +use anyhow::{bail, Context, Ok}; +use jellycommon::Node; +use log::info; +use std::{ffi::OsStr, fs::File, os::unix::prelude::OsStrExt, path::PathBuf}; + +pub fn import(db: &Database) -> anyhow::Result<()> { + info!("clearing node tree"); + db.node.clear()?; + info!("importing..."); + import_path(CONF.library_path.clone(), db).context("indexing")?; + Ok(()) +} + +pub fn import_path(path: PathBuf, db: &Database) -> anyhow::Result<Vec<String>> { + 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 + } + }); + let children = children + .map(|e| import_path(e, db)) + .collect::<anyhow::Result<Vec<_>>>()? + .into_iter() + .flatten() + .collect(); + if mpath.exists() { + let data: Node = + serde_json::from_reader(File::open(mpath).context("metadata missing")?)?; + + let identifier = path.file_name().unwrap().to_str().unwrap().to_string(); + + db.node.insert(&identifier, &data)?; + + Ok(vec![identifier]) + } else { + Ok(children) + } + } else if path.is_file() { + info!("loading item {path:?}"); + let datafile = File::open(path.clone()).context("cant load metadata")?; + let data: Node = serde_json::from_reader(datafile).context("invalid metadata")?; + let identifier = path + .file_name() + .unwrap() + .to_str() + .unwrap() + .strip_suffix(".jelly") + .unwrap() + .to_string(); + db.node.insert(&identifier, &data)?; + Ok(vec![identifier]) + } else { + bail!("did somebody really put a fifo or socket in the library?!") + } +} |