Refactoring: simplify exif/mtime logic

Refactor the exif parsing/mtime parsing logic to have two clear
branches.
This commit is contained in:
Félix Baylac Jacqué 2023-08-24 18:13:43 +02:00
parent cec77e8766
commit 2cf564f1c4

View file

@ -64,33 +64,31 @@ fn main() {
/// If no datetime EXIF data is attached to the file, use the file /// If no datetime EXIF data is attached to the file, use the file
/// last modification date. /// last modification date.
fn get_picture_datetime(file_path: &str, file: &File) -> DateTime<Utc> { fn get_picture_datetime(file_path: &str, file: &File) -> DateTime<Utc> {
let exif_datetime = get_picture_exif_datetime(file);
exif_datetime.unwrap_or_else(|| get_file_modified_time(file_path, file))
}
/// Retrieves the picture EXIF datetime.
fn get_picture_exif_datetime(file: &File) -> Option<DateTime<Utc>> {
let mut bufreader = std::io::BufReader::new(file); let mut bufreader = std::io::BufReader::new(file);
let exifreader = exif::Reader::new(); let exifreader = exif::Reader::new();
let exif = exifreader.read_from_container(&mut bufreader); let exif = exifreader.read_from_container(&mut bufreader);
match exif { exif.map(
Ok(exif) => { |exif| {
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) { exif.get_field(Tag::DateTimeOriginal, In::PRIMARY).and_then(
Some(datetime_field) => { |datetime_field| {
match datetime_field.value { match datetime_field.value {
Value::Ascii(ref vec) if !vec.is_empty() => { Value::Ascii(ref vec) if !vec.is_empty() => {
// Meh… I know… // Meh… I know…
let str_date = String::from_utf8(vec[0].to_vec()).unwrap(); let str_date = String::from_utf8(vec[0].to_vec()).unwrap();
if let Ok(naive_datetime) = NaiveDateTime::parse_from_str(&str_date, "%Y:%m:%d %H:%M:%S") { NaiveDateTime::parse_from_str(&str_date, "%Y:%m:%d %H:%M:%S")
DateTime::from_utc(naive_datetime, Utc) .map(|naive_datetime| DateTime::from_utc(naive_datetime, Utc))
} else { .ok()
panic!("ERROR: incorrect datetime format for file {}", file_path)
}
} }
_ => _ => None
panic!("ERROR: cannot parse ASCII from datetime EXIF field for file {}", file_path)
} }
}, })
// There's no EXIF datetime field. Let's use the file creation time. }).ok().flatten()
None => get_file_modified_time(file_path, file)
}
},
Err(_e) => get_file_modified_time(file_path, file)
}
} }
/// If we cannot load the EXIF creation datetime, we end up using the /// If we cannot load the EXIF creation datetime, we end up using the