From c3c4734beb7b9650936b3c74df21d72a597cd94c Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 6 Aug 2023 13:52:09 +0200 Subject: transcode images --- base/src/lib.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'base/src/lib.rs') diff --git a/base/src/lib.rs b/base/src/lib.rs index 62f93e7..df80036 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -5,8 +5,8 @@ */ #![feature(lazy_cell)] use base64::Engine; -use jellycommon::config::GlobalConfig; -use std::{fs::File, path::PathBuf, str::FromStr, sync::LazyLock}; +use jellycommon::{config::GlobalConfig, AssetLocation}; +use std::{fs::File, path::PathBuf, sync::LazyLock}; pub static CONF: LazyLock = LazyLock::new(|| { serde_json::from_reader( @@ -20,7 +20,7 @@ pub static CONF: LazyLock = LazyLock::new(|| { .unwrap() }); -pub fn cache_file(seed: &[&str]) -> (PathBuf, Option) { +pub fn cache_file(seed: &[&str]) -> PathBuf { use sha2::Digest; let mut d = sha2::Sha512::new(); for s in seed { @@ -30,13 +30,19 @@ pub fn cache_file(seed: &[&str]) -> (PathBuf, Option) { 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 path = CONF.cache_path.join(fname); + path +} - let f = if !fullpath.exists() { - Some(File::create(&fullpath).unwrap()) - } else { - None - }; - (cachepath, f) +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), + } + } } -- cgit v1.2.3-70-g09d2