blob: 62f93e72079413971db477c1488b98571cdfe8e2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 <metamuffin.org>
*/
#![feature(lazy_cell)]
use base64::Engine;
use jellycommon::config::GlobalConfig;
use std::{fs::File, path::PathBuf, str::FromStr, sync::LazyLock};
pub static CONF: LazyLock<GlobalConfig> = 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<File>) {
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)
}
|