From 8551bf2e34d9543fa41a83fae785ed81d6a6c10f Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 6 Aug 2023 12:52:42 +0200 Subject: move shared server code to own crate --- base/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 base/src/lib.rs (limited to 'base/src') diff --git a/base/src/lib.rs b/base/src/lib.rs new file mode 100644 index 0000000..62f93e7 --- /dev/null +++ b/base/src/lib.rs @@ -0,0 +1,42 @@ +/* + 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 +*/ +#![feature(lazy_cell)] +use base64::Engine; +use jellycommon::config::GlobalConfig; +use std::{fs::File, path::PathBuf, str::FromStr, sync::LazyLock}; + +pub static CONF: LazyLock = LazyLock::new(|| { + serde_json::from_reader( + File::open( + std::env::args() + .nth(1) + .expect("First argument must specify the config.json to use."), + ) + .unwrap(), + ) + .unwrap() +}); + +pub fn cache_file(seed: &[&str]) -> (PathBuf, Option) { + use sha2::Digest; + let mut d = sha2::Sha512::new(); + for s in seed { + d.update(s.as_bytes()); + d.update(b"\0"); + } + let d = d.finalize(); + let fname = base64::engine::general_purpose::URL_SAFE.encode(d); + let fname = &fname[..22]; // about 128 bits + let fullpath = CONF.cache_path.join(fname); + let cachepath = PathBuf::from_str(fname).unwrap(); + + let f = if !fullpath.exists() { + Some(File::create(&fullpath).unwrap()) + } else { + None + }; + (cachepath, f) +} -- cgit v1.2.3-70-g09d2