use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::schema::{
Hash, ManifestId, PrivacyClass, ProvenanceRecord, SchemaRef, Timestamp, TrustEnvelope,
};
use crate::{Error, Result, Store};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum BlobSource {
Embedded { bytes: Vec<u8>, media_type: String },
Local { key: String },
Iroh { ticket: String },
Https { url: String },
LocalFile { path: String },
LocalOnlyRef { local_ref: String },
}
impl BlobSource {
pub fn is_transferable(&self) -> bool {
!matches!(self, BlobSource::LocalOnlyRef { .. })
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobManifest {
pub id: ManifestId,
pub schema: SchemaRef,
pub content_hash: Hash,
pub byte_size: u64,
pub created_at: Timestamp,
#[serde(default)]
pub last_accessed: Option<Timestamp>,
pub sources: Vec<BlobSource>,
pub privacy: PrivacyClass,
pub provenance: ProvenanceRecord,
pub trust: TrustEnvelope,
#[serde(default)]
pub schema_metadata: serde_json::Value,
#[serde(default = "BlobManifest::default_version")]
pub manifest_version: u32,
}
impl BlobManifest {
pub const CURRENT_VERSION: u32 = 1;
fn default_version() -> u32 {
Self::CURRENT_VERSION
}
pub fn is_transferable(&self) -> bool {
self.sources.iter().all(BlobSource::is_transferable)
}
pub fn to_transferable(&self) -> Result<Self> {
let sources: Vec<_> = self
.sources
.iter()
.filter(|source| source.is_transferable())
.cloned()
.collect();
if sources.is_empty() {
return Err(Error::new(
"manifest has no transferable sources after stripping LocalOnlyRef",
));
}
Ok(Self {
sources,
..self.clone()
})
}
}
#[async_trait(?Send)]
pub trait BlobFetcher {
async fn fetch(&mut self, source: &BlobSource) -> Result<Option<Vec<u8>>>;
}
pub struct NoFetcher;
#[async_trait(?Send)]
impl BlobFetcher for NoFetcher {
async fn fetch(&mut self, _source: &BlobSource) -> Result<Option<Vec<u8>>> {
Ok(None)
}
}
pub async fn list_manifests(
store: &mut dyn Store,
schema_filter: Option<SchemaRef>,
) -> Result<Vec<BlobManifest>> {
let keys = store.list("manifest:").await?;
let mut out = Vec::with_capacity(keys.len());
for key in keys {
let bytes = match store.get(&key).await? {
Some(b) => b,
None => continue, };
let manifest: BlobManifest = match serde_json::from_slice(&bytes) {
Ok(m) => m,
Err(_) => continue,
};
if let Some(filter) = schema_filter {
if manifest.schema != filter {
continue;
}
}
out.push(manifest);
}
Ok(out)
}
pub async fn save_manifest(store: &mut dyn Store, manifest: &BlobManifest) -> Result<()> {
let bytes =
serde_json::to_vec(manifest).map_err(|e| Error::new(format!("manifest serialize: {e}")))?;
store.put(&manifest.id.store_key(), &bytes).await?;
Ok(())
}
pub async fn delete_manifest(store: &mut dyn Store, id: ManifestId) -> Result<bool> {
let key = id.store_key();
let existed = store.get(&key).await?.is_some();
store.delete(&key).await?;
Ok(existed)
}
pub async fn load_manifest(store: &mut dyn Store, id: ManifestId) -> Result<Option<BlobManifest>> {
let key = id.store_key();
match store.get(&key).await? {
None => Ok(None),
Some(bytes) => {
let manifest: BlobManifest = serde_json::from_slice(&bytes)
.map_err(|e| Error::new(format!("manifest deserialize: {e}")))?;
Ok(Some(manifest))
}
}
}
pub async fn resolve_blob(
store: &mut dyn Store,
fetcher: &mut dyn BlobFetcher,
manifest: &BlobManifest,
) -> Result<Vec<u8>> {
if crate::seal::seal_marker(manifest)?.is_some() {
return Err(Error::new(
"manifest blob is sealed at rest; use seal::resolve_sealed_blob with a PayloadSealer",
));
}
let mut last_error: Option<Error> = None;
for source in &manifest.sources {
let candidate = match source {
BlobSource::Embedded { bytes, .. } => Some(bytes.clone()),
BlobSource::Local { key } => store.get(key).await?,
_ => match fetcher.fetch(source).await {
Ok(value) => value,
Err(error) => {
last_error = Some(error);
continue;
}
},
};
let Some(bytes) = candidate else {
continue;
};
let actual = Hash::of(&bytes);
if actual != manifest.content_hash {
last_error = Some(Error::new(format!(
"content hash mismatch from source ({:?}): manifest declared {}, blob has {}",
source_kind(source),
manifest.content_hash,
actual
)));
continue;
}
if !matches!(
source,
BlobSource::Local { .. } | BlobSource::Embedded { .. }
) {
let cache_key = blob_cache_key(&actual);
store.put(&cache_key, &bytes).await?;
}
return Ok(bytes);
}
Err(last_error.unwrap_or_else(|| {
Error::new(format!(
"no source could resolve blob for manifest {}",
manifest.id
))
}))
}
fn blob_cache_key(hash: &Hash) -> String {
format!("blob:{}", hash.to_hex())
}
fn source_kind(source: &BlobSource) -> &'static str {
match source {
BlobSource::Embedded { .. } => "Embedded",
BlobSource::Local { .. } => "Local",
BlobSource::Iroh { .. } => "Iroh",
BlobSource::Https { .. } => "Https",
BlobSource::LocalFile { .. } => "LocalFile",
BlobSource::LocalOnlyRef { .. } => "LocalOnlyRef",
}
}
#[cfg(test)]
mod tests;