feat: enhance media management with EXIF data extraction, metadata filtering, and storage path generation
refactor: update configuration handling to use environment variables and improve code organization
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use libertas_core::{
|
||||
config::{Config, DatabaseConfig, DatabaseType},
|
||||
config::{AppConfig, DatabaseConfig, DatabaseType},
|
||||
error::{CoreError, CoreResult},
|
||||
repositories::UserRepository,
|
||||
};
|
||||
@@ -47,7 +47,7 @@ pub async fn build_user_repository(
|
||||
}
|
||||
|
||||
pub async fn build_media_repository(
|
||||
config: &Config,
|
||||
config: &AppConfig,
|
||||
pool: DatabasePool,
|
||||
) -> CoreResult<Arc<dyn libertas_core::repositories::MediaRepository>> {
|
||||
match pool {
|
||||
|
||||
@@ -38,12 +38,41 @@ impl QueryBuilder<ListMediaOptions> for MediaQueryBuilder {
|
||||
mut query: SqlxQueryBuilder<'a, sqlx::Postgres>,
|
||||
options: &'a ListMediaOptions,
|
||||
) -> CoreResult<SqlxQueryBuilder<'a, sqlx::Postgres>> {
|
||||
if let Some(_filter) = &options.filter {
|
||||
// In the future, you would add logic here:
|
||||
// if let Some(mime) = &filter.mime_type {
|
||||
// query.push(" AND mime_type = ");
|
||||
// query.push_bind(mime);
|
||||
// }
|
||||
let mut metadata_filter_count = 0;
|
||||
|
||||
if let Some(filter) = &options.filter {
|
||||
if let Some(mime) = &filter.mime_type {
|
||||
query.push(" AND media.mime_type = ");
|
||||
query.push_bind(mime);
|
||||
}
|
||||
|
||||
if let Some(metadata_filters) = &filter.metadata_filters {
|
||||
if !metadata_filters.is_empty() {
|
||||
metadata_filter_count = metadata_filters.len();
|
||||
|
||||
query.push(" JOIN media_metadata mm ON media.id == mm.media_id ");
|
||||
query.push(" AND ( ");
|
||||
|
||||
for (i, filter) in metadata_filters.iter().enumerate() {
|
||||
if i > 0 {
|
||||
query.push(" OR ");
|
||||
}
|
||||
|
||||
query.push(" ( mm.tag_name = ");
|
||||
query.push_bind(&filter.tag_name);
|
||||
query.push(" AND mm.tag_value = ");
|
||||
query.push_bind(&filter.tag_value);
|
||||
query.push(" ) ");
|
||||
}
|
||||
query.push(" ) ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if metadata_filter_count > 0 {
|
||||
query.push(" GROUP BY media.id ");
|
||||
query.push(" HAVING COUNT(DISTINCT mm.tag_name) = ");
|
||||
query.push_bind(metadata_filter_count as i64);
|
||||
}
|
||||
|
||||
if let Some(sort) = &options.sort {
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{
|
||||
config::Config, error::{CoreError, CoreResult}, models::Media, repositories::MediaRepository, schema::ListMediaOptions
|
||||
config::AppConfig, error::{CoreError, CoreResult}, models::Media, repositories::MediaRepository, schema::ListMediaOptions
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
@@ -16,11 +16,11 @@ pub struct PostgresMediaRepository {
|
||||
}
|
||||
|
||||
impl PostgresMediaRepository {
|
||||
pub fn new(pool: PgPool, config: &Config) -> Self {
|
||||
pub fn new(pool: PgPool, config: &AppConfig) -> Self {
|
||||
let allowed_columns = config
|
||||
.allowed_sort_columns
|
||||
.clone()
|
||||
.unwrap_or_else(|| vec!["created_at".to_string()]);
|
||||
.unwrap_or_else(|| vec!["created_at".to_string(), "original_filename".to_string()]);
|
||||
|
||||
Self { pool, query_builder: Arc::new(MediaQueryBuilder::new(allowed_columns)) }
|
||||
}
|
||||
@@ -89,10 +89,10 @@ impl MediaRepository for PostgresMediaRepository {
|
||||
async fn list_by_user(&self, user_id: Uuid, options: &ListMediaOptions) -> CoreResult<Vec<Media>> {
|
||||
let mut query = sqlx::QueryBuilder::new(
|
||||
r#"
|
||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||
thumbnail_path
|
||||
SELECT media.id, media.owner_id, media.storage_path, media.original_filename, media.mime_type, media.hash, media.created_at,
|
||||
media.thumbnail_path
|
||||
FROM media
|
||||
WHERE owner_id =
|
||||
WHERE media.owner_id =
|
||||
"#,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user