feat: implement media metadata management with EXIF and TrackInfo support
This commit is contained in:
79
libertas_infra/src/repositories/media_metadata_repository.rs
Normal file
79
libertas_infra/src/repositories/media_metadata_repository.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{error::{CoreError, CoreResult}, models::MediaMetadata, repositories::MediaMetadataRepository};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::db_models::{PostgresMediaMetadata, PostgresMediaMetadataSource};
|
||||
|
||||
pub struct PostgresMediaMetadataRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresMediaMetadataRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MediaMetadataRepository for PostgresMediaMetadataRepository {
|
||||
async fn create_batch(&self, metadata: &[MediaMetadata]) -> CoreResult<()> {
|
||||
if metadata.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut ids = Vec::with_capacity(metadata.len());
|
||||
let mut media_ids = Vec::with_capacity(metadata.len());
|
||||
let mut sources = Vec::with_capacity(metadata.len());
|
||||
let mut tag_names = Vec::with_capacity(metadata.len());
|
||||
let mut tag_values = Vec::with_capacity(metadata.len());
|
||||
|
||||
for item in metadata {
|
||||
ids.push(item.id);
|
||||
media_ids.push(item.media_id);
|
||||
sources.push(item.source.into());
|
||||
tag_names.push(item.tag_name.clone());
|
||||
tag_values.push(item.tag_value.clone());
|
||||
}
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO media_metadata (id, media_id, source, tag_name, tag_value)
|
||||
SELECT * FROM unnest(
|
||||
$1::uuid[],
|
||||
$2::uuid[],
|
||||
$3::text[],
|
||||
$4::text[],
|
||||
$5::text[]
|
||||
)
|
||||
"#,
|
||||
&ids,
|
||||
&media_ids,
|
||||
&sources as &[PostgresMediaMetadataSource],
|
||||
&tag_names,
|
||||
&tag_values,
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn find_by_media_id(&self, media_id: uuid::Uuid) -> CoreResult<Vec<MediaMetadata>> {
|
||||
let pg_metadata = sqlx::query_as!(
|
||||
PostgresMediaMetadata,
|
||||
r#"
|
||||
SELECT id, media_id, source, tag_name, tag_value
|
||||
FROM media_metadata
|
||||
WHERE media_id = $1
|
||||
"#,
|
||||
media_id
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
let metadata = pg_metadata.into_iter().map(|m| m.into()).collect();
|
||||
Ok(metadata)
|
||||
}
|
||||
}
|
||||
@@ -31,8 +31,8 @@ impl MediaRepository for PostgresMediaRepository {
|
||||
async fn create(&self, media: &Media) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO media (id, owner_id, storage_path, original_filename, mime_type, hash, created_at, width, height, thumbnail_path)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
INSERT INTO media (id, owner_id, storage_path, original_filename, mime_type, hash, created_at, thumbnail_path)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
"#,
|
||||
media.id,
|
||||
media.owner_id,
|
||||
@@ -41,8 +41,6 @@ impl MediaRepository for PostgresMediaRepository {
|
||||
media.mime_type,
|
||||
media.hash,
|
||||
media.created_at,
|
||||
media.width,
|
||||
media.height,
|
||||
media.thumbnail_path
|
||||
)
|
||||
.execute(&self.pool)
|
||||
@@ -114,33 +112,6 @@ impl MediaRepository for PostgresMediaRepository {
|
||||
Ok(media_list)
|
||||
}
|
||||
|
||||
async fn update_exif_data(
|
||||
&self,
|
||||
id: Uuid,
|
||||
width: Option<i32>,
|
||||
height: Option<i32>,
|
||||
location: Option<String>,
|
||||
date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
||||
) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
UPDATE media
|
||||
SET width = $2, height = $3, extracted_location = $4, date_taken = $5
|
||||
WHERE id = $1 AND date_taken IS NULL
|
||||
"#,
|
||||
id,
|
||||
width,
|
||||
height,
|
||||
location,
|
||||
date_taken
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_thumbnail_path(&self, id: Uuid, thumbnail_path: String) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
|
||||
@@ -2,3 +2,4 @@ pub mod album_repository;
|
||||
pub mod album_share_repository;
|
||||
pub mod media_repository;
|
||||
pub mod user_repository;
|
||||
pub mod media_metadata_repository;
|
||||
Reference in New Issue
Block a user