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 {
crate::Format::Doc
crate::Format::Archive
}
}

View file

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