feat: add date_taken field to media model and update related functionalities
This commit is contained in:
@@ -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())
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user