feat: implement media metadata management with EXIF and TrackInfo support
This commit is contained in:
@@ -10,6 +10,14 @@ pub enum PostgresRole {
|
||||
Admin,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
|
||||
#[sqlx(rename_all = "lowercase")]
|
||||
#[sqlx(type_name = "TEXT")]
|
||||
pub enum PostgresMediaMetadataSource {
|
||||
Exif,
|
||||
TrackInfo,
|
||||
}
|
||||
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct PostgresUser {
|
||||
@@ -51,6 +59,15 @@ pub struct PostgresMedia {
|
||||
pub thumbnail_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct PostgresMediaMetadata {
|
||||
pub id: uuid::Uuid,
|
||||
pub media_id: uuid::Uuid,
|
||||
pub source: String,
|
||||
pub tag_name: String,
|
||||
pub tag_value: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, sqlx::Type, PartialEq, Eq, Deserialize)]
|
||||
#[sqlx(rename_all = "lowercase")]
|
||||
#[sqlx(type_name = "album_permission")]
|
||||
|
||||
@@ -87,3 +87,19 @@ pub async fn build_album_share_repository(
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn build_media_metadata_repository(
|
||||
_db_config: &DatabaseConfig,
|
||||
pool: DatabasePool,
|
||||
) -> CoreResult<Arc<dyn libertas_core::repositories::MediaMetadataRepository>> {
|
||||
match pool {
|
||||
DatabasePool::Postgres(pg_pool) => Ok(Arc::new(
|
||||
crate::repositories::media_metadata_repository::PostgresMediaMetadataRepository::new(
|
||||
pg_pool,
|
||||
),
|
||||
)),
|
||||
DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database(
|
||||
"Sqlite media metadata repository not implemented".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use libertas_core::models::{Album, AlbumPermission, AlbumShare, Media, Role, User};
|
||||
use libertas_core::models::{Album, AlbumPermission, AlbumShare, Media, MediaMetadata, MediaMetadataSource, Role, User};
|
||||
|
||||
use crate::db_models::{PostgresAlbum, PostgresAlbumPermission, PostgresAlbumShare, PostgresMedia, PostgresRole, PostgresUser};
|
||||
use crate::db_models::{PostgresAlbum, PostgresAlbumPermission, PostgresAlbumShare, PostgresMedia, PostgresMediaMetadata, PostgresMediaMetadataSource, PostgresRole, PostgresUser};
|
||||
|
||||
impl From<PostgresRole> for Role {
|
||||
fn from(pg_role: PostgresRole) -> Self {
|
||||
@@ -20,6 +20,24 @@ impl From<Role> for PostgresRole {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PostgresMediaMetadataSource> for MediaMetadataSource {
|
||||
fn from(pg_source: PostgresMediaMetadataSource) -> Self {
|
||||
match pg_source {
|
||||
PostgresMediaMetadataSource::Exif => MediaMetadataSource::Exif,
|
||||
PostgresMediaMetadataSource::TrackInfo => MediaMetadataSource::TrackInfo,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MediaMetadataSource> for PostgresMediaMetadataSource {
|
||||
fn from(source: MediaMetadataSource) -> Self {
|
||||
match source {
|
||||
MediaMetadataSource::Exif => PostgresMediaMetadataSource::Exif,
|
||||
MediaMetadataSource::TrackInfo => PostgresMediaMetadataSource::TrackInfo,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PostgresUser> for User {
|
||||
fn from(pg_user: PostgresUser) -> Self {
|
||||
User {
|
||||
@@ -60,15 +78,23 @@ impl From<PostgresMedia> for Media {
|
||||
mime_type: pg_media.mime_type,
|
||||
hash: pg_media.hash,
|
||||
created_at: pg_media.created_at,
|
||||
extracted_location: pg_media.extracted_location,
|
||||
width: pg_media.width,
|
||||
height: pg_media.height,
|
||||
date_taken: pg_media.date_taken,
|
||||
thumbnail_path: pg_media.thumbnail_path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PostgresMediaMetadata> for MediaMetadata {
|
||||
fn from(pg_metadata: PostgresMediaMetadata) -> Self {
|
||||
MediaMetadata {
|
||||
id: pg_metadata.id,
|
||||
media_id: pg_metadata.media_id,
|
||||
source: MediaMetadataSource::from(pg_metadata.source.as_str()),
|
||||
tag_name: pg_metadata.tag_name,
|
||||
tag_value: pg_metadata.tag_value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PostgresAlbumPermission> for AlbumPermission {
|
||||
fn from(pg_permission: PostgresAlbumPermission) -> Self {
|
||||
match pg_permission {
|
||||
|
||||
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