feat: implement r2 image upload for avatars

This commit is contained in:
Ryan Walters
2025-09-18 13:18:14 -05:00
parent 56e02e7253
commit 7f9d3e9158
8 changed files with 1932 additions and 114 deletions

View File

@@ -4,7 +4,7 @@ use std::sync::Arc;
use tokio::sync::RwLock;
use crate::data::pool::PgPool;
use crate::{auth::AuthRegistry, config::Config};
use crate::{auth::AuthRegistry, config::Config, image::ImageStorage};
#[derive(Debug, Clone)]
pub struct Health {
@@ -35,27 +35,37 @@ impl Health {
#[derive(Clone)]
pub struct AppState {
pub config: Arc<Config>,
pub auth: Arc<AuthRegistry>,
pub sessions: Arc<DashMap<String, crate::auth::provider::AuthUser>>,
pub jwt_encoding_key: Arc<EncodingKey>,
pub jwt_decoding_key: Arc<DecodingKey>,
pub db: Arc<PgPool>,
pub health: Arc<RwLock<Health>>,
pub image_storage: Arc<ImageStorage>,
}
impl AppState {
pub fn new(config: Config, auth: AuthRegistry, db: PgPool) -> Self {
let jwt_secret = config.jwt_secret.clone();
// Initialize image storage
let image_storage = match ImageStorage::from_config(&config) {
Ok(storage) => Arc::new(storage),
Err(e) => {
tracing::warn!(error = %e, "Failed to initialize image storage, avatar processing will be disabled");
// Create a dummy storage that will fail gracefully
Arc::new(ImageStorage::new(&config, "dummy").unwrap_or_else(|_| panic!("Failed to create dummy image storage")))
}
};
Self {
config: Arc::new(config),
auth: Arc::new(auth),
sessions: Arc::new(DashMap::new()),
jwt_encoding_key: Arc::new(EncodingKey::from_secret(jwt_secret.as_bytes())),
jwt_decoding_key: Arc::new(DecodingKey::from_secret(jwt_secret.as_bytes())),
db: Arc::new(db),
health: Arc::new(RwLock::new(Health::new())),
image_storage,
}
}
}