use crate::{database::path::AssetPath, store::AssetStore};
use anput::bundle::DynamicBundle;
use std::{error::Error, path::PathBuf};
fn save_file_bytes(file_path: PathBuf, bytes: Vec<u8>) -> Result<DynamicBundle, Box<dyn Error>> {
std::fs::create_dir_all(file_path.parent().unwrap())?;
std::fs::write(&file_path, bytes)?;
Ok(DynamicBundle::default())
}
#[derive(Debug, Default, Clone)]
pub struct AbsoluteFileAssetStore;
impl AssetStore for AbsoluteFileAssetStore {
fn save_bytes(&self, path: AssetPath, bytes: Vec<u8>) -> Result<DynamicBundle, Box<dyn Error>> {
save_file_bytes(PathBuf::from(path.path()), bytes)
}
}
#[derive(Debug, Default, Clone)]
pub struct FileAssetStore {
pub root: PathBuf,
}
impl FileAssetStore {
pub fn with_root(mut self, root: impl Into<PathBuf>) -> Self {
self.root = root.into();
self
}
}
impl AssetStore for FileAssetStore {
fn save_bytes(&self, path: AssetPath, bytes: Vec<u8>) -> Result<DynamicBundle, Box<dyn Error>> {
let path = self.root.join(path.path());
save_file_bytes(path, bytes)
}
}