a
    ž˜ëj‚  ã                   @  s¼   d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ e e¡Ze d¡Ze ej¡ G d	d
„ d
eƒZdddœdd„Zdddœdd„ZdS )é    )ÚannotationsN)ÚcastÚOptionalÚBinaryIOÚUnion)Ú	PdfReader)ÚFileNotDecryptedErrorÚPdfReadError)ÚUNSTRUCTURED_CLIENT_LOGGER_NAME)ÚFileValidationErrorÚpypdfc                      s&   e Zd ZdZddœ‡ fdd„Z‡  ZS )ÚPDFValidationErrorz$Exception for PDF validation errors.Ústr)Úmessagec                   s   t ƒ j|dd� d S )NZPDF)Ú	file_type)ÚsuperÚ__init__)Úselfr   ©Ú	__class__© új/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured_client/_hooks/custom/pdf_utils.pyr      s    zPDFValidationError.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r      s   r   zUnion[BinaryIO, bytes]zOptional[PdfReader])Úpdf_fileÚreturnc              	   C  sJ   z,t | tƒr tt| ƒ}t |¡} t| dd�W S  ttfyD   Y dS 0 dS )z¥Reads the given PDF file.

    Args:
        pdf_file: The PDF file to be read.

    Returns:
        The PdfReader object if the file is a PDF, None otherwise.
    F)ÚstrictN)Ú
isinstanceÚbytesr   ÚioÚBytesIOr   r	   ÚUnicodeDecodeError)r   Úcontentr   r   r   Úread_pdf   s    



r&   r   )Úpdfr   c              
   C  s‚   z| j  | j t| jƒ | W S  tyJ } ztdƒ|‚W Y d}~n<d}~0  ty| } ztd|› �ƒ|‚W Y d}~n
d}~0 0 dS )z¬
    Check if PDF is:
    - Encrypted
    - Has corrupted pages
    - Has corrupted root object

    Throws:
    - PDFValidationError if file is encrypted or corrupted
    z3File is encrypted. Please decrypt it with password.Nz/File does not appear to be a valid PDF. Error: )ÚmetadataZroot_objectÚlistZpagesr   r   r	   )r'   Úer   r   r   Ú	check_pdf/   s"    

ÿþÿþr+   )Ú
__future__r   r"   ÚloggingÚtypingr   r   r   r   r   r   Zpypdf.errorsr   r	   Z(unstructured_client._hooks.custom.commonr
   Z3unstructured_client._hooks.custom.validation_errorsr   Ú	getLoggerÚloggerZ
pdf_loggerÚsetLevelÚERRORr   r&   r+   r   r   r   r   Ú<module>   s   

