use std::path::Path;
use common::peer::BlobsStore;
use crate::state::BlobStoreConfig;
use super::BlobsSetupError;
pub async fn setup_blobs_store(
config: &BlobStoreConfig,
jax_dir: &Path,
max_import_size: u64,
) -> Result<BlobsStore, BlobsSetupError> {
match config {
BlobStoreConfig::Legacy => {
let blobs_path = jax_dir.join("blobs");
tracing::info!(path = %blobs_path.display(), "Using iroh blob store");
BlobsStore::legacy_fs(&blobs_path)
.await
.map_err(|e| BlobsSetupError::StoreError(e.to_string()))
}
BlobStoreConfig::Filesystem { path, db_path } => {
let db_path = db_path.clone().unwrap_or_else(|| path.join("blobs.db"));
tracing::info!(
objects_path = %path.display(),
db_path = %db_path.display(),
max_import_size,
"Using SQLite + local filesystem blob store"
);
BlobsStore::fs(&db_path, path, Some(max_import_size))
.await
.map_err(|e| BlobsSetupError::StoreError(e.to_string()))
}
BlobStoreConfig::S3 { url } => {
let s3_config = BlobStoreConfig::parse_s3_url(url)
.map_err(|e| BlobsSetupError::StoreError(e.to_string()))?;
tracing::info!(
endpoint = %s3_config.endpoint,
bucket = %s3_config.bucket,
max_import_size,
"Using SQLite + S3 blob store"
);
let db_path = jax_dir.join("blobs.db");
BlobsStore::s3(
&db_path,
&s3_config.endpoint,
&s3_config.access_key,
&s3_config.secret_key,
&s3_config.bucket,
None, Some(max_import_size),
)
.await
.map_err(|e| BlobsSetupError::StoreError(e.to_string()))
}
}
}