mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Fix PDF validation
This commit is contained in:
		@@ -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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user