mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 15:29:21 +00:00
Fix PDF validation
This commit is contained in:
parent
24469101bc
commit
9abe17415b
@ -2,17 +2,22 @@
|
|||||||
//!
|
//!
|
||||||
//! @author Pierre Hubert
|
//! @author Pierre Hubert
|
||||||
|
|
||||||
|
use pdf::backend::Backend;
|
||||||
|
|
||||||
use crate::data::error::{ExecError, ResultBoxError};
|
use crate::data::error::{ExecError, ResultBoxError};
|
||||||
|
|
||||||
/// Check out whether a PDF is a valid PDF or not, by trying to open it
|
/// 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> {
|
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() {
|
match backend.read_xref_table_and_trailer() {
|
||||||
Ok(num) if num < 1 =>
|
Ok((refs, _)) => {
|
||||||
Err(ExecError::boxed_string(format!("Detected a PDF with {} pages!", num))),
|
if refs.is_empty() {
|
||||||
|
Err(ExecError::boxed_string(format!("Detected a PDF with 0 references (file size: {})!", file.len())))
|
||||||
Ok(_) => Ok(true),
|
} else {
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("Error while parsing PDF: {}", e);
|
println!("Error while parsing PDF: {}", e);
|
||||||
|
Loading…
Reference in New Issue
Block a user