1use std::fs::{self, OpenOptions};
4use std::io::{BufRead, BufReader, Write};
5use std::path::{Path, PathBuf};
6use std::time::Duration;
7
8use serde::{Deserialize, Serialize};
9use sha1::{Digest as Sha1Digest, Sha1};
10use sha2::Sha256;
11
12use crate::q42_compress;
13use crate::resource_import;
14use crate::social_connect::ChatContact;
15
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
17#[serde(rename_all = "snake_case")]
18pub enum ShareAudience {
19 Private,
20 AddressbookAll,
21 AddressbookCategory,
22 SpecificDids,
23 ChatSessions,
25 PublicSeed,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
29pub struct OntologyTorrentPolicy {
30 pub seed_enabled: bool,
31 pub share_enabled: bool,
32 pub audience: ShareAudience,
33 #[serde(default)]
34 pub allowed_categories: Vec<String>,
35 #[serde(default)]
36 pub allowed_contact_dids: Vec<String>,
37 #[serde(default)]
38 pub allowed_session_dids: Vec<String>,
39 #[serde(default)]
41 pub bandwidth_limit_kbps: u32,
42 #[serde(default)]
43 pub max_upload_mb_per_day: Option<u32>,
44}
45
46impl Default for OntologyTorrentPolicy {
47 fn default() -> Self {
48 Self {
49 seed_enabled: false,
50 share_enabled: false,
51 audience: ShareAudience::Private,
52 allowed_categories: vec![],
53 allowed_contact_dids: vec![],
54 allowed_session_dids: vec![],
55 bandwidth_limit_kbps: 512,
56 max_upload_mb_per_day: Some(500),
57 }
58 }
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct TorrentBandwidthGlobal {
63 #[serde(default)]
64 pub global_limit_kbps: u32,
65 #[serde(default = "default_metered")]
66 pub metered_mode: bool,
67}
68
69fn default_metered() -> bool {
70 true
71}
72
73impl Default for TorrentBandwidthGlobal {
74 fn default() -> Self {
75 Self {
76 global_limit_kbps: 1024,
77 metered_mode: true,
78 }
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct OntologyWorkbenchEntry {
84 pub ontology_id: String,
85 pub title: String,
86 pub source_uri: String,
87 pub domain: String,
88 pub c_q42_path: String,
89 pub quin_count: u64,
90 pub sha256: String,
91 pub info_hash_sha1: String,
92 pub magnet_uri: String,
93 pub imported_at: u64,
94 pub torrent: OntologyTorrentPolicy,
95 pub seed_active: bool,
96 pub bytes_uploaded_total: u64,
97 pub bytes_uploaded_today: u64,
98 pub uploaded_day_epoch: u64,
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct WorkbenchImportResult {
103 pub entry: OntologyWorkbenchEntry,
104 pub compress_ratio: f64,
105 pub source_removed: bool,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct OntologyShareCard {
110 pub ontology_id: String,
111 pub title: String,
112 pub domain: String,
113 pub magnet_uri: String,
114 pub info_hash_sha1: String,
115 pub quin_count: u64,
116}
117
118fn workbench_path(storage_root: &Path) -> PathBuf {
119 resource_import::index_dir(storage_root).join("workbench.jsonl")
120}
121
122fn bandwidth_policy_path() -> PathBuf {
123 crate::state::app_meta_dir().join("torrent_bandwidth.json")
124}
125
126fn unix_now() -> u64 {
127 std::time::SystemTime::now()
128 .duration_since(std::time::UNIX_EPOCH)
129 .unwrap_or_default()
130 .as_secs()
131}
132
133fn day_epoch(ts: u64) -> u64 {
134 ts / 86_400
135}
136
137pub fn derive_ontology_id_from_uri(uri: &str) -> String {
139 let trimmed = uri.trim();
140 if let Ok(url) = url::Url::parse(trimmed) {
141 if let Some(segments) = url.path_segments() {
142 let last = segments
143 .filter(|s| !s.is_empty())
144 .last()
145 .unwrap_or("ontology");
146 let stem = Path::new(last)
147 .file_stem()
148 .and_then(|s| s.to_str())
149 .unwrap_or(last);
150 return sanitize_id(stem);
151 }
152 if let Some(host) = url.host_str() {
153 return sanitize_id(host);
154 }
155 }
156 sanitize_id(trimmed)
157}
158
159fn sanitize_id(raw: &str) -> String {
160 let lower = raw.to_lowercase();
161 let mut out = String::new();
162 for ch in lower.chars() {
163 if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' {
164 out.push(ch);
165 } else if ch == '.' || ch == '/' {
166 out.push('-');
167 }
168 }
169 if out.is_empty() {
170 format!("ontology-{}", unix_now())
171 } else {
172 out.trim_matches('-').to_string()
173 }
174}
175
176fn extension_from_uri(uri: &str) -> String {
177 if let Ok(url) = url::Url::parse(uri) {
178 if let Some(path) = url.path_segments() {
179 if let Some(last) = path.filter(|s| !s.is_empty()).last() {
180 return Path::new(last)
181 .extension()
182 .and_then(|s| s.to_str())
183 .unwrap_or("ttl")
184 .to_lowercase();
185 }
186 }
187 }
188 "ttl".to_string()
189}
190
191fn sha256_file(path: &Path) -> Result<String, String> {
192 use sha2::Digest as Sha2Digest;
193 let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
194 let mut hasher = Sha256::new();
195 let mut buf = [0u8; 65_536];
196 loop {
197 let n = std::io::Read::read(&mut file, &mut buf).map_err(|e| e.to_string())?;
198 if n == 0 {
199 break;
200 }
201 hasher.update(&buf[..n]);
202 }
203 Ok(hex::encode(hasher.finalize()))
204}
205
206pub fn sha1_file(path: &Path) -> Result<String, String> {
208 let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
209 let mut hasher = Sha1::new();
210 let mut buf = [0u8; 8192];
211 loop {
212 let n = std::io::Read::read(&mut file, &mut buf).map_err(|e| e.to_string())?;
213 if n == 0 {
214 break;
215 }
216 hasher.update(&buf[..n]);
217 }
218 Ok(hasher
219 .finalize()
220 .iter()
221 .map(|b| format!("{b:02x}"))
222 .collect())
223}
224
225pub fn build_magnet_uri(info_hash_sha1: &str, display_name: &str) -> String {
227 qualia_core_db::webtorrent_seeder::build_magnet_uri(
228 info_hash_sha1,
229 display_name,
230 crate::api::get_active_daemon_port(),
231 )
232}
233
234fn daemon_base_url() -> String {
235 format!("http://127.0.0.1:{}", crate::api::get_active_daemon_port())
236}
237
238fn daemon_get(path: &str) -> Result<serde_json::Value, String> {
239 let client = reqwest::blocking::Client::builder()
240 .timeout(Duration::from_secs(6))
241 .build()
242 .map_err(|e| e.to_string())?;
243 let url = format!("{}{}", daemon_base_url(), path);
244 let resp = client.get(&url).send().map_err(|e| e.to_string())?;
245 if !resp.status().is_success() {
246 return Err(format!("daemon GET {path} returned {}", resp.status()));
247 }
248 resp.json().map_err(|e| e.to_string())
249}
250
251fn daemon_post(path: &str, body: &serde_json::Value) -> Result<serde_json::Value, String> {
252 let client = reqwest::blocking::Client::builder()
253 .timeout(Duration::from_secs(8))
254 .build()
255 .map_err(|e| e.to_string())?;
256 let url = format!("{}{}", daemon_base_url(), path);
257 let resp = client
258 .post(&url)
259 .json(body)
260 .send()
261 .map_err(|e| e.to_string())?;
262 if !resp.status().is_success() {
263 return Err(format!("daemon POST {path} returned {}", resp.status()));
264 }
265 resp.json().map_err(|e| e.to_string())
266}
267
268fn sync_daemon_bandwidth_policy() {
269 let global = load_bandwidth_policy();
270 let body = serde_json::json!({
271 "global_limit_kbps": global.global_limit_kbps,
272 "metered_mode": global.metered_mode,
273 });
274 let _ = daemon_post("/torrent/policy", &body);
275}
276
277fn register_seed_on_daemon(entry: &OntologyWorkbenchEntry) -> Result<(), String> {
278 let body = serde_json::json!({
279 "info_hash": entry.info_hash_sha1,
280 "file_path": entry.c_q42_path,
281 "display_name": format!("{}.c.q42", entry.ontology_id),
282 "ontology_id": entry.ontology_id,
283 "bandwidth_limit_kbps": entry.torrent.bandwidth_limit_kbps,
284 });
285 daemon_post("/torrent/seed", &body)?;
286 Ok(())
287}
288
289fn unregister_seed_on_daemon(info_hash: &str) -> Result<(), String> {
290 let body = serde_json::json!({ "info_hash": info_hash });
291 let _ = daemon_post("/torrent/unseed", &body)?;
292 Ok(())
293}
294
295fn refresh_entry_magnet(entry: &mut OntologyWorkbenchEntry) {
296 let display = format!("{}.c.q42", entry.ontology_id);
297 entry.magnet_uri = qualia_core_db::webtorrent_seeder::ensure_magnet_webseed(
298 &entry.magnet_uri,
299 &entry.info_hash_sha1,
300 &display,
301 crate::api::get_active_daemon_port(),
302 );
303}
304
305fn load_entries(storage_root: &Path) -> Result<Vec<OntologyWorkbenchEntry>, String> {
306 let path = workbench_path(storage_root);
307 if !path.is_file() {
308 return Ok(vec![]);
309 }
310 let file = fs::File::open(path).map_err(|e| e.to_string())?;
311 let reader = BufReader::new(file);
312 let mut out = Vec::new();
313 for line in reader.lines() {
314 let line = line.map_err(|e| e.to_string())?;
315 if line.trim().is_empty() {
316 continue;
317 }
318 out.push(serde_json::from_str(&line).map_err(|e| e.to_string())?);
319 }
320 Ok(out)
321}
322
323fn write_entries(storage_root: &Path, entries: &[OntologyWorkbenchEntry]) -> Result<(), String> {
324 let path = workbench_path(storage_root);
325 if let Some(parent) = path.parent() {
326 fs::create_dir_all(parent).map_err(|e| e.to_string())?;
327 }
328 let mut file = OpenOptions::new()
329 .create(true)
330 .write(true)
331 .truncate(true)
332 .open(path)
333 .map_err(|e| e.to_string())?;
334 for e in entries {
335 writeln!(
336 file,
337 "{}",
338 serde_json::to_string(e).map_err(|e| e.to_string())?
339 )
340 .map_err(|e| e.to_string())?;
341 }
342 Ok(())
343}
344
345pub fn load_bandwidth_policy() -> TorrentBandwidthGlobal {
347 let path = bandwidth_policy_path();
348 if let Ok(text) = fs::read_to_string(path) {
349 if let Ok(p) = serde_json::from_str(&text) {
350 return p;
351 }
352 }
353 TorrentBandwidthGlobal::default()
354}
355
356pub fn save_bandwidth_policy(policy: &TorrentBandwidthGlobal) -> Result<(), String> {
358 let path = bandwidth_policy_path();
359 if let Some(parent) = path.parent() {
360 fs::create_dir_all(parent).map_err(|e| e.to_string())?;
361 }
362 let text = serde_json::to_string_pretty(policy).map_err(|e| e.to_string())?;
363 fs::write(path, text).map_err(|e| e.to_string())?;
364 sync_daemon_bandwidth_policy();
365 Ok(())
366}
367
368pub fn contact_eligible_for_share(entry: &OntologyWorkbenchEntry, contact: &ChatContact) -> bool {
370 if !entry.torrent.share_enabled {
371 return false;
372 }
373 match entry.torrent.audience {
374 ShareAudience::Private => false,
375 ShareAudience::PublicSeed | ShareAudience::AddressbookAll => true,
376 ShareAudience::SpecificDids => entry
377 .torrent
378 .allowed_contact_dids
379 .iter()
380 .any(|d| d == &contact.did),
381 ShareAudience::AddressbookCategory => entry
382 .torrent
383 .allowed_categories
384 .iter()
385 .any(|cat| contact.categories.iter().any(|c| c == cat)),
386 ShareAudience::ChatSessions => false,
387 }
388}
389
390pub fn session_eligible_for_share(entry: &OntologyWorkbenchEntry, session_did: &str) -> bool {
392 if !entry.torrent.share_enabled || session_did.is_empty() {
393 return false;
394 }
395 match entry.torrent.audience {
396 ShareAudience::Private => false,
397 ShareAudience::PublicSeed | ShareAudience::AddressbookAll => true,
398 ShareAudience::ChatSessions => entry
399 .torrent
400 .allowed_session_dids
401 .iter()
402 .any(|d| d == session_did),
403 ShareAudience::SpecificDids | ShareAudience::AddressbookCategory => false,
404 }
405}
406
407pub fn list_share_cards_for_contact(
409 storage_root: &Path,
410 contact_did: &str,
411) -> Result<Vec<OntologyShareCard>, String> {
412 let contact = crate::social_connect::find_contact_by_did(contact_did)
413 .ok_or_else(|| format!("Contact not found: {contact_did}"))?;
414 let entries = load_entries(storage_root)?;
415 Ok(entries
416 .iter()
417 .filter(|e| contact_eligible_for_share(e, &contact))
418 .map(|e| OntologyShareCard {
419 ontology_id: e.ontology_id.clone(),
420 title: e.title.clone(),
421 domain: e.domain.clone(),
422 magnet_uri: e.magnet_uri.clone(),
423 info_hash_sha1: e.info_hash_sha1.clone(),
424 quin_count: e.quin_count,
425 })
426 .collect())
427}
428
429pub fn list_share_cards_for_session(
431 storage_root: &Path,
432 session_did: &str,
433) -> Result<Vec<OntologyShareCard>, String> {
434 if session_did.trim().is_empty() {
435 return Err("session_did required".to_string());
436 }
437 let entries = load_entries(storage_root)?;
438 Ok(entries
439 .iter()
440 .filter(|e| session_eligible_for_share(e, session_did))
441 .map(|e| OntologyShareCard {
442 ontology_id: e.ontology_id.clone(),
443 title: e.title.clone(),
444 domain: e.domain.clone(),
445 magnet_uri: e.magnet_uri.clone(),
446 info_hash_sha1: e.info_hash_sha1.clone(),
447 quin_count: e.quin_count,
448 })
449 .collect())
450}
451
452pub async fn import_from_uri(
454 storage_root: &Path,
455 uri: String,
456 ontology_id: Option<String>,
457 domain: Option<String>,
458 title: Option<String>,
459) -> Result<WorkbenchImportResult, String> {
460 let uri = uri.trim().to_string();
461 if uri.is_empty() {
462 return Err("URI required".to_string());
463 }
464 if !uri.starts_with("http://") && !uri.starts_with("https://") {
465 return Err("Only http(s) URIs are supported".to_string());
466 }
467
468 let id = ontology_id
469 .map(|s| sanitize_id(&s))
470 .filter(|s| !s.is_empty())
471 .unwrap_or_else(|| derive_ontology_id_from_uri(&uri));
472 let ext = extension_from_uri(&uri);
473 let index = resource_import::index_dir(storage_root);
474 fs::create_dir_all(&index).map_err(|e| e.to_string())?;
475
476 let source_path = index.join(format!("{id}.source.{ext}"));
477 resource_import::stream_download(&uri, &source_path)
478 .await
479 .map_err(|e| e.to_string())?;
480
481 let quin_count = resource_import::ingest_local_rdf(&source_path, &id, storage_root, None)
482 .map_err(|e| e.to_string())?;
483
484 let q42_path = index.join(format!("{id}.q42"));
485 let c_q42_path = index.join(format!("{id}.c.q42"));
486 let stats = q42_compress::finalize_c_q42(&q42_path, &c_q42_path)?;
487
488 let source_removed = fs::remove_file(&source_path).is_ok();
489 let _ = fs::remove_file(&q42_path);
490
491 let sha256 = sha256_file(&c_q42_path)?;
492 let info_hash = sha1_file(&c_q42_path)?;
493 let display = format!("{id}.c.q42");
494 let magnet = build_magnet_uri(&info_hash, &display);
495 let now = unix_now();
496 let domain_label = domain.unwrap_or_else(|| "general".to_string());
497 let title_label = title.unwrap_or_else(|| id.clone());
498
499 let entry = OntologyWorkbenchEntry {
500 ontology_id: id.clone(),
501 title: title_label,
502 source_uri: uri,
503 domain: domain_label,
504 c_q42_path: c_q42_path.to_string_lossy().into_owned(),
505 quin_count,
506 sha256,
507 info_hash_sha1: info_hash,
508 magnet_uri: magnet,
509 imported_at: now,
510 torrent: OntologyTorrentPolicy::default(),
511 seed_active: false,
512 bytes_uploaded_total: 0,
513 bytes_uploaded_today: 0,
514 uploaded_day_epoch: day_epoch(now),
515 };
516
517 let mut entries = load_entries(storage_root)?;
518 entries.retain(|e| e.ontology_id != id);
519 entries.push(entry.clone());
520 write_entries(storage_root, &entries)?;
521
522 let meta_path = index.join(format!("{id}.c.q42.meta.json"));
523 let meta = serde_json::json!({
524 "ontology_id": id,
525 "source_uri": entry.source_uri,
526 "c_q42_path": entry.c_q42_path,
527 "quin_count": quin_count,
528 "sha256": entry.sha256,
529 "magnet_uri": entry.magnet_uri,
530 "imported_at": now,
531 });
532 fs::write(
533 meta_path,
534 serde_json::to_string_pretty(&meta).map_err(|e| e.to_string())?,
535 )
536 .map_err(|e| e.to_string())?;
537
538 Ok(WorkbenchImportResult {
539 entry,
540 compress_ratio: stats.ratio,
541 source_removed,
542 })
543}
544
545pub fn list_workbench_entries(storage_root: &Path) -> Result<Vec<OntologyWorkbenchEntry>, String> {
547 load_entries(storage_root)
548}
549
550pub fn set_torrent_policy(
552 storage_root: &Path,
553 ontology_id: &str,
554 policy: OntologyTorrentPolicy,
555) -> Result<OntologyWorkbenchEntry, String> {
556 let mut entries = load_entries(storage_root)?;
557 let idx = entries
558 .iter()
559 .position(|e| e.ontology_id == ontology_id)
560 .ok_or_else(|| format!("Workbench entry not found: {ontology_id}"))?;
561 entries[idx].torrent = policy.clone();
562 if policy.seed_enabled && entries[idx].seed_active {
563 refresh_entry_magnet(&mut entries[idx]);
564 let _ = register_seed_on_daemon(&entries[idx]);
565 }
566 let updated = entries[idx].clone();
567 write_entries(storage_root, &entries)?;
568 Ok(updated)
569}
570
571pub fn set_seed_active(
573 storage_root: &Path,
574 ontology_id: &str,
575 active: bool,
576) -> Result<OntologyWorkbenchEntry, String> {
577 let mut entries = load_entries(storage_root)?;
578 let idx = entries
579 .iter()
580 .position(|e| e.ontology_id == ontology_id)
581 .ok_or_else(|| format!("Workbench entry not found: {ontology_id}"))?;
582 let should_seed = active && entries[idx].torrent.seed_enabled;
583 entries[idx].seed_active = should_seed;
584 refresh_entry_magnet(&mut entries[idx]);
585 if should_seed {
586 register_seed_on_daemon(&entries[idx])?;
587 } else {
588 let _ = unregister_seed_on_daemon(&entries[idx].info_hash_sha1);
589 }
590 let updated = entries[idx].clone();
591 write_entries(storage_root, &entries)?;
592 Ok(updated)
593}
594
595fn effective_limit_kbps(entry: &OntologyWorkbenchEntry, global: &TorrentBandwidthGlobal) -> u32 {
596 let local = entry.torrent.bandwidth_limit_kbps;
597 let global_limit = global.global_limit_kbps;
598 match (local, global_limit) {
599 (0, 0) => 0,
600 (0, g) => g,
601 (l, 0) => l,
602 (l, g) => l.min(g),
603 }
604}
605
606pub fn torrent_telemetry(storage_root: &Path) -> serde_json::Value {
608 if let Ok(remote) = daemon_get("/torrent/telemetry") {
609 return remote;
610 }
611
612 let entries = load_entries(storage_root).unwrap_or_default();
613 let global = load_bandwidth_policy();
614 let active: Vec<_> = entries.iter().filter(|e| e.seed_active).collect();
615 let seeders = active.len();
616 let total_up: u64 = active.iter().map(|e| e.bytes_uploaded_today).sum();
617 let limit = active
618 .first()
619 .map(|e| effective_limit_kbps(e, &global))
620 .unwrap_or(global.global_limit_kbps);
621 let speed = if limit == 0 {
622 "unlimited".to_string()
623 } else {
624 format!("{limit} KiB/s cap")
625 };
626 serde_json::json!({
627 "seeders": seeders,
628 "leechers": 0,
629 "speed": speed,
630 "status": if seeders > 0 { "seeding" } else { "idle" },
631 "uploaded_today_kb": total_up / 1024,
632 "active_ontologies": active.iter().map(|e| &e.ontology_id).collect::<Vec<_>>(),
633 "global_bandwidth_kbps": global.global_limit_kbps,
634 "metered_mode": global.metered_mode,
635 "seeder": "qualia-daemon (unreachable)",
636 })
637}
638
639pub fn sync_workbench_seeds_to_daemon(storage_root: &Path) -> Result<serde_json::Value, String> {
641 sync_daemon_bandwidth_policy();
642 let entries = load_entries(storage_root)?;
643 let mut registered = 0usize;
644 for entry in &entries {
645 if entry.seed_active && entry.torrent.seed_enabled {
646 register_seed_on_daemon(entry)?;
647 registered += 1;
648 }
649 }
650 daemon_post("/torrent/sync", &serde_json::json!({})).map(|v| {
651 serde_json::json!({
652 "registered": registered,
653 "daemon": v,
654 })
655 })
656}
657
658#[cfg(test)]
659mod tests {
660 use super::*;
661
662 #[test]
663 fn derive_id_from_uri() {
664 let id = derive_ontology_id_from_uri("https://example.org/vocab/foaf.ttl");
665 assert!(id.contains("foaf"));
666 }
667
668 #[test]
669 fn magnet_format() {
670 let m = build_magnet_uri("abc123", "wordnet.c.q42");
671 assert!(m.starts_with("magnet:?xt=urn:btih:abc123"));
672 assert!(m.contains("&ws="));
673 assert!(m.contains("webseed"));
674 }
675
676 #[test]
677 fn session_share_eligibility() {
678 let did = "did:qualia:chat:group:abc123";
679 let mut entry = OntologyWorkbenchEntry {
680 ontology_id: "wordnet".into(),
681 title: "WordNet".into(),
682 source_uri: "https://example.org".into(),
683 domain: "lexicon".into(),
684 c_q42_path: "/tmp/wordnet.c.q42".into(),
685 quin_count: 100,
686 sha256: "sha".into(),
687 info_hash_sha1: "ih".into(),
688 magnet_uri: "magnet:?xt=urn:btih:ih".into(),
689 imported_at: 0,
690 torrent: OntologyTorrentPolicy {
691 share_enabled: true,
692 audience: ShareAudience::ChatSessions,
693 allowed_session_dids: vec![did.to_string()],
694 ..OntologyTorrentPolicy::default()
695 },
696 seed_active: false,
697 bytes_uploaded_total: 0,
698 bytes_uploaded_today: 0,
699 uploaded_day_epoch: 0,
700 };
701 assert!(session_eligible_for_share(&entry, did));
702 assert!(!session_eligible_for_share(
703 &entry,
704 "did:qualia:chat:solo:other"
705 ));
706 entry.torrent.share_enabled = false;
707 assert!(!session_eligible_for_share(&entry, did));
708 }
709}