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