deps updated

particularly quick_xml which caused some code changes. Lots of testing should be done.
This commit is contained in:
Martin Brodbeck 2024-11-05 14:01:48 +01:00
parent d5c2b277dc
commit 23c126ec9a
3 changed files with 304 additions and 98 deletions

View file

@ -1,4 +1,5 @@
use std::{
borrow::Borrow,
collections::HashMap,
fs::{self, File},
io::Read,
@ -52,17 +53,19 @@ fn get_rootfile(archive: &mut ZipArchive<File>) -> String {
container.read_to_string(&mut xml_str_buffer).unwrap();
let mut reader = Reader::from_str(&xml_str_buffer);
reader.trim_text(true);
reader.config_mut().trim_text(true);
let mut buf = Vec::new();
let mut opf_filename = String::new();
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) if e.local_name() == b"rootfile" => {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e))
if e.local_name().into_inner() == b"rootfile" =>
{
opf_filename = String::from_utf8(
e.attributes()
.filter(|attr| attr.as_ref().unwrap().key == b"full-path")
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"full-path")
.next()
.unwrap()
.unwrap()
@ -128,22 +131,22 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
let mut xml_authors = HashMap::new();
loop {
match reader.read_event(&mut buf) {
match reader.read_event_into(&mut buf) {
// See if we have EPUB3 or EPUB2
Ok(Event::Start(ref e)) if e.local_name() == b"package" => {
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"package" => {
if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"version"
attr.as_ref().unwrap().key.into_inner() == b"version"
&& attr.as_ref().unwrap().value.starts_with(b"3")
}) {
is_epub3 = true;
}
}
Ok(Event::Start(ref e)) if e.local_name() == b"creator" => {
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"creator" => {
creator_found = true;
if is_epub3 {
if let Some(idval) = e
.attributes()
.filter(|attr| attr.as_ref().unwrap().key == b"id")
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"id")
.next()
{
curr_id = "#".to_string()
@ -159,19 +162,26 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
} else {
if let Some(file_as_val) = e
.attributes()
.filter(|attr| attr.as_ref().unwrap().key.ends_with(b"file-as"))
.filter(|attr| {
attr.as_ref()
.unwrap()
.key
.into_inner()
.ends_with(b"file-as")
})
.next()
{
curr_id = "none".to_string() + xml_authors.len().to_string().as_str();
let entry = xml_authors.entry(curr_id.clone()).or_insert(XmlAut::new());
entry.sort = file_as_val
.unwrap()
.unescape_and_decode_value(&reader)
.unwrap_or_default();
.decode_and_unescape_value(*reader.decoder().borrow())
.unwrap_or_default()
.to_string();
entry.role = "aut".to_string();
} else if let Some(_role_val) = e
.attributes()
.filter(|attr| attr.as_ref().unwrap().key.ends_with(b"role"))
.filter(|attr| attr.as_ref().unwrap().key.into_inner().ends_with(b"role"))
.next()
{
curr_id = "none".to_string() + xml_authors.len().to_string().as_str();
@ -190,33 +200,33 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
creator_found = false;
}
Ok(Event::Start(ref e)) if e.local_name() == b"meta" && is_epub3 => {
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"meta" && is_epub3 => {
if let Some(refines) = e
.attributes()
.filter(|attr| attr.as_ref().unwrap().key == b"refines")
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"refines")
.next()
{
if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"property"
attr.as_ref().unwrap().key.into_inner() == b"property"
&& attr.as_ref().unwrap().value.ends_with(b"file-as")
}) {
curr_id = String::from_utf8(refines.unwrap().value.to_vec()).unwrap();
file_as_found = true;
} else if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"property"
attr.as_ref().unwrap().key.into_inner() == b"property"
&& attr.as_ref().unwrap().value.ends_with(b"role")
}) {
curr_id = String::from_utf8(refines.unwrap().value.to_vec()).unwrap();
role_found = true;
} else if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"property"
attr.as_ref().unwrap().key.into_inner() == b"property"
&& attr.as_ref().unwrap().value.ends_with(b"group-position")
}) {
series_index_found = true;
}
}
if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"property"
attr.as_ref().unwrap().key.into_inner() == b"property"
&& attr
.as_ref()
.unwrap()
@ -226,40 +236,41 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
series_found = true;
}
}
Ok(Event::Empty(ref e)) if e.local_name() == b"meta" && !is_epub3 => {
Ok(Event::Empty(ref e)) if e.local_name().into_inner() == b"meta" && !is_epub3 => {
if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"name"
attr.as_ref().unwrap().key.into_inner() == b"name"
&& attr
.as_ref()
.unwrap()
.unescaped_value()
.unescape_value()
.unwrap()
.ends_with(b"series")
.ends_with("series")
}) {
epub_meta.series.name = e
.attributes()
.filter(|attr| attr.as_ref().unwrap().key == b"content")
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"content")
.next()
.unwrap()
.unwrap()
.unescape_and_decode_value(&reader)
.unwrap_or_default();
.decode_and_unescape_value(*reader.decoder().borrow())
.unwrap_or_default()
.to_string();
} else if e.attributes().any(|attr| {
attr.as_ref().unwrap().key == b"name"
attr.as_ref().unwrap().key.into_inner() == b"name"
&& attr
.as_ref()
.unwrap()
.unescaped_value()
.unescape_value()
.unwrap()
.ends_with(b"series_index")
.ends_with("series_index")
}) {
let index_float = e
.attributes()
.filter(|attr| attr.as_ref().unwrap().key == b"content")
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"content")
.next()
.unwrap()
.unwrap()
.unescape_and_decode_value(&reader)
.decode_and_unescape_value(*reader.decoder().borrow())
.unwrap_or_default()
.parse::<f32>()
.unwrap_or_default();
@ -291,11 +302,12 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
series_index_found = false;
}
Ok(Event::Start(ref e)) if e.local_name() == b"subject" => {
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"subject" => {
genre_found = true;
}
Ok(Event::Text(ref e)) if genre_found => {
epub_meta.genre = e.unescape_and_decode(&reader).unwrap();
//epub_meta.genre = e.unescape_and_decode(&reader).unwrap();
epub_meta.genre = e.unescape().unwrap().to_string();
genre_found = false;
}
Ok(Event::Eof) => break,