1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Fix PDF validation

This commit is contained in:
Pierre HUBERT 2020-07-08 17:34:44 +02:00
parent 24469101bc
commit 9abe17415b

View File

@ -2,17 +2,22 @@
//!
//! @author Pierre Hubert
use pdf::backend::Backend;
use crate::data::error::{ExecError, ResultBoxError};
/// Check out whether a PDF is a valid PDF or not, by trying to open it
pub fn is_valid_pdf(file: &bytes::Bytes) -> ResultBoxError<bool> {
let pdf = pdf::file::File::new(Vec::from(file.as_ref()));
let backend = file.to_vec();
match pdf.get_num_pages() {
Ok(num) if num < 1 =>
Err(ExecError::boxed_string(format!("Detected a PDF with {} pages!", num))),
Ok(_) => Ok(true),
match backend.read_xref_table_and_trailer() {
Ok((refs, _)) => {
if refs.is_empty() {
Err(ExecError::boxed_string(format!("Detected a PDF with 0 references (file size: {})!", file.len())))
} else {
Ok(true)
}
}
Err(e) => {
println!("Error while parsing PDF: {}", e);