use std::sync::Arc;
use crate::Error;
use bytes::Bytes;
use reqwest::Client;
use serde::Deserialize;
use url::Url;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint {
Frankfurt,
London,
NewYork,
LosAngeles,
Singapore,
Stockholm,
SaoPaulo,
Johannesburg,
Sydney,
Custom(String),
}
impl TryInto<Url> for Endpoint {
type Error = Error;
fn try_into(self) -> Result<Url, Error> {
match self {
Endpoint::Frankfurt => Ok(Url::parse("https://storage.bunnycdn.com")?),
Endpoint::London => Ok(Url::parse("https://uk.storage.bunnycdn.com")?),
Endpoint::NewYork => Ok(Url::parse("https://ny.storage.bunnycdn.com")?),
Endpoint::LosAngeles => Ok(Url::parse("https://la.storage.bunnycdn.com")?),
Endpoint::Singapore => Ok(Url::parse("https://sg.storage.bunnycdn.com")?),
Endpoint::Stockholm => Ok(Url::parse("https://se.storage.bunnycdn.com")?),
Endpoint::SaoPaulo => Ok(Url::parse("https://br.storage.bunnycdn.com")?),
Endpoint::Johannesburg => Ok(Url::parse("https://jh.storage.bunnycdn.com")?),
Endpoint::Sydney => Ok(Url::parse("https://syd.storage.bunnycdn.com")?),
Endpoint::Custom(url) => Ok(Url::parse(&url)?),
}
}
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct ListFile {
pub guid: String,
pub storage_zone_name: String,
pub path: String,
pub object_name: String,
pub length: u32,
pub last_changed: String,
pub server_id: u32,
pub array_number: u32,
pub is_directory: bool,
pub user_id: String,
pub content_type: String,
pub date_created: String,
pub storage_zone_id: u32,
pub checksum: String,
pub replicated_zones: String,
}
#[derive(Debug, Clone)]
pub struct Storage {
pub(crate) url: Url,
pub(crate) reqwest: Arc<Client>,
}
impl<'a> Storage {
pub fn init<T: AsRef<str>>(
&mut self,
endpoint: Endpoint,
storage_zone: T,
) -> Result<(), Error> {
let endpoint: Url = endpoint.try_into()?;
let storage_zone = String::from("/") + storage_zone.as_ref() + "/";
self.url = endpoint.join(&storage_zone)?;
Ok(())
}
pub async fn upload<T: AsRef<str>>(&self, path: T, file: Bytes) -> Result<(), Error> {
let response = self
.reqwest
.put(self.url.join(path.as_ref())?)
.header("Content-Type", "application/octet-stream")
.body(file)
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
} else if response.status().as_u16() == 400 {
return Err(Error::BadRequest(response.text().await?));
}
Ok(())
}
pub async fn download<T: AsRef<str>>(&self, path: T) -> Result<Bytes, Error> {
let response = self
.reqwest
.get(self.url.join(path.as_ref())?)
.header("accept", "*/*")
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
} else if response.status().as_u16() == 404 {
return Err(Error::NotFound(response.text().await?));
}
Ok(response.bytes().await?)
}
pub async fn delete<T: AsRef<str>>(&self, path: T) -> Result<(), Error> {
let response = self
.reqwest
.delete(self.url.join(path.as_ref())?)
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
} else if response.status().as_u16() == 400 {
return Err(Error::BadRequest(response.text().await?));
}
Ok(())
}
pub async fn list<T: AsRef<str>>(&self, path: T) -> Result<Vec<ListFile>, Error> {
let response = self
.reqwest
.get(self.url.join(path.as_ref())?)
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
} else if response.status().as_u16() == 400 {
return Err(Error::BadRequest(response.text().await?));
}
Ok(response.json().await?)
}
}