feat: async image conversion service (avif/webp) with backfill
This commit is contained in:
48
crates/adapters/postgres/src/image_ref.rs
Normal file
48
crates/adapters/postgres/src/image_ref.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use async_trait::async_trait;
|
||||
use domain::{errors::DomainError, ports::ImageRefPort};
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct PostgresImageRefAdapter {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresImageRefAdapter {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_image_ref(pool: PgPool) -> Arc<dyn ImageRefPort> {
|
||||
Arc::new(PostgresImageRefAdapter::new(pool))
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ImageRefPort for PostgresImageRefAdapter {
|
||||
async fn swap(&self, old_key: &str, new_key: &str) -> Result<(), DomainError> {
|
||||
let mut tx = self.pool.begin().await
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
sqlx::query("UPDATE users SET avatar_path = $1 WHERE avatar_path = $2")
|
||||
.bind(new_key).bind(old_key)
|
||||
.execute(&mut *tx).await
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
sqlx::query("UPDATE movies SET poster_path = $1 WHERE poster_path = $2")
|
||||
.bind(new_key).bind(old_key)
|
||||
.execute(&mut *tx).await
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
tx.commit().await
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))
|
||||
}
|
||||
|
||||
async fn list_keys(&self) -> Result<Vec<String>, DomainError> {
|
||||
let rows: Vec<(String,)> = sqlx::query_as(
|
||||
"SELECT avatar_path FROM users WHERE avatar_path IS NOT NULL
|
||||
UNION
|
||||
SELECT poster_path FROM movies WHERE poster_path IS NOT NULL",
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::InfrastructureError(e.to_string()))?;
|
||||
Ok(rows.into_iter().map(|(k,)| k).collect())
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use domain::{
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
|
||||
mod image_ref;
|
||||
mod import_profile;
|
||||
mod import_session;
|
||||
mod models;
|
||||
@@ -23,6 +24,7 @@ use models::{
|
||||
UserTotalsRow, datetime_to_str,
|
||||
};
|
||||
|
||||
pub use image_ref::{PostgresImageRefAdapter, create_image_ref};
|
||||
pub use import_profile::PostgresImportProfileRepository;
|
||||
pub use import_session::PostgresImportSessionRepository;
|
||||
pub use profile::PostgresMovieProfileRepository;
|
||||
|
||||
Reference in New Issue
Block a user