use crate::StorageError;
use anyhow::{anyhow, Context};
use cid::Cid;
use co_primitives::{unixfs_add, AnyBlockStorage};
use std::path::Path;
use tokio_util::compat::TokioAsyncReadCompatExt;
pub async fn unixfs_add_file(storage: &impl AnyBlockStorage, file: impl AsRef<Path>) -> Result<Cid, anyhow::Error> {
let mut handle = tokio::fs::File::open(file.as_ref())
.await
.with_context(|| format!("open file: {:?}", file.as_ref()))?
.compat();
Ok(unixfs_add(storage, &mut handle)
.await?
.last()
.ok_or(StorageError::InvalidArgument(anyhow!("No CID generated: {:?}", file.as_ref())))? .to_owned())
}