From 1fa9940e8882ade26a5a46e1f305929e6cdc1aee Mon Sep 17 00:00:00 2001 From: Edison Jwa Date: Wed, 10 Jun 2026 07:09:47 +0900 Subject: [PATCH] feat(bridge): add init_cache, download_avatar, and cache management functions - Add init_cache(dir) bridge function - Add download_avatar(avatar_hash, client_uid) bridge function - Add clear_file_cache() and file_cache_size() bridge functions - Map CoreError::Cache and ProtocolError::FileTransfer in BridgeError --- crates/chanora_bridge/src/api.rs | 39 ++++++++++++++++++++++++++++++++ crates/chanora_bridge/src/lib.rs | 4 ++++ 2 files changed, 43 insertions(+) diff --git a/crates/chanora_bridge/src/api.rs b/crates/chanora_bridge/src/api.rs index 6cde524..a808484 100644 --- a/crates/chanora_bridge/src/api.rs +++ b/crates/chanora_bridge/src/api.rs @@ -1465,6 +1465,45 @@ pub async fn init_storage(dir: String) -> Result<(), BridgeError> { Ok(()) } +/// Configure the bridge blob cache root. +pub async fn init_cache(dir: String) -> Result<(), BridgeError> { + runtime() + .spawn(async move { session().init_cache(&dir).await }) + .await + .map_err(|e| task_join_error("init_cache", e))??; + Ok(()) +} + +/// Resolve avatar bytes through the bridge. +pub async fn download_avatar( + avatar_hash: String, + client_uid: String, +) -> Result>, BridgeError> { + runtime() + .spawn(async move { session().get_avatar(&avatar_hash, &client_uid).await }) + .await + .map_err(|e| task_join_error("download_avatar", e))? + .map_err(BridgeError::from) +} + +/// Purge cached protocol-owned assets. +pub async fn clear_file_cache() -> Result<(), BridgeError> { + runtime() + .spawn(async move { session().clear_cache().await }) + .await + .map_err(|e| task_join_error("clear_file_cache", e))??; + Ok(()) +} + +/// Report the configured file-cache size. +pub async fn file_cache_size() -> Result { + runtime() + .spawn(async move { session().cache_size().await }) + .await + .map_err(|e| task_join_error("file_cache_size", e))? + .map_err(BridgeError::from) +} + /// Bookmark DTO mirroring [`chanora_core::Bookmark`]. #[derive(Debug, Clone)] pub struct BridgeBookmark { diff --git a/crates/chanora_bridge/src/lib.rs b/crates/chanora_bridge/src/lib.rs index 3d85857..e01c8af 100644 --- a/crates/chanora_bridge/src/lib.rs +++ b/crates/chanora_bridge/src/lib.rs @@ -110,9 +110,13 @@ impl From for BridgeError { code, message, }) => BridgeError::ServerRejected { code, message }, + chanora_core::CoreError::Protocol(chanora_core::ProtocolError::FileTransfer(p)) => { + BridgeError::Connection(format!("file transfer: {p}")) + } chanora_core::CoreError::Protocol(p) => BridgeError::Connection(format!("{p}")), chanora_core::CoreError::Audio(a) => BridgeError::Connection(format!("audio: {a}")), chanora_core::CoreError::Storage(s) => BridgeError::Connection(format!("storage: {s}")), + chanora_core::CoreError::Cache(c) => BridgeError::Connection(format!("cache: {c}")), other => BridgeError::Unmapped(format!("{other}")), } }