1use anyhow::Result;
9use hashtree_blossom::BlossomClient;
10use hashtree_core::{decode_tree_node, to_hex};
11use nostr::Keys;
12use std::collections::VecDeque;
13use std::sync::Arc;
14use std::time::Duration;
15use tracing::debug;
16
17use crate::storage::HashtreeStore;
18use crate::webrtc::WebRTCState;
19
20#[derive(Clone)]
22pub struct FetchConfig {
23 pub webrtc_timeout: Duration,
25 pub blossom_timeout: Duration,
27}
28
29impl Default for FetchConfig {
30 fn default() -> Self {
31 Self {
32 webrtc_timeout: Duration::from_millis(2000),
33 blossom_timeout: Duration::from_millis(10000),
34 }
35 }
36}
37
38pub struct Fetcher {
40 config: FetchConfig,
41 blossom: BlossomClient,
42}
43
44impl Fetcher {
45 pub fn new(config: FetchConfig) -> Self {
48 let keys = Keys::generate();
50 let blossom = BlossomClient::new(keys)
51 .with_timeout(config.blossom_timeout);
52
53 Self { config, blossom }
54 }
55
56 pub fn with_keys(config: FetchConfig, keys: Keys) -> Self {
58 let blossom = BlossomClient::new(keys)
59 .with_timeout(config.blossom_timeout);
60
61 Self { config, blossom }
62 }
63
64 pub fn blossom(&self) -> &BlossomClient {
66 &self.blossom
67 }
68
69 pub async fn fetch_chunk(
71 &self,
72 webrtc_state: Option<&Arc<WebRTCState>>,
73 hash_hex: &str,
74 ) -> Result<Vec<u8>> {
75 let short_hash = if hash_hex.len() >= 12 {
76 &hash_hex[..12]
77 } else {
78 hash_hex
79 };
80
81 if let Some(state) = webrtc_state {
83 debug!("Trying WebRTC for {}", short_hash);
84 let webrtc_result = tokio::time::timeout(
85 self.config.webrtc_timeout,
86 state.request_from_peers(hash_hex),
87 )
88 .await;
89
90 if let Ok(Some(data)) = webrtc_result {
91 debug!("Got {} from WebRTC ({} bytes)", short_hash, data.len());
92 return Ok(data);
93 }
94 }
95
96 debug!("Trying Blossom for {}", short_hash);
98 match self.blossom.download(hash_hex).await {
99 Ok(data) => {
100 debug!("Got {} from Blossom ({} bytes)", short_hash, data.len());
101 Ok(data)
102 }
103 Err(e) => {
104 debug!("Blossom download failed for {}: {}", short_hash, e);
105 Err(anyhow::anyhow!("Failed to fetch {} from any source: {}", short_hash, e))
106 }
107 }
108 }
109
110 pub async fn fetch_chunk_with_store(
112 &self,
113 store: &HashtreeStore,
114 webrtc_state: Option<&Arc<WebRTCState>>,
115 hash: &[u8; 32],
116 ) -> Result<Vec<u8>> {
117 if let Some(data) = store.get_chunk(hash)? {
119 return Ok(data);
120 }
121
122 let hash_hex = to_hex(hash);
124 let data = self.fetch_chunk(webrtc_state, &hash_hex).await?;
125 store.put_blob(&data)?;
126 Ok(data)
127 }
128
129 pub async fn fetch_tree(
132 &self,
133 store: &HashtreeStore,
134 webrtc_state: Option<&Arc<WebRTCState>>,
135 root_hash: &[u8; 32],
136 ) -> Result<(usize, u64)> {
137 self.fetch_tree_parallel(store, webrtc_state, root_hash, 1).await
138 }
139
140 pub async fn fetch_tree_parallel(
144 &self,
145 store: &HashtreeStore,
146 webrtc_state: Option<&Arc<WebRTCState>>,
147 root_hash: &[u8; 32],
148 concurrency: usize,
149 ) -> Result<(usize, u64)> {
150 use futures::stream::{FuturesUnordered, StreamExt};
151 use std::collections::HashSet;
152 use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
153
154 if store.blob_exists(root_hash)? {
156 return Ok((0, 0));
157 }
158
159 let chunks_fetched = Arc::new(AtomicUsize::new(0));
160 let bytes_fetched = Arc::new(AtomicU64::new(0));
161
162 let mut queued: HashSet<[u8; 32]> = HashSet::new();
164 let mut pending: VecDeque<[u8; 32]> = VecDeque::new();
165
166 pending.push_back(*root_hash);
168 queued.insert(*root_hash);
169
170 let mut active = FuturesUnordered::new();
171
172 loop {
173 while active.len() < concurrency {
175 if let Some(hash) = pending.pop_front() {
176 if store.blob_exists(&hash).unwrap_or(false) {
178 continue;
179 }
180
181 let hash_hex = to_hex(&hash);
182 let blossom = &self.blossom;
183
184 let fut = async move {
185 let data = blossom.download(&hash_hex).await;
186 (hash, data)
187 };
188 active.push(fut);
189 } else {
190 break;
191 }
192 }
193
194 if active.is_empty() {
196 break;
197 }
198
199 if let Some((hash, result)) = active.next().await {
201 match result {
202 Ok(data) => {
203 store.put_blob(&data)?;
205 chunks_fetched.fetch_add(1, Ordering::Relaxed);
206 bytes_fetched.fetch_add(data.len() as u64, Ordering::Relaxed);
207
208 if let Ok(node) = decode_tree_node(&data) {
210 for link in node.links {
211 if !queued.contains(&link.hash) {
212 queued.insert(link.hash);
213 pending.push_back(link.hash);
214 }
215 }
216 }
217 }
218 Err(e) => {
219 debug!("Failed to fetch {}: {}", to_hex(&hash), e);
220 }
222 }
223 }
224 }
225
226 Ok((
227 chunks_fetched.load(Ordering::Relaxed),
228 bytes_fetched.load(Ordering::Relaxed),
229 ))
230 }
231
232 pub async fn fetch_file(
235 &self,
236 store: &HashtreeStore,
237 webrtc_state: Option<&Arc<WebRTCState>>,
238 hash: &[u8; 32],
239 ) -> Result<Option<Vec<u8>>> {
240 if let Some(content) = store.get_file(hash)? {
242 return Ok(Some(content));
243 }
244
245 self.fetch_tree(store, webrtc_state, hash).await?;
247
248 store.get_file(hash)
250 }
251
252 pub async fn fetch_directory(
254 &self,
255 store: &HashtreeStore,
256 webrtc_state: Option<&Arc<WebRTCState>>,
257 hash: &[u8; 32],
258 ) -> Result<Option<crate::storage::DirectoryListing>> {
259 if let Ok(Some(listing)) = store.get_directory_listing(hash) {
261 return Ok(Some(listing));
262 }
263
264 self.fetch_tree(store, webrtc_state, hash).await?;
266
267 store.get_directory_listing(hash)
269 }
270
271 pub async fn upload(&self, data: &[u8]) -> Result<String> {
273 self.blossom
274 .upload(data)
275 .await
276 .map_err(|e| anyhow::anyhow!("Blossom upload failed: {}", e))
277 }
278
279 pub async fn upload_if_missing(&self, data: &[u8]) -> Result<(String, bool)> {
281 self.blossom
282 .upload_if_missing(data)
283 .await
284 .map_err(|e| anyhow::anyhow!("Blossom upload failed: {}", e))
285 }
286}