1use deaddrop_core::chunk::ErasureSpec;
4use deaddrop_core::config::Config;
5use deaddrop_core::event::Event;
6use deaddrop_core::{NodeMode, ObjectId, PeerId, Priority, Result};
7pub use deaddrop_net::SendOpts;
8use deaddrop_net::{InboxItem, Node};
9use std::path::{Path, PathBuf};
10
11pub struct DeadDropBuilder {
12 dir: Option<PathBuf>,
13 mode: Option<NodeMode>,
14}
15
16impl DeadDropBuilder {
17 pub fn new() -> Self {
18 Self {
19 dir: None,
20 mode: None,
21 }
22 }
23
24 pub fn data_dir(mut self, p: impl Into<PathBuf>) -> Self {
25 self.dir = Some(p.into());
26 self
27 }
28
29 pub fn mode(mut self, mode: NodeMode) -> Self {
30 self.mode = Some(mode);
31 self
32 }
33
34 pub fn build(self) -> Result<DeadDrop> {
35 let dir = self.dir.unwrap_or_else(default_dir);
36 let mut cfg = Config::load(&dir.join("deaddrop.toml"))?;
37 if let Some(m) = self.mode {
38 cfg.node.mode = m;
39 }
40 Ok(DeadDrop {
41 node: Node::open(&dir, cfg)?,
42 })
43 }
44}
45
46impl Default for DeadDropBuilder {
47 fn default() -> Self {
48 Self::new()
49 }
50}
51
52pub struct DeadDrop {
53 pub(crate) node: Node,
54}
55
56pub struct Recipient(pub String);
57
58impl Recipient {
59 pub fn from(s: impl Into<String>) -> Self {
60 Self(s.into())
61 }
62}
63
64impl DeadDrop {
65 pub fn builder() -> DeadDropBuilder {
66 DeadDropBuilder::new()
67 }
68
69 pub fn connect() -> Result<Self> {
72 Self::builder().build()
73 }
74
75 pub fn open_dir(dir: &Path) -> Result<Self> {
76 Self::builder().data_dir(dir.to_path_buf()).build()
77 }
78
79 pub async fn send(&self, to: Recipient, payload: impl AsRef<[u8]>) -> Result<ObjectId> {
80 self.node.send_payload(
81 &to.0,
82 payload.as_ref().to_vec(),
83 false,
84 None,
85 Priority::Normal,
86 "dd.file",
87 None,
88 false,
89 )
90 }
91
92 pub async fn send_file(&self, path: impl AsRef<Path>, to: Recipient) -> Result<ObjectId> {
93 self.node
94 .send_file(path.as_ref(), &to.0, false, None, Priority::Normal)
95 }
96
97 pub fn send_with(&self, opts: SendOpts) -> Result<ObjectId> {
98 self.node.send_opts(opts)
99 }
100
101 pub fn receive(&self) -> Result<Vec<InboxItem>> {
102 self.node.inbox()
103 }
104
105 pub fn events(&self) -> Vec<Event> {
106 self.node.events()
107 }
108
109 pub fn peer_id(&self) -> PeerId {
110 self.node.identity.peer_id
111 }
112
113 pub fn node(&self) -> &Node {
114 &self.node
115 }
116}
117
118pub fn erasure(data_shards: u32, parity_shards: u32) -> ErasureSpec {
119 ErasureSpec {
120 data_shards,
121 parity_shards,
122 }
123}
124
125fn default_dir() -> PathBuf {
126 std::env::var("DEADDROP_HOME")
127 .map(PathBuf::from)
128 .unwrap_or_else(|_| std::env::temp_dir().join("deaddrop"))
129}
130
131pub mod ffi;