1use std::time::SystemTime;
2
3use async_trait::async_trait;
4use bytes::Bytes;
5use stoat_models::v0::File;
6
7use crate::{HttpClient, Identifiable, Result, created_at};
8
9#[async_trait]
10pub trait FileExt {
11 fn url(&self, http: impl AsRef<HttpClient>, preview: bool) -> String;
12 async fn bytes(&self, http: impl AsRef<HttpClient> + Send, preview: bool) -> Result<Bytes>;
13}
14
15#[async_trait]
16impl FileExt for File {
17 fn url(&self, http: impl AsRef<HttpClient>, preview: bool) -> String {
18 http.as_ref()
19 .format_file_url(&self.tag, &self.id, preview.then_some(&self.filename))
20 }
21
22 async fn bytes(&self, http: impl AsRef<HttpClient> + Send, preview: bool) -> Result<Bytes> {
23 if preview {
24 http.as_ref().fetch_image_preview(&self.tag, &self.id).await
25 } else {
26 http.as_ref()
27 .fetch_image(&self.tag, &self.id, &self.filename)
28 .await
29 }
30 }
31}
32
33impl Identifiable for File {
34 fn created_at(&self) -> SystemTime {
35 created_at(&self.id)
36 }
37}