pub struct DocxAssetProvider { /* private fields */ }Expand description
Provides streaming access to binary assets stored inside a DOCX ZIP archive.
Use this alongside DocxReader when you need to resolve embedded images.
DocxReader emits Event::Image with an asset_id of the form
zip://word/media/image1.png; pass that asset_id to
DocxAssetProvider to stream the raw bytes.
§Example
use docspec_docx_reader::DocxAssetProvider;
use docspec_core::AssetProvider;
let provider = DocxAssetProvider::from_path("document.docx")?;
let mut buf = Vec::new();
if let Some(result) = provider.stream_to("zip://word/media/image1.png", &mut buf) {
result?;
}Provides streaming access to binary assets stored inside a DOCX ZIP archive.
Holds the docx ZIP file open until dropped. Uses internal Mutex to serialize
concurrent ZIP reads. Not Clone — use Arc<DocxAssetProvider> to share.
Implementations§
Source§impl DocxAssetProvider
impl DocxAssetProvider
Sourcepub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>
Creates a DocxAssetProvider from a file path.
Opens the DOCX ZIP file and reads [Content_Types].xml to build the
content type lookup table.
§Errors
Returns Error::Io if the file cannot be opened, or Error::Parse
if the file is not a valid ZIP archive.
Sourcepub fn from_reader<R: Read + Seek + Send + 'static>(reader: R) -> Result<Self>
pub fn from_reader<R: Read + Seek + Send + 'static>(reader: R) -> Result<Self>
Creates a DocxAssetProvider from any Read + Seek + Send source.
The source must be positioned at the start of a valid DOCX (ZIP) archive.
Reads [Content_Types].xml to build the content type lookup table.
§Errors
Returns Error::Parse if the input is not a valid ZIP archive, or
Error::Io for I/O failures when reading [Content_Types].xml.
Trait Implementations§
Source§impl AssetProvider for DocxAssetProvider
impl AssetProvider for DocxAssetProvider
Source§fn content_type(&self, asset_id: &str) -> Option<Cow<'_, str>>
fn content_type(&self, asset_id: &str) -> Option<Cow<'_, str>>
Returns the MIME content type for an asset ID with a zip:// scheme prefix.
Strips the zip:// prefix and looks up the path in [Content_Types].xml.
Returns None if the scheme is not zip:// or if no content type is registered
for the path.
Source§fn stream_to(
&self,
asset_id: &str,
writer: &mut dyn Write,
) -> Option<Result<u64>>
fn stream_to( &self, asset_id: &str, writer: &mut dyn Write, ) -> Option<Result<u64>>
Streams the asset bytes at asset_id to writer.
Strips the zip:// prefix, acquires the archive mutex, locates the ZIP entry,
and copies bytes via io::copy — never buffers the full asset. Returns:
Noneifasset_iddoes not start withzip://Noneif the mutex is poisonedNoneif the entry is not found in the archiveSome(Ok(n))on success withnbytes writtenSome(Err(_))on I/O error during copy