2022-03-27 01:45:12 +00:00
|
|
|
use crate::error::Error;
|
2021-10-23 04:48:56 +00:00
|
|
|
use actix_web::web::Bytes;
|
|
|
|
use futures_util::stream::Stream;
|
2022-03-27 01:45:12 +00:00
|
|
|
use std::fmt::Debug;
|
2021-10-23 04:48:56 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
|
|
|
|
|
|
|
pub(crate) mod file_store;
|
2021-10-28 04:06:03 +00:00
|
|
|
pub(crate) mod object_store;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
|
|
|
pub(crate) trait Identifier: Send + Sync + Clone + Debug {
|
2022-03-27 01:45:12 +00:00
|
|
|
fn to_bytes(&self) -> Result<Vec<u8>, Error>;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2022-03-27 01:45:12 +00:00
|
|
|
fn from_bytes(bytes: Vec<u8>) -> Result<Self, Error>
|
2021-10-23 04:48:56 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-09-26 01:59:57 +00:00
|
|
|
|
|
|
|
fn string_repr(&self) -> String;
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
pub(crate) trait StoreConfig: Send + Sync + Clone {
|
|
|
|
type Store: Store;
|
|
|
|
|
|
|
|
fn build(self) -> Self::Store;
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:48:56 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2022-09-24 19:18:49 +00:00
|
|
|
pub(crate) trait Store: Clone + Debug {
|
2022-04-01 21:51:12 +00:00
|
|
|
type Identifier: Identifier + 'static;
|
2022-09-24 22:18:53 +00:00
|
|
|
type Stream: Stream<Item = std::io::Result<Bytes>> + Unpin + 'static;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, Error>
|
|
|
|
where
|
|
|
|
Reader: AsyncRead + Unpin + 'static;
|
2022-09-24 19:18:49 +00:00
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
async fn save_stream<S>(&self, stream: S) -> Result<Self::Identifier, Error>
|
2021-10-23 04:48:56 +00:00
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
S: Stream<Item = std::io::Result<Bytes>> + Unpin + 'static;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2022-03-27 01:45:12 +00:00
|
|
|
async fn save_bytes(&self, bytes: Bytes) -> Result<Self::Identifier, Error>;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
|
|
|
async fn to_stream(
|
|
|
|
&self,
|
|
|
|
identifier: &Self::Identifier,
|
|
|
|
from_start: Option<u64>,
|
|
|
|
len: Option<u64>,
|
2022-03-27 01:45:12 +00:00
|
|
|
) -> Result<Self::Stream, Error>;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
|
|
|
async fn read_into<Writer>(
|
|
|
|
&self,
|
|
|
|
identifier: &Self::Identifier,
|
|
|
|
writer: &mut Writer,
|
|
|
|
) -> Result<(), std::io::Error>
|
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
Writer: AsyncWrite + Unpin;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2022-03-27 01:45:12 +00:00
|
|
|
async fn len(&self, identifier: &Self::Identifier) -> Result<u64, Error>;
|
2021-10-23 04:48:56 +00:00
|
|
|
|
2022-03-27 01:45:12 +00:00
|
|
|
async fn remove(&self, identifier: &Self::Identifier) -> Result<(), Error>;
|
2021-10-23 04:48:56 +00:00
|
|
|
}
|
2022-04-01 21:51:12 +00:00
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl<T> Store for actix_web::web::Data<T>
|
|
|
|
where
|
|
|
|
T: Store,
|
|
|
|
{
|
|
|
|
type Identifier = T::Identifier;
|
|
|
|
type Stream = T::Stream;
|
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, Error>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
Reader: AsyncRead + Unpin + 'static,
|
2022-09-11 00:29:22 +00:00
|
|
|
{
|
2022-09-11 15:04:37 +00:00
|
|
|
T::save_async_read(self, reader).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
async fn save_stream<S>(&self, stream: S) -> Result<Self::Identifier, Error>
|
|
|
|
where
|
|
|
|
S: Stream<Item = std::io::Result<Bytes>> + Unpin + 'static,
|
|
|
|
{
|
|
|
|
T::save_stream(self, stream).await
|
|
|
|
}
|
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
async fn save_bytes(&self, bytes: Bytes) -> Result<Self::Identifier, Error> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::save_bytes(self, bytes).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn to_stream(
|
|
|
|
&self,
|
|
|
|
identifier: &Self::Identifier,
|
|
|
|
from_start: Option<u64>,
|
|
|
|
len: Option<u64>,
|
|
|
|
) -> Result<Self::Stream, Error> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::to_stream(self, identifier, from_start, len).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_into<Writer>(
|
|
|
|
&self,
|
|
|
|
identifier: &Self::Identifier,
|
|
|
|
writer: &mut Writer,
|
|
|
|
) -> Result<(), std::io::Error>
|
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
Writer: AsyncWrite + Unpin,
|
2022-09-11 00:29:22 +00:00
|
|
|
{
|
2022-09-11 15:04:37 +00:00
|
|
|
T::read_into(self, identifier, writer).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn len(&self, identifier: &Self::Identifier) -> Result<u64, Error> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::len(self, identifier).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn remove(&self, identifier: &Self::Identifier) -> Result<(), Error> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::remove(self, identifier).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:51:12 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl<'a, T> Store for &'a T
|
|
|
|
where
|
|
|
|
T: Store,
|
|
|
|
{
|
|
|
|
type Identifier = T::Identifier;
|
|
|
|
type Stream = T::Stream;
|
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, Error>
|
2022-04-01 21:51:12 +00:00
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
Reader: AsyncRead + Unpin + 'static,
|
2022-04-01 21:51:12 +00:00
|
|
|
{
|
|
|
|
T::save_async_read(self, reader).await
|
|
|
|
}
|
|
|
|
|
2022-09-24 22:18:53 +00:00
|
|
|
async fn save_stream<S>(&self, stream: S) -> Result<Self::Identifier, Error>
|
|
|
|
where
|
|
|
|
S: Stream<Item = std::io::Result<Bytes>> + Unpin + 'static,
|
|
|
|
{
|
|
|
|
T::save_stream(self, stream).await
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:51:12 +00:00
|
|
|
async fn save_bytes(&self, bytes: Bytes) -> Result<Self::Identifier, Error> {
|
|
|
|
T::save_bytes(self, bytes).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn to_stream(
|
|
|
|
&self,
|
|
|
|
identifier: &Self::Identifier,
|
|
|
|
from_start: Option<u64>,
|
|
|
|
len: Option<u64>,
|
|
|
|
) -> Result<Self::Stream, Error> {
|
|
|
|
T::to_stream(self, identifier, from_start, len).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_into<Writer>(
|
|
|
|
&self,
|
|
|
|
identifier: &Self::Identifier,
|
|
|
|
writer: &mut Writer,
|
|
|
|
) -> Result<(), std::io::Error>
|
|
|
|
where
|
2022-09-24 22:18:53 +00:00
|
|
|
Writer: AsyncWrite + Unpin,
|
2022-04-01 21:51:12 +00:00
|
|
|
{
|
|
|
|
T::read_into(self, identifier, writer).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn len(&self, identifier: &Self::Identifier) -> Result<u64, Error> {
|
|
|
|
T::len(self, identifier).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn remove(&self, identifier: &Self::Identifier) -> Result<(), Error> {
|
|
|
|
T::remove(self, identifier).await
|
|
|
|
}
|
|
|
|
}
|