use crate::CONFIG;
use crate::error::Error;
use ragit_fs::{
WriteMode,
create_dir_all,
join3,
parent,
read_bytes,
write_bytes,
};
pub fn save(id: &str, blob: &[u8]) -> Result<(), Error> {
let blob_at = get_blob_path(id)?;
create_dir_all(&parent(&blob_at)?)?;
Ok(write_bytes(
&blob_at,
blob,
WriteMode::CreateOrTruncate,
)?)
}
pub fn get(id: &str) -> Result<Vec<u8>, Error> {
Ok(read_bytes(&get_blob_path(id)?)?)
}
fn get_blob_path(id: &str) -> Result<String, Error> {
let config = CONFIG.get().ok_or(Error::ConfigNotInitialized)?;
let prefix = id.get(0..2).unwrap();
let suffix = id.get(2..).unwrap();
Ok(join3(
&config.blob_data_dir,
prefix,
suffix,
)?)
}