feat: add date_taken field to media model and update related functionalities

This commit is contained in:
2025-11-04 05:28:27 +01:00
parent 39ee8d52a4
commit 828d8e4a2b
11 changed files with 48 additions and 10 deletions

View File

@@ -25,3 +25,4 @@ uuid = { version = "1.18.1", features = ["v4", "serde"] }
nom-exif = { version = "2.5.4", features = ["serde", "tokio", "async"] }
async-trait = "0.1.89"
xmp_toolkit = "1.11.0"
chrono = "0.4.42"

View File

@@ -1,4 +1,5 @@
use async_trait::async_trait;
use chrono::{DateTime, NaiveDateTime, Utc};
use std::path::PathBuf;
use libertas_core::{
@@ -59,15 +60,20 @@ impl MediaProcessorPlugin for ExifReaderPlugin {
.and_then(|f| f.as_u32())
.map(|v| v as i32);
if width.is_some() || height.is_some() || location.is_some() {
let date_taken = exif
.get(ExifTag::DateTimeOriginal)
.and_then(|f| f.as_str())
.and_then(parse_exif_datetime);
if width.is_some() || height.is_some() || location.is_some() || date_taken.is_some() {
context
.media_repo
.update_metadata(media.id, width, height, location.clone())
.update_metadata(media.id, width, height, location.clone(), date_taken)
.await?;
let message = format!(
"Extracted EXIF: width={:?}, height={:?}, location={:?}",
width, height, location
"Extracted EXIF: width={:?}, height={:?}, location={:?}, date_taken={:?}",
width, height, location, date_taken
);
Ok(PluginData { message })
} else {
@@ -77,3 +83,10 @@ impl MediaProcessorPlugin for ExifReaderPlugin {
}
}
}
fn parse_exif_datetime(s: &str) -> Option<DateTime<Utc>> {
// EXIF datetime format is 'YYYY:MM:DD HH:MM:SS'
NaiveDateTime::parse_from_str(s, "%Y:%m:%d %H:%M:%S")
.ok()
.map(|ndt| ndt.and_local_timezone(Utc).unwrap())
}

View File

@@ -7,7 +7,7 @@ use libertas_core::{
plugins::{MediaProcessorPlugin, PluginContext, PluginData},
};
use tokio::fs;
use xmp_toolkit::XmpMeta;
use xmp_toolkit::{XmpMeta, XmpValue};
pub struct XmpWriterPlugin;
@@ -39,6 +39,18 @@ impl MediaProcessorPlugin for XmpWriterPlugin {
CoreError::Unknown(format!("Failed to set description property in XMP: {}", e))
})?;
if let Some(date_taken) = &fresh_media.date_taken {
let date_str = date_taken.to_rfc3339();
xmp.set_property(
"http://ns.adobe.com/exif/1.0/",
"DateTimeOriginal",
&XmpValue::from(date_str),
)
.map_err(|e| {
CoreError::Unknown(format!("Failed to set DateTimeOriginal in XMP: {}", e))
})?;
}
if let Some(_location) = &fresh_media.extracted_location {
// TODO: Set location properties in XMP
}