feat: integrate EXIF data extraction using nom-exif and refactor related components
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
use std::{path::{Path, PathBuf}, sync::Arc};
|
||||
use anyhow::Result;
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use chrono::{DateTime, Datelike, NaiveDateTime, Utc};
|
||||
use chrono::Datelike;
|
||||
use clap::Parser;
|
||||
use libertas_core::{config::Config, error::{CoreError, CoreResult}, models::{Media, User}, repositories::{MediaRepository, UserRepository}};
|
||||
use libertas_core::{
|
||||
config::Config, error::{CoreError, CoreResult}, media_utils::extract_exif_data, models::{Media, User}, repositories::{MediaRepository, UserRepository}
|
||||
};
|
||||
use libertas_infra::factory::{build_database_pool, build_media_repository, build_user_repository};
|
||||
use nom_exif::{AsyncMediaParser, AsyncMediaSource, Exif, ExifIter, ExifTag};
|
||||
use nom_exif::{AsyncMediaParser, AsyncMediaSource, ExifIter};
|
||||
use serde_json;
|
||||
use sha2::{Digest, Sha256};
|
||||
use tokio::fs;
|
||||
use uuid::Uuid;
|
||||
use walkdir::WalkDir;
|
||||
use tokio::fs;
|
||||
|
||||
mod config;
|
||||
|
||||
@@ -34,7 +40,10 @@ struct ImporterState {
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
println!("Starting import for user: '{}' from path '{}'...", cli.username, cli.path);
|
||||
println!(
|
||||
"Starting import for user: '{}' from path '{}'...",
|
||||
cli.username, cli.path
|
||||
);
|
||||
|
||||
let config = config::load_config()?;
|
||||
let db_pool = build_database_pool(&config.database).await?;
|
||||
@@ -52,10 +61,10 @@ async fn main() -> Result<()> {
|
||||
};
|
||||
|
||||
let user = state
|
||||
.user_repo
|
||||
.find_by_username(&cli.username)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("User '{}' not found", cli.username))?;
|
||||
.user_repo
|
||||
.find_by_username(&cli.username)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("User '{}' not found", cli.username))?;
|
||||
|
||||
println!("User '{}' found with ID: {}", cli.username, user.id);
|
||||
println!("Storage: {} / {}", user.storage_used, user.storage_quota);
|
||||
@@ -66,11 +75,11 @@ async fn main() -> Result<()> {
|
||||
for entry in walker.filter_map(Result::ok) {
|
||||
if entry.file_type().is_file() {
|
||||
let path = entry.path();
|
||||
|
||||
|
||||
match process_file(path, &user, &state).await {
|
||||
Ok(media) => {
|
||||
println!("-> Imported: '{}'", media.original_filename);
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("!! Skipped: '{}' (Reason: {})", path.display(), e);
|
||||
}
|
||||
@@ -83,11 +92,7 @@ async fn main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_file(
|
||||
file_path: &Path,
|
||||
user: &User,
|
||||
state: &ImporterState,
|
||||
) -> CoreResult<Media> {
|
||||
async fn process_file(file_path: &Path, user: &User, state: &ImporterState) -> CoreResult<Media> {
|
||||
let file_bytes = fs::read(file_path).await?;
|
||||
let file_size = file_bytes.len() as i64;
|
||||
let hash = format!("{:x}", Sha256::digest(&file_bytes));
|
||||
@@ -107,34 +112,58 @@ async fn process_file(
|
||||
));
|
||||
}
|
||||
|
||||
let (width, height, location, date_taken) =
|
||||
match AsyncMediaSource::file_path(file_path).await {
|
||||
Ok(ms) => {
|
||||
if ms.has_exif() {
|
||||
let mut parser = AsyncMediaParser::new();
|
||||
if let Ok(iter) = parser.parse::<_,_, ExifIter>(ms).await {
|
||||
let gps = iter.parse_gps_info().ok().flatten().map(|g| g.format_iso6709());
|
||||
println!(" -> EXIF GPS Info: {:?}", gps);
|
||||
let exif: Exif = iter.into();
|
||||
let modified_date = exif.get(ExifTag::ModifyDate).and_then(|f| f.as_str()).and_then(parse_exif_datetime);
|
||||
println!(" -> EXIF ModifyDate: {:?}", modified_date);
|
||||
let w = exif.get(ExifTag::ExifImageWidth).and_then(|f| f.as_u32()).map(|v| v as i32);
|
||||
println!(" -> EXIF ExifImageWidth: {:?}", w);
|
||||
let h = exif.get(ExifTag::ExifImageHeight).and_then(|f| f.as_u32()).map(|v| v as i32);
|
||||
println!(" -> EXIF ExifImageHeight: {:?}", h);
|
||||
let dt = exif.get(ExifTag::DateTimeOriginal).and_then(|f| f.as_str()).and_then(parse_exif_datetime);
|
||||
println!(" -> EXIF DateTimeOriginal: {:?}", dt);
|
||||
(w, h, gps, dt)
|
||||
} else {
|
||||
(None, None, None, None)
|
||||
}
|
||||
let (width, height, location, date_taken) = match extract_exif_data(file_path).await {
|
||||
Ok(data) => {
|
||||
println!(" -> Parsed EXIF: DateTimeOriginal={:?}, GPS={:?}", data.date_taken, data.location);
|
||||
(data.width, data.height, data.location, data.date_taken)
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!(" -> EXIF parsing failed for {}: {}. Skipping.", file_path.display(), e);
|
||||
(None, None, None, None)
|
||||
}
|
||||
};
|
||||
|
||||
match AsyncMediaSource::file_path(file_path).await {
|
||||
Ok(ms) => {
|
||||
if ms.has_exif() {
|
||||
let mut parser = AsyncMediaParser::new();
|
||||
if let Ok(iter) = parser.parse::<_, _, ExifIter>(ms).await {
|
||||
let values = iter
|
||||
.into_iter()
|
||||
.filter_map(|mut x| {
|
||||
let res = x.take_result();
|
||||
match res {
|
||||
Ok(v) => Some((
|
||||
x.tag().map(|x| x.to_string()).unwrap_or_else(|| {
|
||||
format!("Unknown(0x{:04x})", x.tag_code())
|
||||
}),
|
||||
v,
|
||||
)),
|
||||
Err(e) => {
|
||||
println!(
|
||||
" !! EXIF parsing error for tag 0x{:04x}: {}",
|
||||
x.tag_code(),
|
||||
e
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
values.iter().for_each(|x| {
|
||||
println!("{:<32}=> {}", x.0, x.1);
|
||||
});
|
||||
} else {
|
||||
(None, None, None, None)
|
||||
()
|
||||
}
|
||||
} else {
|
||||
()
|
||||
}
|
||||
Err(_) => (None, None, None, None),
|
||||
};
|
||||
|
||||
}
|
||||
Err(_) => (),
|
||||
};
|
||||
|
||||
let file_date = date_taken.unwrap_or_else(|| chrono::Utc::now());
|
||||
let year = file_date.year().to_string();
|
||||
let month = format!("{:02}", file_date.month());
|
||||
@@ -155,8 +184,8 @@ async fn process_file(
|
||||
.to_string();
|
||||
|
||||
let mime_type = mime_guess::from_path(file_path)
|
||||
.first_or_octet_stream()
|
||||
.to_string();
|
||||
.first_or_octet_stream()
|
||||
.to_string();
|
||||
|
||||
let media_model = Media {
|
||||
id: Uuid::new_v4(),
|
||||
@@ -172,23 +201,19 @@ async fn process_file(
|
||||
date_taken: date_taken,
|
||||
thumbnail_path: None,
|
||||
};
|
||||
|
||||
|
||||
state.media_repo.create(&media_model).await?;
|
||||
state.user_repo
|
||||
state
|
||||
.user_repo
|
||||
.update_storage_used(user.id, file_size)
|
||||
.await?;
|
||||
|
||||
let job_payload = serde_json::json!({ "media_id": media_model.id });
|
||||
state.nats_client
|
||||
state
|
||||
.nats_client
|
||||
.publish("media.new".to_string(), job_payload.to_string().into())
|
||||
.await
|
||||
.map_err(|e| CoreError::Unknown(format!("Failed to publish NATS message: {}", e)))?;
|
||||
|
||||
Ok(media_model)
|
||||
}
|
||||
|
||||
fn parse_exif_datetime(s: &str) -> Option<DateTime<Utc>> {
|
||||
NaiveDateTime::parse_from_str(s, "%Y:%m:%d %H:%M:%S")
|
||||
.ok()
|
||||
.map(|ndt| ndt.and_local_timezone(Utc).unwrap())
|
||||
}
|
||||
Reference in New Issue
Block a user