-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
- Loading branch information
Showing
3 changed files
with
68 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,67 @@ | ||
use chrono::NaiveDate; | ||
use exif::{DateTime, Error, In, Tag, Value}; | ||
use exif::{DateTime, In, Tag, Value}; | ||
use std::fs::File; | ||
use std::path::Path; | ||
|
||
pub fn captures(path: &Path) -> Result<Option<chrono::DateTime<chrono::Utc>>, String> { | ||
let file = match File::open(path) { | ||
Ok(f) => f, | ||
Err(e) => return Err(e.to_string()), | ||
}; | ||
pub fn captures(path: &Path) -> Result<Option<chrono::DateTime<chrono::Utc>>, exif::Error> { | ||
let file = File::open(path)?; | ||
|
||
let mut bufreader = std::io::BufReader::new(&file); | ||
let exifreader = exif::Reader::new(); | ||
match exifreader.read_from_container(&mut bufreader) { | ||
Ok(exif) => { | ||
//for f in exif.fields() { | ||
// println!( | ||
// " {}/{}: {}", | ||
// f.ifd_num.index(), | ||
// f.tag, | ||
// f.display_value().with_unit(&exif) | ||
// ); | ||
// println!(" {:?}", f.value); | ||
//} | ||
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) { | ||
Some(field) => match field.value { | ||
Value::Ascii(ref vec) if !vec.is_empty() => { | ||
match DateTime::from_ascii(&vec[0]) { | ||
Ok(datetime) => Ok(Some( | ||
NaiveDate::from_ymd_opt( | ||
i32::from(datetime.year), | ||
u32::from(datetime.month), | ||
u32::from(datetime.day), | ||
) | ||
.unwrap() | ||
.and_hms_milli_opt( | ||
u32::from(datetime.hour), | ||
u32::from(datetime.minute), | ||
u32::from(datetime.second), | ||
0, | ||
) | ||
.unwrap() | ||
.and_utc(), | ||
)), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
} | ||
_ => Ok(None), | ||
}, | ||
None => Ok(None), | ||
} | ||
let result = exif::Reader::new() | ||
.continue_on_error(true) | ||
.read_from_container(&mut bufreader); | ||
|
||
let exif = if let Err(exif::Error::PartialResult(partial)) = result { | ||
let (exif, _) = partial.into_inner(); | ||
exif | ||
} else { | ||
result? | ||
}; | ||
|
||
//if let Some(field) = exif.get_field(Tag::DateTime, In::PRIMARY) { | ||
// match field.value { | ||
// Value::Ascii(ref vec) if !vec.is_empty() => { | ||
// if let Ok(datetime) = DateTime::from_ascii(&vec[0]) { | ||
// println!("Year of DateTime is {}.", datetime.year); | ||
// } | ||
// } | ||
// _ => {} | ||
// } | ||
//} | ||
} | ||
Err(e) => match e { | ||
Error::NotFound(_) => Ok(None), | ||
_ => Err(e.to_string()), | ||
//for f in exif.fields() { | ||
// println!( | ||
// " {}/{}: {}", | ||
// f.ifd_num.index(), | ||
// f.tag, | ||
// f.display_value().with_unit(&exif) | ||
// ); | ||
// println!(" {:?}", f.value); | ||
//} | ||
|
||
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) { | ||
Some(field) => match field.value { | ||
Value::Ascii(ref vec) if !vec.is_empty() => { | ||
let datetime = DateTime::from_ascii(&vec[0])?; | ||
Ok(Some( | ||
NaiveDate::from_ymd_opt( | ||
i32::from(datetime.year), | ||
u32::from(datetime.month), | ||
u32::from(datetime.day), | ||
) | ||
.unwrap() | ||
.and_hms_milli_opt( | ||
u32::from(datetime.hour), | ||
u32::from(datetime.minute), | ||
u32::from(datetime.second), | ||
0, | ||
) | ||
.unwrap() | ||
.and_utc(), | ||
)) | ||
} | ||
_ => Ok(None), | ||
}, | ||
None => Ok(None), | ||
} | ||
|
||
//if let Some(field) = exif.get_field(Tag::DateTime, In::PRIMARY) { | ||
// match field.value { | ||
// Value::Ascii(ref vec) if !vec.is_empty() => { | ||
// if let Ok(datetime) = DateTime::from_ascii(&vec[0]) { | ||
// println!("Year of DateTime is {}.", datetime.year); | ||
// } | ||
// } | ||
// _ => {} | ||
// } | ||
//} | ||
} |