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,21 +46,35 @@ 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(
|(matcher, extension)| -> Result<Option<DynamicBendable>, OpenError> {
Ok(match matcher { Ok(match matcher {
Image => Some(DynamicBendable::Image(img::open(path)?)), Image => Some(DynamicBendable::Image(img::open(path)?)),
App | Archive => Some(DynamicBendable::Binary({ App | Archive if extension != "pdf" => Some(DynamicBendable::Binary({
let mut buf = Vec::new(); let mut buf = Vec::new();
File::open(path)?.read_to_end(&mut buf)?; File::open(path)?.read_to_end(&mut buf)?;
buf buf
})), })),
App => unreachable!(),
Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)), Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)),
Archive => Some(DynamicBendable::Archive(
PdfDocument::try_from_data_bytes(
File::open(path)?
.bytes()
.collect::<Result<Bytes, io::Error>>()?,
(),
Default::default(),
)
.map_err(OpenError::Pdf)?,
)),
Book => todo!(), Book => todo!(),
Doc => todo!(), Doc => todo!(),
Font => todo!(), Font => todo!(),
@ -65,7 +82,8 @@ mod dynamic {
Video => todo!(), Video => todo!(),
Custom => None, Custom => None,
}) })
}) },
)
.transpose() .transpose()
.map(|opt| -> Option<DynamicBendable> { opt? }) .map(|opt| -> Option<DynamicBendable> { opt? })
} }