mod bytes;
mod collection;
mod relationship;
#[cfg(feature = "tree-write")]
mod write;
use std::collections::BTreeMap;
use laminapdf_cos::{Dict, DirectPath, ObjRef, Object, PathSegment, Result};
pub use bytes::{AttachmentBytes, Checksum};
pub use collection::{
Collection, CollectionField, CollectionFieldKind, CollectionFile, CollectionValue, CollectionView,
};
pub use relationship::AfRelationship;
#[cfg(feature = "tree-write")]
pub use write::{EmbeddedFileSpec, write_embedded_file};
use crate::PdfDate;
use crate::annot::AnnotSubtype;
use crate::document::Document;
use crate::id::{AnnotId, AttachmentId, PageId};
use crate::values::{file_spec_name, read_name, text_of};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct AttachedFile {
pub id: AttachmentId,
pub key: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
pub mime: Option<String>,
pub size: Option<u64>,
pub created: Option<PdfDate>,
pub modified: Option<PdfDate>,
pub checksum: Option<[u8; 16]>,
pub relationship: Option<AfRelationship>,
pub embedded: bool,
pub holders: Vec<AttachmentHolder>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AttachmentHolder {
EmbeddedFiles,
Document,
Page(PageId),
Annotation {
page: PageId,
annotation: AnnotId,
},
FileAttachment {
page: PageId,
annotation: AnnotId,
},
Object {
object: u32,
},
}
impl AttachmentHolder {
#[must_use]
pub const fn kind_name(&self) -> &'static str {
match self {
Self::EmbeddedFiles => "embeddedFiles",
Self::Document => "document",
Self::Page(_) => "page",
Self::Annotation { .. } => "annotation",
Self::FileAttachment { .. } => "fileAttachment",
Self::Object { .. } => "object",
}
}
#[must_use]
pub const fn page(&self) -> Option<&PageId> {
match self {
Self::Page(page) | Self::Annotation { page, .. } | Self::FileAttachment { page, .. } => Some(page),
Self::EmbeddedFiles | Self::Document | Self::Object { .. } => None,
}
}
#[must_use]
pub const fn annotation(&self) -> Option<&AnnotId> {
match self {
Self::Annotation { annotation, .. } | Self::FileAttachment { annotation, .. } => Some(annotation),
Self::EmbeddedFiles | Self::Document | Self::Page(_) | Self::Object { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FileSpecAt {
Object(ObjRef),
Direct(DirectPath),
}
pub(crate) struct Found {
pub(crate) attachment: AttachedFile,
pub(crate) at: FileSpecAt,
pub(crate) spec: Dict,
}
impl Document {
pub fn attachments(&self) -> Result<Vec<AttachedFile>> {
Ok(self.found_attachments()?.into_iter().map(|f| f.attachment).collect())
}
pub fn attachment(&self, id: &AttachmentId) -> Result<Option<AttachedFile>> {
Ok(self.find_attachment(id)?.map(|f| f.attachment))
}
pub fn file_spec_at(&self, id: &AttachmentId) -> Result<Option<(FileSpecAt, Dict)>> {
Ok(self.find_attachment(id)?.map(|f| (f.at, f.spec)))
}
fn find_attachment(&self, id: &AttachmentId) -> Result<Option<Found>> {
Ok(self.found_attachments()?.into_iter().find(|f| &f.attachment.id == id))
}
fn found_attachments(&self) -> Result<Vec<Found>> {
let mut list = List::default();
self.list_attachments(&mut list)?;
Ok(list.found)
}
pub(crate) fn every_file_spec(&self) -> Result<Vec<Found>> {
let mut list = List::default();
let seen = self.list_attachments(&mut list)?;
for num in self.object_numbers() {
if seen.contains(&num) {
continue;
}
let Some(object) = laminapdf_cos::tolerant(self.object(num))? else {
continue;
};
let Some(dict) = object.as_dict() else { continue };
let holder = AttachmentHolder::Object { object: num };
self.hold_associated(
&mut list,
dict,
DirectPath::new(Some(ObjRef::new(num, 0)), Vec::new()),
&holder,
)?;
if dict.get("EF").is_some() {
list.add_unheld(self, ObjRef::new(num, 0), dict)?;
}
}
Ok(list.found)
}
fn list_attachments(&self, list: &mut List) -> Result<std::collections::BTreeSet<u32>> {
let mut seen = std::collections::BTreeSet::new();
if let Some(tree) = self.name_tree("EmbeddedFiles")? {
let root_path = self.embedded_files_root_path()?;
for entry in tree.located_entries()? {
let at = match entry.node.as_ref() {
Some(node) => Some(DirectPath::new(
Some(node),
vec![PathSegment::Key("Names"), PathSegment::Index(entry.index)],
)),
None if &entry.node == tree.root() => root_path.clone().map(|mut p| {
p.segments
.extend([PathSegment::Key("Names"), PathSegment::Index(entry.index)]);
p
}),
None => None,
};
let key = Some(laminapdf_syntax::text::decode_text_string(&entry.key));
list.hold(self, &entry.value, at, AttachmentHolder::EmbeddedFiles, key)?;
}
}
let catalog = self.catalog()?;
let catalog_path = match self.trailer().get("Root") {
Some(Object::Ref(r)) => {
seen.insert(r.num);
DirectPath::new(Some(*r), Vec::new())
}
_ => DirectPath::new(None, vec![PathSegment::Key("Root")]),
};
self.hold_associated(list, &catalog, catalog_path, &AttachmentHolder::Document)?;
let pages = self.pages()?;
for page in pages.iter() {
let page_path = page.obj_ref().map(|r| DirectPath::new(Some(r), Vec::new()));
if let Some(path) = page_path.clone() {
seen.extend(path.anchor.map(|r| r.num));
self.hold_associated(list, page.dict(), path, &AttachmentHolder::Page(page.id().clone()))?;
}
let mut annots: Vec<(AnnotId, DirectPath, Dict)> = Vec::new();
self.each_annotation(&page, |id, index, dict| {
annots.push((id.clone(), annotation_path(&page, &id, index), dict.clone()));
Ok(())
})?;
for (annotation, anchor, dict) in annots {
let holder = AttachmentHolder::Annotation {
page: page.id().clone(),
annotation: annotation.clone(),
};
if anchor.segments.is_empty() {
seen.extend(anchor.anchor.map(|r| r.num));
}
self.hold_associated(list, &dict, anchor.clone(), &holder)?;
let subtype = AnnotSubtype::from_name(&read_name(self, dict.get("Subtype"))?.unwrap_or_default());
if subtype == AnnotSubtype::FileAttachment
&& let Some(fs) = dict.get("FS")
{
let mut at = anchor.clone();
at.segments.push(PathSegment::Key("FS"));
let holder = AttachmentHolder::FileAttachment {
page: page.id().clone(),
annotation,
};
list.hold(self, fs, Some(at), holder, None)?;
}
}
}
Ok(seen)
}
fn hold_associated(
&self,
list: &mut List,
owner: &Dict,
owner_path: DirectPath,
holder: &AttachmentHolder,
) -> Result<()> {
let Some(af) = owner.get("AF") else { return Ok(()) };
let Some(items) = self.read(af)? else { return Ok(()) };
let base = if let Some(array) = af.as_ref() {
DirectPath::new(Some(array), Vec::new())
} else {
let mut p = owner_path;
p.segments.push(PathSegment::Key("AF"));
p
};
for (i, item) in items.as_array().unwrap_or_default().iter().enumerate() {
let mut at = base.clone();
at.segments.push(PathSegment::Index(i));
list.hold(self, item, Some(at), holder.clone(), None)?;
}
Ok(())
}
fn embedded_files_root_path(&self) -> Result<Option<DirectPath>> {
let catalog = self.catalog()?;
Ok(match catalog.get("Names") {
Some(Object::Ref(r)) => Some(DirectPath::new(Some(*r), vec![PathSegment::Key("EmbeddedFiles")])),
Some(Object::Dict(_)) => match self.trailer().get("Root") {
Some(Object::Ref(root)) => Some(DirectPath::new(
Some(*root),
vec![PathSegment::Key("Names"), PathSegment::Key("EmbeddedFiles")],
)),
_ => None,
},
_ => None,
})
}
}
fn annotation_path(page: &crate::Page<'_>, id: &AnnotId, index: usize) -> DirectPath {
if let Some(r) = id.as_lamina_id().obj_ref() {
return DirectPath::new(Some(r), Vec::new());
}
crate::annot::direct_path(page, page.dict().get("Annots").and_then(Object::as_ref), index)
}
impl Document {
pub fn annotation_at(&self, id: &AnnotId) -> Result<Option<DirectPath>> {
if let Some(r) = id.as_lamina_id().obj_ref() {
return Ok(Some(DirectPath::new(Some(r), Vec::new())));
}
for page in self.pages()?.iter() {
let mut at = None;
self.each_annotation(&page, |found, index, _| {
if &found == id {
at = Some(annotation_path(&page, &found, index));
}
Ok(())
})?;
if at.is_some() {
return Ok(at);
}
}
Ok(None)
}
}
#[derive(Default)]
struct List {
found: Vec<Found>,
by_object: BTreeMap<u32, usize>,
}
impl List {
fn add_unheld(&mut self, doc: &Document, r: ObjRef, spec: &Dict) -> Result<()> {
if self.by_object.contains_key(&r.num) {
return Ok(());
}
let attachment = read_spec(doc, AttachmentId::object(r), spec)?;
self.by_object.insert(r.num, self.found.len());
self.found.push(Found {
attachment,
at: FileSpecAt::Object(r),
spec: spec.clone(),
});
Ok(())
}
fn hold(
&mut self,
doc: &Document,
value: &Object,
at: Option<DirectPath>,
holder: AttachmentHolder,
key: Option<String>,
) -> Result<()> {
if let Some(r) = value.as_ref()
&& let Some(&i) = self.by_object.get(&r.num)
{
if let Some(found) = self.found.get_mut(i) {
found.attachment.holders.push(holder);
if found.attachment.key.is_none() {
found.attachment.key = key;
}
}
return Ok(());
}
let Some(spec) = doc.read(value)? else { return Ok(()) };
let Some(spec) = spec.as_dict() else { return Ok(()) };
let (id, at) = match (value.as_ref(), at) {
(Some(r), _) => (AttachmentId::object(r), FileSpecAt::Object(r)),
(None, Some(path)) => (AttachmentId::path(&path), FileSpecAt::Direct(path)),
(None, None) => return Ok(()),
};
let mut attachment = read_spec(doc, id, spec)?;
attachment.key = key;
attachment.holders.push(holder);
if let Some(r) = value.as_ref() {
self.by_object.insert(r.num, self.found.len());
}
self.found.push(Found {
attachment,
at,
spec: spec.clone(),
});
Ok(())
}
}
fn read_spec(doc: &Document, id: AttachmentId, spec: &Dict) -> Result<AttachedFile> {
let relationship = read_name(doc, spec.get("AFRelationship"))?.and_then(|n| AfRelationship::from_name(&n));
let mut out = AttachedFile {
id,
key: None,
name: file_spec_name(doc, spec)?,
description: doc.text_entry(spec, "Desc")?,
mime: None,
size: None,
created: None,
modified: None,
checksum: None,
relationship,
embedded: false,
holders: Vec::new(),
};
let Some(Object::Stream(file)) = embedded_stream(doc, spec)?.as_deref().cloned() else {
return Ok(out);
};
out.embedded = true;
out.mime = read_name(doc, file.dict.get("Subtype"))?.map(|n| String::from_utf8_lossy(&n).into_owned());
let params = match file.dict.get("Params") {
Some(p) => doc.read(p)?,
None => None,
};
if let Some(params) = params.as_deref().and_then(Object::as_dict) {
out.size = match params.get("Size") {
Some(s) => doc
.read(s)?
.and_then(|s| s.as_int())
.and_then(|s| u64::try_from(s).ok()),
None => None,
};
out.created = date(doc, params.get("CreationDate"))?;
out.modified = date(doc, params.get("ModDate"))?;
out.checksum = match params.get("CheckSum") {
Some(c) => doc.read(c)?.and_then(|c| match &*c {
Object::String(s) => <[u8; 16]>::try_from(s.bytes.as_slice()).ok(),
_ => None,
}),
None => None,
};
}
Ok(out)
}
pub(crate) fn md5(bytes: &[u8]) -> [u8; 16] {
use md5::{Digest as _, Md5};
Md5::digest(bytes).into()
}
pub(crate) fn embedded_stream(doc: &Document, spec: &Dict) -> Result<Option<std::sync::Arc<Object>>> {
let Some(ef) = spec.get("EF") else { return Ok(None) };
let Some(ef) = doc.read(ef)? else { return Ok(None) };
let Some(ef) = ef.as_dict() else { return Ok(None) };
match ef.get("UF").or_else(|| ef.get("F")) {
Some(f) => doc.read(f),
None => Ok(None),
}
}
fn date(doc: &Document, value: Option<&Object>) -> Result<Option<PdfDate>> {
let Some(value) = value else { return Ok(None) };
Ok(doc
.read(value)?
.as_deref()
.and_then(text_of)
.and_then(|t| t.parse().ok()))
}