Skip to content

Commit

Permalink
log where we cache the downloaded assets
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 29, 2023
1 parent e6f3964 commit f498d58
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/http_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,25 @@ use std::{

/// A custom asset reader implementation that wraps a given asset reader implementation
struct HttpAssetReader {
base_url: String,
pub base_url: String,
/// Whether to load tiles from this path
tile: bool,
pub tile: bool,
/// Used to ensure the same asset doesn't get its cache file written twice at the same time,
/// as that depends on the OS whether it succeeds (could result in broken cache files).
sync: Arc<RwLock<HashSet<PathBuf>>>,
}

impl HttpAssetReader {
/// Creates a new `HttpAssetReader`. The path provided will be used to build URLs to query for assets.
fn new(base_url: &str, tile: bool, sync: Arc<RwLock<HashSet<PathBuf>>>) -> Self {
Self {
base_url: base_url.into(),
tile,
sync,
}
}
pub sync: Arc<RwLock<HashSet<PathBuf>>>,
pub cache_path: Option<PathBuf>,
}
impl HttpAssetReader {}

impl AssetReader for HttpAssetReader {
fn read<'a>(
&'a self,
path: &'a Path,
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
Box::pin(async move {
let cache_path = directories::ProjectDirs::from("org", "osmeta", "OSMeta")
.map(|dirs| dirs.cache_dir().join(path));
let cache_path = self.cache_path.as_ref().map(|p| p.join(path));
// Load from cache if the asset exists there.
if let Some(cache_path) = cache_path.clone() {
if let Some(cache_path) = &cache_path {
if cache_path.exists() {
let file = File::open(&cache_path).await?;
return Ok(Box::new(file) as Box<Reader>);
Expand Down Expand Up @@ -125,17 +115,31 @@ impl Plugin for HttpAssetReaderPlugin {
let base_url = self.base_url.clone();
let sync = Arc::new(RwLock::new(HashSet::new()));
let sync2 = sync.clone();
let cache_path = directories::ProjectDirs::from("org", "osmeta", "OSMeta")
.map(|dirs| dirs.cache_dir().to_owned());
let cache_path2 = cache_path.clone();
app.register_asset_source(
AssetSourceId::Default,
AssetSource::build().with_reader(move || {
Box::new(HttpAssetReader::new(&base_url, false, sync.clone()))
Box::new(HttpAssetReader {
base_url: base_url.clone(),
tile: false,
sync: sync.clone(),
cache_path: cache_path2.clone(),
})
}),
);
let base_url = self.base_url.clone();
app.register_asset_source(
AssetSourceId::Name("tile".into()),
AssetSource::build().with_reader(move || {
Box::new(HttpAssetReader::new(&base_url, true, sync2.clone()))
info!(?cache_path);
Box::new(HttpAssetReader {
base_url: base_url.clone(),
tile: true,
sync: sync2.clone(),
cache_path: cache_path.clone(),
})
}),
);
}
Expand Down

0 comments on commit f498d58

Please sign in to comment.