use crate::ooxml::error::{OoxmlError, Result};
use crate::ooxml::pptx::shapes::base::BaseShape;
use quick_xml::events::Event;
use quick_xml::Reader;
#[derive(Debug, Clone)]
pub struct Picture {
base: BaseShape,
}
impl Picture {
pub fn new(xml_bytes: Vec<u8>) -> Self {
Self {
base: BaseShape::new(xml_bytes, crate::ooxml::pptx::shapes::base::ShapeType::Picture),
}
}
#[inline]
pub fn base(&mut self) -> &mut BaseShape {
&mut self.base
}
pub fn image_r_id(&self) -> Result<String> {
let mut reader = Reader::from_reader(self.base.xml_bytes());
reader.config_mut().trim_text(true);
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Empty(e)) | Ok(Event::Start(e)) => {
if e.local_name().as_ref() == b"blip" {
for attr in e.attributes().flatten() {
let key = attr.key.as_ref();
if key == b"r:embed" ||
(key.starts_with(b"r:") && attr.key.local_name().as_ref() == b"embed") ||
attr.key.local_name().as_ref() == b"embed" {
let rid = std::str::from_utf8(&attr.value)
.map_err(|e| OoxmlError::Xml(e.to_string()))?;
return Ok(rid.to_string());
}
}
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(OoxmlError::Xml(e.to_string())),
_ => {}
}
buf.clear();
}
Err(OoxmlError::PartNotFound("Image relationship not found".to_string()))
}
pub fn image_filename(&self) -> Option<String> {
None
}
}