#![cfg(feature = "storage")]
use anyhow::{Error, Result};
use reqwest::{Client, Error as ReqwestError, Response};
use std::fs::File;
use std::io::prelude::*;
use crate::storage::SupabaseStorage;
impl SupabaseStorage {
pub async fn download(&self) -> Result<Vec<u8>, ReqwestError> {
let url: String = format!(
"{}/storage/v1/object/public/{}/{}",
self.supabase_url, self.bucket_name, self.filename
);
let client: Client = Client::new();
let response: Response = client.get(&url).send().await?;
let bytes = response.bytes().await?;
Ok(bytes.to_vec())
}
pub async fn save(&self, file_path: &str) -> Result<(), Error> {
let bytes: Vec<u8> = self.download().await.map_err(Error::new)?;
let mut file: File = File::create(file_path)?;
file.write_all(&bytes)?;
Ok(())
}
}