/* 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, AssetLocation}; use std::{fs::File, path::PathBuf, 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 { 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 path = CONF.cache_path.join(fname); path } pub trait AssetLocationExt { fn path(&self) -> PathBuf; } impl AssetLocationExt for AssetLocation { fn path(&self) -> PathBuf { match self { AssetLocation::Assets(p) => CONF.asset_path.join(p), AssetLocation::Cache(p) => CONF.cache_path.join(p), AssetLocation::Library(p) => CONF.library_path.join(p), } } }