-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathapi.rs
More file actions
314 lines (277 loc) · 9.03 KB
/
api.rs
File metadata and controls
314 lines (277 loc) · 9.03 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! TODO: We should investigate generating this with OpenAPI.
//! This will come part of the EffectTS rewrite work.
use serde::{Deserialize, Serialize};
use serde_json::json;
use specta::Type;
use tauri::AppHandle;
use tracing::{instrument, trace};
use crate::web_api::{AuthedApiError, ManagerExt};
#[instrument(skip(app))]
pub async fn upload_multipart_initiate(
app: &AppHandle,
video_id: &str,
) -> Result<String, AuthedApiError> {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
upload_id: String,
}
let resp = app
.authed_api_request("/api/upload/multipart/initiate", |c, url| {
c.post(url)
.header("Content-Type", "application/json")
.json(&serde_json::json!({
"videoId": video_id,
"contentType": "video/mp4"
}))
})
.await
.map_err(|err| format!("api/upload_multipart_initiate/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/upload_multipart_initiate/{status}: {error_body}").into());
}
resp.json::<Response>()
.await
.map_err(|err| format!("api/upload_multipart_initiate/response: {err}").into())
.map(|data| data.upload_id)
}
#[instrument(skip(app, upload_id))]
pub async fn upload_multipart_presign_part(
app: &AppHandle,
video_id: &str,
upload_id: &str,
part_number: u32,
md5_sum: Option<&str>,
) -> Result<String, AuthedApiError> {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
presigned_url: String,
}
let mut body = serde_json::Map::from_iter([
("videoId".to_string(), json!(video_id)),
("uploadId".to_string(), json!(upload_id)),
("partNumber".to_string(), json!(part_number)),
]);
if let Some(md5_sum) = md5_sum {
body.insert("md5Sum".to_string(), json!(md5_sum));
}
let resp = app
.authed_api_request("/api/upload/multipart/presign-part", |c, url| {
c.post(url)
.header("Content-Type", "application/json")
.json(&serde_json::json!(body))
})
.await
.map_err(|err| format!("api/upload_multipart_presign_part/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/upload_multipart_presign_part/{status}: {error_body}").into());
}
resp.json::<Response>()
.await
.map_err(|err| format!("api/upload_multipart_presign_part/response: {err}").into())
.map(|data| data.presigned_url)
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UploadedPart {
pub part_number: u32,
pub etag: String,
pub size: usize,
#[serde(skip)]
pub total_size: u64,
}
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct S3VideoMeta {
#[serde(rename = "durationInSecs")]
pub duration_in_secs: f64,
pub width: u32,
pub height: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub fps: Option<f32>,
}
#[instrument(skip_all)]
pub async fn upload_multipart_complete(
app: &AppHandle,
video_id: &str,
upload_id: &str,
parts: &[UploadedPart],
meta: Option<S3VideoMeta>,
) -> Result<Option<String>, AuthedApiError> {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MultipartCompleteRequest<'a> {
video_id: &'a str,
upload_id: &'a str,
parts: &'a [UploadedPart],
#[serde(flatten)]
meta: Option<S3VideoMeta>,
}
#[derive(Deserialize)]
pub struct Response {
location: Option<String>,
}
trace!("Completing multipart upload");
let resp = app
.authed_api_request("/api/upload/multipart/complete", |c, url| {
c.post(url)
.header("Content-Type", "application/json")
.json(&MultipartCompleteRequest {
video_id,
upload_id,
parts,
meta,
})
})
.await
.map_err(|err| format!("api/upload_multipart_complete/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/upload_multipart_complete/{status}: {error_body}").into());
}
resp.json::<Response>()
.await
.map_err(|err| format!("api/upload_multipart_complete/response: {err}").into())
.map(|data| data.location)
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PresignedS3PutRequestMethod {
#[allow(unused)]
Post,
Put,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PresignedS3PutRequest {
pub video_id: String,
pub subpath: String,
pub method: PresignedS3PutRequestMethod,
#[serde(flatten)]
pub meta: Option<S3VideoMeta>,
}
#[instrument(skip(app))]
pub async fn upload_signed(
app: &AppHandle,
body: PresignedS3PutRequest,
) -> Result<String, AuthedApiError> {
#[derive(Deserialize)]
struct Data {
url: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
presigned_put_data: Data,
}
let resp = app
.authed_api_request("/api/upload/signed", |client, url| {
client.post(url).json(&body)
})
.await
.map_err(|err| format!("api/upload_signed/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/upload_signed/{status}: {error_body}").into());
}
resp.json::<Response>()
.await
.map_err(|err| format!("api/upload_signed/response: {err}").into())
.map(|data| data.presigned_put_data.url)
}
#[instrument(skip(app))]
pub async fn desktop_video_progress(
app: &AppHandle,
video_id: &str,
uploaded: u64,
total: u64,
) -> Result<(), AuthedApiError> {
let resp = app
.authed_api_request("/api/desktop/video/progress", |client, url| {
client.post(url).json(&json!({
"videoId": video_id,
"uploaded": uploaded,
"total": total,
"updatedAt": chrono::Utc::now().to_rfc3339()
}))
})
.await
.map_err(|err| format!("api/desktop_video_progress/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/desktop_video_progress/{status}: {error_body}").into());
}
Ok(())
}
#[derive(Serialize, Deserialize, Type, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Organization {
pub id: String,
pub name: String,
pub owner_id: String,
}
pub async fn signal_recording_complete(
app: &AppHandle,
video_id: &str,
) -> Result<(), AuthedApiError> {
let resp = app
.authed_api_request("/api/upload/recording-complete", |client, url| {
client
.post(url)
.header("Content-Type", "application/json")
.json(&serde_json::json!({
"videoId": video_id,
}))
})
.await
.map_err(|err| format!("api/signal_recording_complete/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/signal_recording_complete/{status}: {error_body}").into());
}
Ok(())
}
pub async fn fetch_organizations(app: &AppHandle) -> Result<Vec<Organization>, AuthedApiError> {
let resp = app
.authed_api_request("/api/desktop/organizations", |client, url| client.get(url))
.await
.map_err(|err| format!("api/fetch_organizations/request: {err}"))?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let error_body = resp
.text()
.await
.unwrap_or_else(|_| "<no response body>".to_string());
return Err(format!("api/fetch_organizations/{status}: {error_body}").into());
}
resp.json()
.await
.map_err(|err| format!("api/fetch_organizations/response: {err}").into())
}