mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-03-14 01:42:37 +00:00
22 lines
602 B
Rust
22 lines
602 B
Rust
|
//! # PDF utilities
|
||
|
//!
|
||
|
//! @author Pierre Hubert
|
||
|
|
||
|
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()));
|
||
|
|
||
|
match pdf.get_num_pages() {
|
||
|
Ok(num) if num < 1 =>
|
||
|
Err(ExecError::boxed_string(format!("Detected a PDF with {} pages!", num))),
|
||
|
|
||
|
Ok(_) => Ok(true),
|
||
|
|
||
|
Err(e) => {
|
||
|
println!("Error while parsing PDF: {}", e);
|
||
|
Ok(false)
|
||
|
}
|
||
|
}
|
||
|
}
|