Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Sep 25, 2024
1 parent ea38665 commit 72b27a2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions src/datetime_tag_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use chrono::NaiveDate;
use exif::{DateTime, Error, In, Tag, Value};
use std::fs::File;
use std::path::Path;
Expand All @@ -22,27 +22,26 @@ pub fn captures(path: &Path) -> Result<Option<chrono::DateTime<chrono::Utc>>, St
// );
// println!(" {:?}", f.value);
//}
// To parse a DateTime-like field, `DateTime::from_ascii` can be used.
match exif.get_field(Tag::DateTime, In::PRIMARY) {
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) => {
let d = NaiveDate::from_ymd_opt(
Ok(datetime) => Ok(Some(
NaiveDate::from_ymd_opt(
i32::from(datetime.year),
u32::from(datetime.month),
u32::from(datetime.day),
)
.unwrap();
let t = NaiveTime::from_hms_milli_opt(
.unwrap()
.and_hms_milli_opt(
u32::from(datetime.hour),
u32::from(datetime.minute),
u32::from(datetime.second),
0,
)
.unwrap();
Ok(Some(NaiveDateTime::new(d, t).and_utc()))
}
.unwrap()
.and_utc(),
)),
Err(e) => Err(e.to_string()),
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/datetime_tag_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ pub fn write_tag(path: &Path, date_tag: DateTime<Utc>) -> Result<(), String> {
Err(e) => return Err(e.to_string()),
};

metadata.set_tag(ExifTag::CreateDate(date_tag.to_string()));
metadata.set_tag(ExifTag::DateTimeOriginal(date_tag.to_string()));
metadata.set_tag(ExifTag::ModifyDate(date_tag.to_string()));
metadata.set_tag(ExifTag::DateTimeOriginal(
date_tag.format("%Y:%m:%d %H:%M:%S").to_string(),
));
//metadata.set_tag(ExifTag::ModifyDate(
// date_tag.format("%Y:%m:%d %H:%M:%S").to_string(),
//));
//metadata.set_tag(ExifTag::CreateDate(
// date_tag.format("%Y:%m:%d %H:%M:%S").to_string(),
//));

match metadata.write_to_file(path) {
Ok(_) => Ok(()),
Expand Down
4 changes: 2 additions & 2 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FromIterator<DirEntry> for Stats {
let datetime_tag = match datetime_tag_parser::captures(i.path()) {
Ok(datetime_tag) => datetime_tag,
Err(e) => {
println!("[ERROR] parsing metadata: {e:?}");
println!("[ERROR] {} parsing metadata: {e:?}", i.path().display());
s.num_files_failed_tag_parsing += 1;
s.filenames_tag_unparseable
.push(i.path().display().to_string());
Expand All @@ -65,7 +65,7 @@ impl FromIterator<DirEntry> for Stats {
Some(tag) => match datetime_tag_writer::write_tag(i.path(), tag) {
Ok(_) => s.num_files_successfully_tagged += 1,
Err(e) => {
println!("[ERROR] tagging metadata: {e:?}");
println!("[ERROR] {} tagging metadata: {e:?}", i.path().display());
s.num_files_failed_tagging += 1;
s.filenames_name_untaggeable
.push(i.path().display().to_string());
Expand Down

0 comments on commit 72b27a2

Please sign in to comment.