about summary refs log tree commit diff
path: root/kittybox-rs/src/media/storage/mod.rs
blob: e9b01f9cae45f73a568e8207acc074665b33bbd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use async_trait::async_trait;

pub mod file;

#[derive(Debug, Clone, Copy)]
pub enum ErrorKind {
    Backend,
    Permission,
    Conflict,
    Other,
}

#[derive(Debug)]
pub struct MediaStoreError {
    kind: ErrorKind,
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
    msg: String,
}

impl std::error::Error for MediaStoreError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_ref()
            .map(|i| i.as_ref() as &dyn std::error::Error)
    }
}

impl std::fmt::Display for MediaStoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}: {}",
            match self.kind {
                ErrorKind::Backend => "media storage backend error",
                ErrorKind::Permission => "permission denied",
                ErrorKind::Conflict => "conflict with existing data",
                ErrorKind::Other => "unknown media storage error",
            },
            self.msg
        )
    }
}

pub type Result<T> = std::result::Result<T, MediaStoreError>;

#[async_trait]
pub trait MediaStore: 'static + Send + Sync + Clone {
    async fn write_streaming(&self, domain: url::Host, filename: &str, content: axum::extract::multipart::Field<'_>) -> Result<()>;
    async fn write(&self, domain: url::Host, filename: &str, content: &[u8]) -> Result<()>;
    async fn read_streaming(&self, domain: url::Host, filename: &str) -> Result<tokio_util::io::ReaderStream<Box<dyn tokio::io::AsyncRead>>>;
    async fn read(&self, domain: url::Host, filename: &str) -> Result<Vec<u8>>;
    async fn delete(&self, domain: url::Host, filename: &str) -> Result<()>;
}