1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! `Cua.shares()`: links to things you run (`cua share`). The cua daemon
//! owns the shares, so a link keeps working after the process that made it
//! exits; an embedded SDK asks the daemon (starting one if needed).
use super::{Backend, Cua, run};
use crate::Result;
use std::time::{SystemTime, UNIX_EPOCH};
/// Who may open a share.
#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
pub enum ShareAccess {
/// Only the owner's cua.ai account, on any of the owner's devices.
Private,
/// Members of the owner's team.
Team,
/// Anyone with the link, until it expires or is stopped.
Public,
}
impl From<ShareAccess> for cua_daemon::ShareAccess {
fn from(a: ShareAccess) -> Self {
match a {
ShareAccess::Private => Self::Private,
ShareAccess::Team => Self::Team,
ShareAccess::Public => Self::Public,
}
}
}
impl From<cua_daemon::ShareAccess> for ShareAccess {
fn from(a: cua_daemon::ShareAccess) -> Self {
match a {
cua_daemon::ShareAccess::Private => Self::Private,
cua_daemon::ShareAccess::Team => Self::Team,
cua_daemon::ShareAccess::Public => Self::Public,
}
}
}
/// One share.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct Share {
/// Handle for `Shares.stop` (`share-...`).
pub id: String,
/// The link.
pub url: String,
/// Name for lists.
pub name: Option<String>,
/// Who may open it.
pub access: ShareAccess,
/// `port`, `space_port`, `file` or `dir`.
pub kind: String,
/// What it serves (`port 3000`, `space:dev:3000`, a path).
pub target: String,
/// The account that made it.
pub owner: String,
/// When it was made (unix seconds).
pub created_at_unix: Option<i64>,
/// When it ends (unix seconds); none: until stopped.
pub expires_at_unix: Option<i64>,
/// Limits of this link worth showing next to it (for example that
/// browsers cannot open it yet).
pub notices: Vec<String>,
}
fn unix(t: SystemTime) -> i64 {
t.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
impl From<cua_daemon::ShareInfo> for Share {
fn from(s: cua_daemon::ShareInfo) -> Self {
use cua_daemon::ShareTarget as T;
let kind = match &s.target {
T::Port(_) => "port",
T::SpacePort { .. } => "space_port",
T::File(_) => "file",
T::Dir(_) => "dir",
};
Share {
id: s.id,
url: s.url,
name: s.name,
access: s.access.into(),
kind: kind.into(),
target: s.target.describe(),
owner: s.owner,
created_at_unix: s.created_at.map(unix),
expires_at_unix: s.expires_at.map(unix),
notices: s.notices,
}
}
}
/// What opening a link once returned.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct ShareCheck {
/// 2xx.
pub ok: bool,
/// HTTP status (0: the link could not be opened).
pub status: u32,
/// `link` (the real link, through the relay, as the owner) or `local`.
pub via: String,
/// A short human line.
pub detail: String,
}
impl From<cua_daemon::ShareCheck> for ShareCheck {
fn from(c: cua_daemon::ShareCheck) -> Self {
ShareCheck {
ok: c.ok,
status: c.status as u32,
via: c.via,
detail: c.detail,
}
}
}
async fn daemon(backend: &Backend) -> Result<cua_daemon::client::DaemonClient> {
match backend {
Backend::Daemon(d) => Ok(d.clone()),
Backend::Embedded(_) => Ok(cua_daemon::client::ensure_daemon().await?),
}
}
#[uniffi::export]
impl Cua {
/// Shares `target` and returns a link that opens on the owner's other
/// signed-in devices, never a localhost link: a port (`"3000"`), a port in
/// a Space (`"space:dev:3000"`), or a file or folder (`"./report.html"`,
/// read-only; relative paths resolve against this process's directory).
/// `access` defaults to private; a public share lasts one hour unless
/// `ttl_seconds` (60 to 86400) says otherwise. The cua daemon owns the
/// share, so it keeps working after this process exits.
#[uniffi::method(default(access = None, ttl_seconds = None, name = None))]
pub async fn share(
&self,
target: String,
access: Option<ShareAccess>,
ttl_seconds: Option<u32>,
name: Option<String>,
) -> Result<Share> {
let backend = self.backend.clone();
let cwd =
std::env::current_dir().map_err(|e| crate::CuaError::InvalidArgument(e.to_string()))?;
let parsed = cua_daemon::ShareTarget::parse(&target, &cwd)?;
let ttl = ttl_seconds.map(super::secs);
run(async move {
let d = daemon(&backend).await?;
let s = d
.create_share(
&parsed,
access.unwrap_or(ShareAccess::Private).into(),
ttl,
name,
)
.await?;
Ok(s.into())
})
.await
}
/// The active shares, newest first.
pub async fn shares(&self) -> Result<Vec<Share>> {
let backend = self.backend.clone();
run(async move {
let d = daemon(&backend).await?;
Ok(d.list_shares()
.await?
.into_iter()
.map(Share::from)
.collect())
})
.await
}
/// Stops a share (its id or its name). The link stops working at once.
/// Returns the ids stopped.
pub async fn stop_share(&self, id: String) -> Result<Vec<String>> {
let backend = self.backend.clone();
run(async move {
let d = daemon(&backend).await?;
Ok(d.stop_share(Some(&id), false).await?)
})
.await
}
/// Stops every share. Returns the ids stopped.
pub async fn stop_all_shares(&self) -> Result<Vec<String>> {
let backend = self.backend.clone();
run(async move {
let d = daemon(&backend).await?;
Ok(d.stop_share(None, true).await?)
})
.await
}
/// Opens a share's link once, as its owner, so a link is known to work
/// before it is handed to anyone. `path` is below the link.
#[uniffi::method(default(path = None))]
pub async fn check_share(&self, id: String, path: Option<String>) -> Result<ShareCheck> {
let backend = self.backend.clone();
run(async move {
let d = daemon(&backend).await?;
Ok(d.check_share(&id, path.as_deref()).await?.into())
})
.await
}
}