Skip to main content

fauna_api/
drive.rs

1use std::sync::Arc;
2
3use chrono::{DateTime, Utc};
4use futures_util::{StreamExt, stream};
5use reqwest::header::{CONTENT_LENGTH, CONTENT_TYPE};
6use serde::de::DeserializeOwned;
7use serde_json::{Value, json};
8
9use crate::authentification::{CredentialsOrApiKey, Profile};
10use crate::error::{Error, Result};
11use crate::http::{client, ok};
12
13const DIRECTORY: &str = "application/x-directory";
14const UPLOAD_CHUNK: usize = 64 * 1024;
15
16/// Progress callback, called with a fraction from 0 to 1.
17pub type Progress = Arc<dyn Fn(f64) + Send + Sync>;
18
19pub struct File {
20    pub authorization: DriveAuthorization,
21
22    pub content_type: String,
23    pub content_length: u64,
24
25    pub access_date: DateTime<Utc>,
26    pub create_date: DateTime<Utc>,
27
28    name: String,
29    online: bool,
30
31    data: Option<Vec<u8>>,
32    preview_data: Option<Option<Vec<u8>>>,
33}
34
35fn authorization_header(authorization: &CredentialsOrApiKey) -> String {
36    match authorization {
37        CredentialsOrApiKey::Credentials(credentials) => format!("Token {}", credentials.token),
38        CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
39    }
40}
41
42fn status_error(status: u16) -> Error {
43    match status {
44        401 => Error::Message(format!("{status}: Unauthorized request.")),
45        403 => Error::Message(format!("{status}: Forbidden request.")),
46        _ => Error::Message(format!("{status}: Bad request.")),
47    }
48}
49
50impl File {
51    #[allow(clippy::too_many_arguments)]
52    fn new(
53        authorization: DriveAuthorization,
54        name: String,
55        online: bool,
56        content_type: String,
57        content_length: u64,
58        access_date: DateTime<Utc>,
59        create_date: DateTime<Utc>,
60        data: Option<Vec<u8>>,
61    ) -> Self {
62        File {
63            authorization,
64            name,
65            online,
66            content_type,
67            content_length,
68            access_date,
69            create_date,
70            data,
71            preview_data: None,
72        }
73    }
74
75    pub fn from_json(json: &Value, authorization: DriveAuthorization) -> File {
76        let now = Utc::now();
77        let date = |key: &str| crate::tickets::parse_date(&json[key]).unwrap_or(now);
78        let content_length = match &json["contentLength"] {
79            Value::String(length) => length.parse().unwrap_or(0),
80            length => length.as_f64().unwrap_or(0.0) as u64,
81        };
82        File::new(
83            authorization,
84            json["name"].as_str().unwrap_or_default().to_string(),
85            true,
86            json["contentType"]
87                .as_str()
88                .unwrap_or("application/octet-stream")
89                .to_string(),
90            content_length,
91            date("accessDate"),
92            date("createDate"),
93            None,
94        )
95    }
96
97    pub fn name(&self) -> &str {
98        &self.name
99    }
100
101    pub fn online(&self) -> bool {
102        self.online
103    }
104
105    fn is_directory(&self) -> bool {
106        self.content_type == DIRECTORY
107    }
108
109    /// Authorization plus either Path (+ Group) or Access-Code, with the
110    /// trailing slash directories need.
111    fn file_request(&self, request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
112        match &self.authorization {
113            DriveAuthorization::Path {
114                authorization,
115                path,
116                group,
117            } => {
118                let request = request
119                    .header("Authorization", authorization_header(authorization))
120                    .header(
121                        "Path",
122                        if self.is_directory() {
123                            format!("{path}/")
124                        } else {
125                            path.clone()
126                        },
127                    );
128                match group {
129                    Some(group) => request.header("Group", group),
130                    None => request,
131                }
132            }
133            DriveAuthorization::AccessCode {
134                authorization,
135                access_code,
136            } => request
137                .header("Authorization", authorization_header(authorization))
138                .header("Access-Code", access_code),
139        }
140    }
141
142    pub async fn download(
143        &mut self,
144        revalidate: bool,
145        on_progress: Option<Progress>,
146    ) -> Result<Vec<u8>> {
147        if let Some(data) = &self.data
148            && !revalidate
149        {
150            return Ok(data.clone());
151        }
152
153        let response = self
154            .file_request(client().get(format!("{}/file/download", crate::get_url())))
155            .send()
156            .await?;
157
158        if !response.status().is_success() {
159            return Err(status_error(response.status().as_u16()));
160        }
161
162        let content_length = self.content_length;
163        let on_loaded = on_progress.clone().map(|progress| {
164            move |loaded: u64| {
165                if content_length > 0 {
166                    progress((loaded as f64 / content_length as f64).clamp(0.0, 1.0));
167                }
168            }
169        });
170        let buffer = parse_stream(response, on_loaded).await?;
171        if let Some(progress) = on_progress {
172            progress(1.0);
173        }
174
175        let data = buffer.into_iter().next().unwrap_or_default();
176        self.data = Some(data.clone());
177        Ok(data)
178    }
179
180    // Fetch a server-generated preview image (image/webp) for this file, or
181    // None when the file type has no meaningful preview. Cached per file.
182    pub async fn preview(&mut self, revalidate: bool) -> Result<Option<Vec<u8>>> {
183        if let Some(preview) = &self.preview_data
184            && !revalidate
185        {
186            return Ok(preview.clone());
187        }
188
189        let response = self
190            .file_request(client().get(format!("{}/file/preview", crate::get_url())))
191            .send()
192            .await?;
193
194        if response.status().as_u16() == 204 {
195            self.preview_data = Some(None);
196            return Ok(None);
197        }
198
199        if !response.status().is_success() {
200            let status = response.status().as_u16();
201            if status == 401 || status == 403 {
202                return Err(status_error(status));
203            }
204            // Soft-fail (incl. 429): cache miss so this File does not hammer.
205            self.preview_data = Some(None);
206            return Ok(None);
207        }
208
209        let buffer = parse_stream(response, None::<fn(u64)>).await?;
210        let data = buffer.into_iter().next().unwrap_or_default();
211        self.preview_data = Some(Some(data.clone()));
212        Ok(Some(data))
213    }
214
215    pub async fn upload(&mut self, on_progress: Option<Progress>) -> Result<()> {
216        let DriveAuthorization::Path {
217            authorization,
218            path,
219            group,
220        } = &self.authorization
221        else {
222            return Err(Error::Message("401: Unauthorized request.".to_string()));
223        };
224
225        let body: Vec<u8> = if self.is_directory() {
226            b"Folder".to_vec()
227        } else {
228            match &self.data {
229                Some(data) => data.clone(),
230                None => return Err(Error::Message("400: No data to upload.".to_string())),
231            }
232        };
233
234        let total = body.len();
235        let chunks: Vec<Vec<u8>> = body.chunks(UPLOAD_CHUNK).map(<[u8]>::to_vec).collect();
236        let mut sent = 0usize;
237        let progress = on_progress.clone();
238        let stream = stream::iter(chunks).map(move |chunk| {
239            sent += chunk.len();
240            if let Some(progress) = &progress
241                && total > 0
242            {
243                progress(sent as f64 / total as f64);
244            }
245            Ok::<_, std::io::Error>(chunk)
246        });
247
248        let request = client()
249            .post(format!("{}/file/upload", crate::get_url()))
250            .header("Authorization", authorization_header(authorization))
251            .header(
252                "Path",
253                if self.is_directory() {
254                    format!("{path}/")
255                } else {
256                    path.clone()
257                },
258            );
259        let request = match group {
260            Some(group) => request.header("Group", group),
261            None => request,
262        };
263        let content_type = if self.content_type.is_empty() {
264            "application/octet-stream".to_string()
265        } else {
266            self.content_type.clone()
267        };
268        let response = request
269            .header(CONTENT_TYPE, content_type)
270            .header(CONTENT_LENGTH, total)
271            .body(reqwest::Body::wrap_stream(stream))
272            .send()
273            .await
274            .map_err(|_| Error::Message("Failed to upload file.".to_string()))?;
275
276        if let Some(progress) = on_progress {
277            progress(1.0);
278        }
279
280        if response.status().is_success() {
281            self.online = true;
282            return Ok(());
283        }
284        Err(status_error(response.status().as_u16()))
285    }
286
287    pub async fn copy(&self, path: &str) -> Result<()> {
288        if matches!(self.authorization, DriveAuthorization::AccessCode { .. }) {
289            return Err(Error::Message("401: Unauthorized request.".to_string()));
290        }
291
292        let response = self
293            .file_request(client().post(format!("{}/file/copy", crate::get_url())))
294            .json(&json!({ "copyPath": path }))
295            .send()
296            .await?;
297        ok(response).await?;
298        Ok(())
299    }
300
301    pub async fn text(&mut self) -> Result<String> {
302        Ok(String::from_utf8_lossy(&self.download(false, None).await?).into_owned())
303    }
304
305    pub async fn json<T: DeserializeOwned>(&mut self) -> Result<T> {
306        Ok(serde_json::from_str(&self.text().await?)?)
307    }
308
309    pub async fn remove(&mut self) -> Result<()> {
310        let DriveAuthorization::Path {
311            authorization,
312            path,
313            group,
314        } = &self.authorization
315        else {
316            return Err(Error::Message("401: Unauthorized request.".to_string()));
317        };
318        let path = if self.is_directory() {
319            format!("{path}/")
320        } else {
321            path.clone()
322        };
323        remove(authorization, &path, group.as_deref()).await?;
324        self.data = None;
325        self.online = false;
326        Ok(())
327    }
328
329    pub async fn generate_access_code(&self, expiration: Option<u64>) -> Result<String> {
330        if matches!(self.authorization, DriveAuthorization::AccessCode { .. }) {
331            return Err(Error::Message("401: Unauthorized request.".to_string()));
332        }
333        let response = self
334            .file_request(client().post(format!("{}/file/access-code", crate::get_url())))
335            .json(&json!({ "expiration": expiration }))
336            .send()
337            .await?;
338        Ok(ok(response).await?.text().await?)
339    }
340
341    pub async fn owner(&self) -> Result<Profile> {
342        let response = correct_header(
343            &self.authorization,
344            client().get(format!("{}/file/owner", crate::get_url())),
345        )
346        .send()
347        .await?;
348        Ok(ok(response).await?.json().await?)
349    }
350}
351
352#[derive(Debug, Clone)]
353pub enum DriveAuthorization {
354    Path {
355        authorization: CredentialsOrApiKey,
356        path: String,
357        group: Option<String>,
358    },
359    AccessCode {
360        authorization: CredentialsOrApiKey,
361        access_code: String,
362    },
363}
364
365/// `Partial<{ accessDate, createDate }>` in the TypeScript SDK.
366#[derive(Debug, Clone, Copy, Default)]
367pub struct FileDates {
368    pub access_date: Option<DateTime<Utc>>,
369    pub create_date: Option<DateTime<Utc>>,
370}
371
372/// The `meta` argument of `fromAccessCode`.
373#[derive(Debug, Clone, Default)]
374pub struct FileMeta {
375    pub name: String,
376    pub content_type: String,
377    pub content_length: Option<u64>,
378    pub access_date: Option<DateTime<Utc>>,
379    pub create_date: Option<DateTime<Utc>>,
380}
381
382fn correct_header(
383    authorization: &DriveAuthorization,
384    request: reqwest::RequestBuilder,
385) -> reqwest::RequestBuilder {
386    match authorization {
387        DriveAuthorization::Path {
388            authorization,
389            path,
390            group,
391        } => {
392            let request = request
393                .header("Authorization", authorization_header(authorization))
394                .header("Path", path);
395            match group {
396                Some(group) => request.header("Group", group),
397                None => request,
398            }
399        }
400        DriveAuthorization::AccessCode {
401            authorization,
402            access_code,
403        } => request
404            .header("Authorization", authorization_header(authorization))
405            .header("Access-Code", access_code),
406    }
407}
408
409pub async fn list(
410    authorization: &CredentialsOrApiKey,
411    path: &str,
412    group: Option<&str>,
413) -> Result<Vec<String>> {
414    let request = client()
415        .get(format!("{}/file/list", crate::get_url()))
416        .header("Authorization", authorization_header(authorization))
417        .header("Path", path);
418    let request = match group {
419        Some(group) => request.header("Group", group),
420        None => request,
421    };
422    let response = request.send().await?;
423    Ok(ok(response).await?.json().await?)
424}
425
426/// Builds an in-memory file from bytes the caller already holds (the browser
427/// SDK takes a `File`). An empty `content_type` is inferred from the name.
428pub async fn wrap(
429    authorization: DriveAuthorization,
430    name: &str,
431    content_type: &str,
432    data: Vec<u8>,
433    options: FileDates,
434) -> Result<File> {
435    let now = Utc::now();
436    let content_type = if content_type.is_empty() {
437        mime_guess::from_path(name)
438            .first()
439            .map(|mime| mime.essence_str().to_string())
440            .unwrap_or_else(|| "application/octet-stream".to_string())
441    } else {
442        content_type.to_string()
443    };
444    Ok(File::new(
445        authorization,
446        name.to_string(),
447        false,
448        content_type,
449        data.len() as u64,
450        options.access_date.unwrap_or(now),
451        options.create_date.unwrap_or(now),
452        Some(data),
453    ))
454}
455
456/// Build an in-memory File from raw bytes (not yet uploaded). `path` is
457/// the Drive destination (e.g. `tmp/${uuid}-${name}`); the display name is the
458/// last path segment. The file is offline until an explicit `upload()`.
459pub fn from_bytes(
460    authorization: CredentialsOrApiKey,
461    path: &str,
462    content_type: &str,
463    data: Vec<u8>,
464    options: FileDates,
465) -> File {
466    let now = Utc::now();
467    let name = path
468        .rsplit('/')
469        .next()
470        .filter(|name| !name.is_empty())
471        .unwrap_or(path);
472    File::new(
473        DriveAuthorization::Path {
474            authorization,
475            path: path.to_string(),
476            group: None,
477        },
478        name.to_string(),
479        false,
480        content_type.to_string(),
481        data.len() as u64,
482        options.access_date.unwrap_or(now),
483        options.create_date.unwrap_or(now),
484        Some(data),
485    )
486}
487
488/// Build a streaming File addressed by a per-file access code. Bytes are
489/// pulled on demand via `download()` (the `Access-Code` path). Used to hydrate
490/// chat message FILE parts returned by the server (each carries its own code),
491/// so members stream file bytes uniformly without a host-drive path.
492pub fn from_access_code(
493    authorization: CredentialsOrApiKey,
494    access_code: &str,
495    meta: FileMeta,
496) -> File {
497    let now = Utc::now();
498    File::new(
499        DriveAuthorization::AccessCode {
500            authorization,
501            access_code: access_code.to_string(),
502        },
503        meta.name,
504        true,
505        meta.content_type,
506        meta.content_length.unwrap_or(0),
507        meta.access_date.unwrap_or(now),
508        meta.create_date.unwrap_or(now),
509        None,
510    )
511}
512
513pub async fn folder(authorization: DriveAuthorization, options: FileDates) -> Result<File> {
514    let DriveAuthorization::Path { path, .. } = &authorization else {
515        return Err(Error::Message("401: Unauthorized request.".to_string()));
516    };
517
518    let normalized = path.replace('\\', "/");
519    let normalized = normalized.trim_end_matches('/');
520    let name = normalized
521        .rsplit('/')
522        .next()
523        .unwrap_or_default()
524        .to_string();
525
526    let now = Utc::now();
527    Ok(File::new(
528        authorization,
529        name,
530        false,
531        DIRECTORY.to_string(),
532        0,
533        options.access_date.unwrap_or(now),
534        options.create_date.unwrap_or(now),
535        None,
536    ))
537}
538
539pub async fn pull(authorization: DriveAuthorization) -> Result<File> {
540    let response = correct_header(
541        &authorization,
542        client().get(format!("{}/file/pull", crate::get_url())),
543    )
544    .send()
545    .await?;
546    let json: Value = ok(response).await?.json().await?;
547    Ok(File::from_json(&json, authorization))
548}
549
550pub async fn remove(
551    authorization: &CredentialsOrApiKey,
552    path: &str,
553    group: Option<&str>,
554) -> Result<bool> {
555    let request = client()
556        .delete(format!("{}/file", crate::get_url()))
557        .header("Authorization", authorization_header(authorization))
558        .header("Path", path);
559    let request = match group {
560        Some(group) => request.header("Group", group),
561        None => request,
562    };
563    let response = request.send().await?;
564    ok(response).await?;
565    Ok(true)
566}
567
568/// Split a Drive style response into its parts. With `Encoding-Size` above 1 the
569/// body is a `--BOUNDARY` separated multipart and this returns every part.
570pub async fn parse_stream(
571    response: reqwest::Response,
572    on_loaded: Option<impl Fn(u64)>,
573) -> Result<Vec<Vec<u8>>> {
574    let encoding_size = response
575        .headers()
576        .get("Encoding-Size")
577        .and_then(|value| value.to_str().ok())
578        .and_then(|value| value.trim().parse::<i64>().ok());
579
580    let mut buffer = Vec::new();
581    let mut loaded = 0u64;
582    let mut body = response.bytes_stream();
583    while let Some(chunk) = body.next().await {
584        let chunk = chunk?;
585        loaded += chunk.len() as u64;
586        buffer.extend_from_slice(&chunk);
587        if let Some(on_loaded) = &on_loaded {
588            on_loaded(loaded);
589        }
590    }
591
592    if encoding_size.is_none_or(|size| size <= 1) {
593        return Ok(vec![buffer]);
594    }
595
596    const BOUNDARY: &[u8] = b"--BOUNDARY";
597    let mut parts = Vec::new();
598    let mut current = 0;
599    while let Some(offset) = buffer[current..]
600        .windows(BOUNDARY.len())
601        .position(|window| window == BOUNDARY)
602    {
603        let index = current + offset;
604        if index > current {
605            parts.push(buffer[current..index].to_vec());
606        }
607        current = index + BOUNDARY.len();
608    }
609    if current < buffer.len() {
610        parts.push(buffer[current..].to_vec());
611    }
612    Ok(parts)
613}
614
615#[cfg(test)]
616mod tests {
617    use super::parse_stream;
618
619    fn response(encoding_size: Option<&str>, body: &'static [u8]) -> reqwest::Response {
620        let mut builder = http::Response::builder();
621        if let Some(size) = encoding_size {
622            builder = builder.header("Encoding-Size", size);
623        }
624        reqwest::Response::from(builder.body(body).unwrap())
625    }
626
627    #[tokio::test]
628    async fn single_part_bodies_come_back_whole() {
629        let parts = parse_stream(response(None, b"hello"), None::<fn(u64)>)
630            .await
631            .unwrap();
632        assert_eq!(parts, vec![b"hello".to_vec()]);
633    }
634
635    #[tokio::test]
636    async fn multi_part_bodies_split_on_the_boundary() {
637        let loaded = std::sync::Mutex::new(0);
638        let parts = parse_stream(
639            response(Some("2"), b"--BOUNDARYfirst--BOUNDARYsecond"),
640            Some(|bytes| *loaded.lock().unwrap() = bytes),
641        )
642        .await
643        .unwrap();
644        assert_eq!(parts, vec![b"first".to_vec(), b"second".to_vec()]);
645        assert_eq!(*loaded.lock().unwrap(), 31);
646    }
647}