forked from jmug/stoatchat
feat: repository architecture for files crate w. added tests (#498)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
use anyhow::Result;
|
||||
|
||||
pub trait EncryptionRepository: Send + Sync + 'static {
|
||||
fn decrypt_buffer(&self, buf: Vec<u8>, iv: &str) -> anyhow::Result<Vec<u8>>;
|
||||
fn encrypt_buffer(&self, buf: &[u8]) -> Result<(Vec<u8>, String)>;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use anyhow::Result;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait FileStorageRepository: Send + Sync + 'static {
|
||||
async fn create_bucket(&self, bucket_id: &str) -> anyhow::Result<()>;
|
||||
|
||||
async fn fetch_and_decrypt_file(
|
||||
&self,
|
||||
bucket_id: &str,
|
||||
path: &str,
|
||||
iv: &str,
|
||||
) -> Result<Vec<u8>>;
|
||||
|
||||
async fn encrypt_and_upload_file(
|
||||
&self,
|
||||
bucket_id: &str,
|
||||
path: &str,
|
||||
buf: &[u8],
|
||||
) -> Result<String>;
|
||||
|
||||
async fn delete_file(&self, bucket_id: &str, path: &str) -> Result<()>;
|
||||
}
|
||||
30
crates/core/files/src/repositories/media_repository.rs
Normal file
30
crates/core/files/src/repositories/media_repository.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use anyhow::Result;
|
||||
use image::DynamicImage;
|
||||
use std::io::{BufRead, Read, Seek};
|
||||
use tempfile::NamedTempFile;
|
||||
use thiserror::Error;
|
||||
|
||||
pub trait MediaRepository: Send + Sync + 'static {
|
||||
fn image_size(&self, f: &NamedTempFile) -> Option<(usize, usize)>;
|
||||
fn image_size_vec(&self, v: &[u8], mime: &str) -> Option<(usize, usize)>;
|
||||
|
||||
fn decode_image<R: Read + BufRead + Seek>(
|
||||
&self,
|
||||
reader: &mut R,
|
||||
mime: &str,
|
||||
) -> Result<DynamicImage, MediaError>;
|
||||
|
||||
fn is_valid_image<R: Read + BufRead + Seek>(&self, reader: &mut R, mime: &str) -> bool;
|
||||
|
||||
fn create_thumbnail(&self, image: DynamicImage, tag: &str) -> Vec<u8>;
|
||||
|
||||
fn video_size(&self, f: &NamedTempFile) -> Option<(i64, i64)>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum MediaError {
|
||||
#[error("image processing failed because {cause}")]
|
||||
ImageProcessingFailed { cause: String },
|
||||
#[error(transparent)]
|
||||
Unknown(#[from] anyhow::Error),
|
||||
}
|
||||
7
crates/core/files/src/repositories/mod.rs
Normal file
7
crates/core/files/src/repositories/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod encryption_repository;
|
||||
mod file_storage_repository;
|
||||
mod media_repository;
|
||||
|
||||
pub use encryption_repository::EncryptionRepository;
|
||||
pub use file_storage_repository::FileStorageRepository;
|
||||
pub use media_repository::{MediaError, MediaRepository};
|
||||
Reference in New Issue
Block a user