use std::path::{Path, PathBuf};
use std::sync::Arc;
use object_store::ObjectStore;
use object_store::aws::AmazonS3Builder;
use object_store::local::LocalFileSystem;
#[derive(Debug, Clone)]
pub struct QuarantineStorageConfig {
pub endpoint: String,
pub bucket: String,
pub prefix: String,
pub access_key: String,
pub secret_key: String,
pub region: String,
pub local_dir: Option<PathBuf>,
}
pub fn build_quarantine_store(
config: &QuarantineStorageConfig,
data_dir: &Path,
) -> crate::Result<Arc<dyn ObjectStore>> {
let dir = config
.local_dir
.clone()
.unwrap_or_else(|| data_dir.join("quarantine"));
if config.endpoint.is_empty() {
std::fs::create_dir_all(&dir).map_err(crate::Error::Io)?;
let store = LocalFileSystem::new_with_prefix(&dir).map_err(|e| crate::Error::Storage {
engine: "quarantine".into(),
detail: format!("local quarantine storage init: {e}"),
})?;
Ok(Arc::new(store))
} else {
let mut builder = AmazonS3Builder::new()
.with_endpoint(&config.endpoint)
.with_bucket_name(&config.bucket)
.with_region(&config.region)
.with_allow_http(config.endpoint.starts_with("http://"));
if !config.access_key.is_empty() {
builder = builder
.with_access_key_id(&config.access_key)
.with_secret_access_key(&config.secret_key);
}
let s3 = builder.build().map_err(|e| crate::Error::Storage {
engine: "quarantine".into(),
detail: format!("S3 quarantine client init: {e}"),
})?;
Ok(Arc::new(s3))
}
}