Make date parse from metadata a little more consistent
This commit is contained in:
57
src/files.rs
57
src/files.rs
@@ -1,3 +1,6 @@
|
||||
use ::anyhow;
|
||||
use actix::{Handler, Message};
|
||||
use anyhow::{Context, anyhow};
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::Debug;
|
||||
use std::fs::read_dir;
|
||||
@@ -5,10 +8,7 @@ use std::io;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use ::anyhow;
|
||||
use actix::{Handler, Message};
|
||||
use anyhow::{Context, anyhow};
|
||||
use std::time::SystemTime;
|
||||
|
||||
use crate::data::{Claims, FilesRequest, FilterMode, MediaType, PhotosResponse, SortType};
|
||||
use crate::database::ExifDao;
|
||||
@@ -22,6 +22,7 @@ use actix_web::{
|
||||
HttpRequest, HttpResponse,
|
||||
web::{self, Query},
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use opentelemetry::KeyValue;
|
||||
use opentelemetry::trace::{Span, Status, TraceContextExt, Tracer};
|
||||
@@ -73,18 +74,24 @@ fn apply_sorting_with_exif(
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
// Try EXIF date first
|
||||
let date_taken = exif_map.get(&f.file_name).copied().or_else(|| {
|
||||
// Fallback to filename extraction
|
||||
extract_date_from_filename(&f.file_name).map(|dt| dt.timestamp())
|
||||
}).or_else(|| {
|
||||
// Fallback to filesystem metadata creation date
|
||||
let full_path = base_path.join(&f.file_name);
|
||||
std::fs::metadata(full_path)
|
||||
.and_then(|md| md.created())
|
||||
.ok()
|
||||
.and_then(|ct| ct.duration_since(std::time::UNIX_EPOCH).ok())
|
||||
.map(|d| d.as_secs() as i64)
|
||||
});
|
||||
let date_taken = exif_map
|
||||
.get(&f.file_name)
|
||||
.copied()
|
||||
.or_else(|| {
|
||||
// Fallback to filename extraction
|
||||
extract_date_from_filename(&f.file_name).map(|dt| dt.timestamp())
|
||||
})
|
||||
.or_else(|| {
|
||||
// Fallback to filesystem metadata creation date
|
||||
let full_path = base_path.join(&f.file_name);
|
||||
std::fs::metadata(full_path)
|
||||
.and_then(|md| md.created().or(md.modified()))
|
||||
.ok()
|
||||
.map(|system_time| {
|
||||
<SystemTime as Into<DateTime<Utc>>>::into(system_time)
|
||||
.timestamp()
|
||||
})
|
||||
});
|
||||
|
||||
FileWithMetadata {
|
||||
file_name: f.file_name,
|
||||
@@ -332,8 +339,13 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
// Handle sorting - use helper function that supports EXIF date sorting
|
||||
let sort_type = req.sort.unwrap_or(NameAsc);
|
||||
let mut exif_dao_guard = exif_dao.lock().expect("Unable to get ExifDao");
|
||||
let result =
|
||||
apply_sorting_with_exif(files, sort_type, &mut exif_dao_guard, &span_context, (&app_state.base_path).as_ref());
|
||||
let result = apply_sorting_with_exif(
|
||||
files,
|
||||
sort_type,
|
||||
&mut exif_dao_guard,
|
||||
&span_context,
|
||||
(&app_state.base_path).as_ref(),
|
||||
);
|
||||
drop(exif_dao_guard);
|
||||
result
|
||||
})
|
||||
@@ -476,8 +488,13 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
// Handle sorting - use helper function that supports EXIF date sorting
|
||||
let response_files = if let Some(sort_type) = req.sort {
|
||||
let mut exif_dao_guard = exif_dao.lock().expect("Unable to get ExifDao");
|
||||
let result =
|
||||
apply_sorting_with_exif(photos, sort_type, &mut exif_dao_guard, &span_context, (&app_state.base_path).as_ref());
|
||||
let result = apply_sorting_with_exif(
|
||||
photos,
|
||||
sort_type,
|
||||
&mut exif_dao_guard,
|
||||
&span_context,
|
||||
(&app_state.base_path).as_ref(),
|
||||
);
|
||||
drop(exif_dao_guard);
|
||||
result
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user