open pdf files, change type to archive

This commit is contained in:
Breval Ferrari 2025-03-20 10:04:35 -04:00
parent 5c3ea7a512
commit ba15f4fda3
No known key found for this signature in database
GPG key ID: F71E304D6400AB8E
2 changed files with 39 additions and 21 deletions

View file

@ -63,7 +63,7 @@ impl Bendable for PdfDocument {
} }
fn format() -> crate::Format { fn format() -> crate::Format {
crate::Format::Doc crate::Format::Archive
} }
} }

View file

@ -13,7 +13,10 @@ mod dynamic {
path::Path, path::Path,
}; };
use crate::snd::{self, Audio}; use crate::{
snd::{self, Audio},
TryFromDataBytes,
};
use super::{ use super::{
img::{self, DynamicImage}, img::{self, DynamicImage},
@ -32,7 +35,7 @@ mod dynamic {
Binary(Bytes), Binary(Bytes),
Sound(Audio), Sound(Audio),
Text, Text,
Doc(PdfDocument), Archive(PdfDocument),
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -43,29 +46,44 @@ mod dynamic {
Image(#[from] img::ImageError), Image(#[from] img::ImageError),
#[error("audio: {0}")] #[error("audio: {0}")]
Audio(#[from] snd::AudioOpenError), Audio(#[from] snd::AudioOpenError),
#[error("pdf: {0}")]
Pdf(String),
} }
pub fn open<P: AsRef<Path>>(path: P) -> Result<Option<DynamicBendable>, OpenError> { pub fn open<P: AsRef<Path>>(path: P) -> Result<Option<DynamicBendable>, OpenError> {
use MatcherType::*; use MatcherType::*;
infer::get_from_path(&path)? infer::get_from_path(&path)?
.map(|t| t.matcher_type()) .map(|t| (t.matcher_type(), t.extension()))
.map(|matcher| -> Result<Option<DynamicBendable>, OpenError> { .map(
Ok(match matcher { |(matcher, extension)| -> Result<Option<DynamicBendable>, OpenError> {
Image => Some(DynamicBendable::Image(img::open(path)?)), Ok(match matcher {
App | Archive => Some(DynamicBendable::Binary({ Image => Some(DynamicBendable::Image(img::open(path)?)),
let mut buf = Vec::new(); App | Archive if extension != "pdf" => Some(DynamicBendable::Binary({
File::open(path)?.read_to_end(&mut buf)?; let mut buf = Vec::new();
buf File::open(path)?.read_to_end(&mut buf)?;
})), buf
Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)), })),
Book => todo!(), App => unreachable!(),
Doc => todo!(), Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)),
Font => todo!(), Archive => Some(DynamicBendable::Archive(
Text => todo!(), PdfDocument::try_from_data_bytes(
Video => todo!(), File::open(path)?
Custom => None, .bytes()
}) .collect::<Result<Bytes, io::Error>>()?,
}) (),
Default::default(),
)
.map_err(OpenError::Pdf)?,
)),
Book => todo!(),
Doc => todo!(),
Font => todo!(),
Text => todo!(),
Video => todo!(),
Custom => None,
})
},
)
.transpose() .transpose()
.map(|opt| -> Option<DynamicBendable> { opt? }) .map(|opt| -> Option<DynamicBendable> { opt? })
} }