Skip to content

Commit 4c6135c

Browse files
committed
🤷‍♂️
1 parent 5359a71 commit 4c6135c

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ readme = "README.md"
1212
image = "0.25.5"
1313
strum_macros = "0.27.1"
1414
tiff = "0.9.1"
15+
xml-rs = "0.8.25"

src/tiff.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
1-
use crate::ReadUtils;
2-
use crate::reader::read_iptc_data;
3-
use crate::tags::IPTCTag;
1+
use crate::{ReadUtils, tags};
42
use std::collections::HashMap;
53
use std::error::Error;
64
use std::io::Cursor;
7-
use tiff::decoder::Decoder;
5+
use tags::IPTCTag;
6+
use tiff::decoder::{Decoder, DecodingResult};
7+
use xml::reader::{EventReader, XmlEvent};
88

99
pub(crate) struct TIFFReader;
1010

1111
impl TIFFReader {
1212
pub fn read_iptc(buffer: &Vec<u8>) -> Result<HashMap<IPTCTag, String>, Box<dyn Error>> {
1313
let cursor = Cursor::new(buffer);
14-
let decoder = Decoder::new(cursor);
14+
let mut decoder = Decoder::new(cursor)?;
15+
16+
while let Ok(Some(field)) = decoder.next_field() {
17+
if field.tag == 700 {
18+
if let DecodingResult::U8(data) = field.data {
19+
return read_xmp_data(&data);
20+
}
21+
}
22+
}
1523

1624
Ok(HashMap::new())
1725
}
1826
}
27+
28+
fn read_xmp_data(data: &[u8]) -> Result<HashMap<IPTCTag, String>, Box<dyn Error>> {
29+
let parser = EventReader::new(Cursor::new(data));
30+
let mut iptc_data = HashMap::new();
31+
let mut current_tag: Option<IPTCTag> = None;
32+
33+
for event in parser {
34+
match event? {
35+
XmlEvent::StartElement { name, .. } => {
36+
current_tag = match name.local_name.as_str() {
37+
"creator" => Some(IPTCTag::ByLine),
38+
"title" => Some(IPTCTag::ByLineTitle),
39+
_ => None,
40+
};
41+
}
42+
XmlEvent::Characters(data) => {
43+
if let Some(tag) = &current_tag {
44+
iptc_data.insert(tag.clone(), data);
45+
}
46+
}
47+
XmlEvent::EndElement { .. } => {
48+
current_tag = None;
49+
}
50+
_ => {}
51+
}
52+
}
53+
54+
Ok(iptc_data)
55+
}

0 commit comments

Comments
 (0)