Skip to content

Commit

Permalink
Merge pull request #38 from thibaultamartin/lookup-cover
Browse files Browse the repository at this point in the history
Find the cover id either in the metadata or in the manifest
  • Loading branch information
danigm committed Jun 26, 2023
2 parents c53a54c + 355495a commit 455f07e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["epub", "ebook"]
license = "GPL-3.0"
name = "epub"
repository = "https://github.com/danigm/epub-rs.git"
version = "2.0.0"
version = "2.1.0"
edition = "2018"

[dependencies]
Expand Down
46 changes: 32 additions & 14 deletions src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ pub struct EpubDoc<R: Read + Seek> {

/// unique identifier
pub unique_identifier: Option<String>,

/// The id of the cover, if any
pub cover_id: Option<String>,
}

impl EpubDoc<BufReader<File>> {
Expand Down Expand Up @@ -173,6 +176,7 @@ impl<R: Read + Seek> EpubDoc<R> {
current: 0,
extra_css: vec![],
unique_identifier: None,
cover_id: None,
};
doc.fill_resources()?;
Ok(doc)
Expand Down Expand Up @@ -210,9 +214,8 @@ impl<R: Read + Seek> EpubDoc<R> {
///
/// This returns the cover id, which can be used to get the cover data.
/// The id is not guaranteed to be valid.
pub fn get_cover_id(&self) -> String {
self.mdata("cover")
.unwrap_or_else(|| "coverimagestandard".into())
pub fn get_cover_id(&self) -> Option<String> {
self.cover_id.clone()
}

/// Returns the cover's content and mime-type
Expand All @@ -239,8 +242,9 @@ impl<R: Read + Seek> EpubDoc<R> {
/// Returns [`None`] if the cover can't be found.
pub fn get_cover(&mut self) -> Option<(Vec<u8>, String)> {
let cover_id = self.get_cover_id();
let cover_data = self.get_resource(&cover_id)?;
Some(cover_data)
cover_id.and_then(|cid| {
self.get_resource(&cid)
})
}

/// Returns Release Identifier defined at
Expand Down Expand Up @@ -595,25 +599,19 @@ impl<R: Read + Seek> EpubDoc<R> {
let container = self.archive.get_entry(&self.root_file)?;
let root = xmlutils::XMLReader::parse(container.as_slice())?;
let unique_identifier_id = &root.borrow().get_attr("unique-identifier");
// resources from manifest
let manifest = root
.borrow()
.find("manifest")
.ok_or(DocError::InvalidEpub)?;
for r in &manifest.borrow().children {
let item = r.borrow();
let _ = self.insert_resource(&item);
}

// items from spine
let spine = root.borrow().find("spine").ok_or(DocError::InvalidEpub)?;
for r in &spine.borrow().children {
let item = r.borrow();
let _ = self.insert_spine(&item);
}

// toc.ncx
if let Some(toc) = spine.borrow().get_attr("toc") {
let _ = self.fill_toc(&toc);
}

// metadata
let metadata = root
.borrow()
Expand All @@ -623,6 +621,9 @@ impl<R: Read + Seek> EpubDoc<R> {
let item = r.borrow();
if item.name.local_name == "meta" {
if let (Some(k), Some(v)) = (item.get_attr("name"), item.get_attr("content")) {
if k == "cover" {
self.cover_id = Some(v.clone());
}
self.metadata.entry(k).or_default().push(v);
} else if let Some(k) = item.get_attr("property") {
let v = item.text.clone().unwrap_or_default();
Expand Down Expand Up @@ -650,6 +651,23 @@ impl<R: Read + Seek> EpubDoc<R> {
}
}
}

// resources from manifest
let manifest = root
.borrow()
.find("manifest")
.ok_or(DocError::InvalidEpub)?;
for r in &manifest.borrow().children {
let item = r.borrow();
if self.cover_id.is_none() {
if let (Some(id), Some(property)) = (item.get_attr("id"), item.get_attr("properties")) {
if property == "cover-image" {
self.cover_id = Some(id);
}
}
}
let _ = self.insert_resource(&item);
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn doc_open() {

{
let cover = doc.get_cover_id();
assert_eq!(cover, "portada.png");
assert_eq!(cover, Some("portada.png".into()));
}

{
Expand Down

0 comments on commit 455f07e

Please sign in to comment.