diff --git a/rhio-client/src/lib.rs b/rhio-client/src/lib.rs index d1ad027..cdb1a1d 100644 --- a/rhio-client/src/lib.rs +++ b/rhio-client/src/lib.rs @@ -1,8 +1,10 @@ use anyhow::Result; use async_nats::jetstream::Context; -use p2panda_core::PrivateKey; +use p2panda_core::{Hash, PrivateKey}; use p2panda_store::MemoryStore; -use rhio_core::{create_message, encode_operation, LogId, RhioExtensions}; +use rhio_core::{ + create_blob_announcement, create_message, encode_operation, LogId, RhioExtensions, +}; pub struct Client { jetstream: JetStream, @@ -33,6 +35,14 @@ impl Client { self.jetstream.publish(subject, encoded_operation).await?; Ok(()) } + + pub async fn announce_blob(&mut self, subject: String, hash: Hash) -> Result<()> { + let operation = + create_blob_announcement(&mut self.store, &self.private_key, &subject, hash)?; + let encoded_operation = encode_operation(operation.header, operation.body)?; + self.jetstream.publish(subject, encoded_operation).await?; + Ok(()) + } } pub struct JetStream { diff --git a/rhio-client/src/main.rs b/rhio-client/src/main.rs index 6532864..99bc10f 100644 --- a/rhio-client/src/main.rs +++ b/rhio-client/src/main.rs @@ -1,5 +1,8 @@ +use std::str::FromStr; + use anyhow::{Context, Result}; use clap::Parser; +use p2panda_core::Hash; use rhio_client::Client; use tokio::sync::mpsc; @@ -35,9 +38,19 @@ async fn main() -> Result<()> { loop { tokio::select! { Some(payload) = line_rx.recv() => { - client - .publish(args.subject.clone(), payload.as_bytes()) - .await?; + // If user writes a string, starting with "blob" (4 characters), followed by a + // space (1 character) and then ending with an hex-encoded BLAKE3 hash (64 + // characters), then we interpret this as a blob announcement! + if payload.len() == 4 + 1 + 64 && payload.to_lowercase().starts_with("blob") { + let file_name = &payload[5..]; + client + .announce_blob(args.subject.clone(), file_name) + .await?; + } else { + client + .publish(args.subject.clone(), payload.as_bytes()) + .await?; + } } _ = tokio::signal::ctrl_c() => { break; diff --git a/rhio-core/src/operation.rs b/rhio-core/src/operation.rs index f1863bc..de426e0 100644 --- a/rhio-core/src/operation.rs +++ b/rhio-core/src/operation.rs @@ -13,12 +13,12 @@ pub fn create_blob_announcement( store: &mut S, private_key: &PrivateKey, subject: &str, - blob_hash: Option, + blob_hash: Hash, ) -> Result> where S: OperationStore + LogStore, { - create_operation(store, private_key, subject, blob_hash, None) + create_operation(store, private_key, subject, Some(blob_hash), None) } pub fn create_message(