Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log where we cache the downloaded assets #47

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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