a
    �˜ëjb  ã                   @   s^   d Z ddlZddlmZ ddlmZ ddlmZ	 ddlm
Z
 ddlmZ G dd„ de	jƒZdS ))ÚPdfAttachmenté    N)Údecode)ÚFPDF_WCHAR_size)ÚPdfiumErrorc                   @   sh   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r   aN  
    Attachment helper class. See PDF 1.7, Section 7.11 "File Specifications".
    
    Attributes:
        raw (FPDF_ATTACHMENT):
            The underlying PDFium attachment handle.
        pdf (PdfDocument):
            Reference to the document this attachment belongs to. Must remain valid as long as the attachment is used.
    c                 C   s   || _ || _d S )N)ÚrawÚpdf)Úselfr   r   © r	   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdfium2/_helpers/attachment.pyÚ__init__   s    zPdfAttachment.__init__c                 C   sZ   t  | dd¡}|stdƒ‚|t   }t j| ƒ }t  | ||¡ tt|ƒd|d … dƒS )zC
        Returns:
            str: Name of the attachment.
        Nr   zFailed to get attachment name.é   ú	utf-16-le)Úpdfium_cZFPDFAttachment_GetNamer   r   Ú
FPDF_WCHARr   Ú
memoryview©r   Ún_bytesÚn_unitsÚbufferr	   r	   r
   Úget_name!   s    zPdfAttachment.get_namec                 C   s‚   t  ¡ }t | dd|¡ |j}|s*tdƒ‚t j| ƒ }t  ¡ }t | |||¡}|j}|s`tdƒ‚||k r~td|› d|› d�ƒ‚|S )zs
        Returns:
            ctypes.Array: The attachment's file data (as :class:`~ctypes.c_char` array).
        Nr   z'Failed to extract attachment (no data).z,Failed to extract attachment (error status).z'Failed to extract attachment (expected z bytes, but got z).)ÚctypesÚc_ulongr   ZFPDFAttachment_GetFileÚvaluer   Úc_char)r   r   r   Z
out_buflenÚokr	   r	   r
   Úget_data.   s    zPdfAttachment.get_datac                 C   s&   t  | | j|t|ƒ¡}|s"tdƒ‚dS )aµ  
        Set the attachment's file data.
        If this function is called on an existing attachment, it will be changed to point at the new data, but the previous data will not be removed from the file (as of PDFium 5418).
        
        Parameters:
            data (bytes | ctypes.Array):
                New file data for the attachment. May be any data type that can be implicitly converted to :class:`~ctypes.c_void_p`.
        zFailed to set attachment data.N)r   ZFPDFAttachment_SetFiler   Úlenr   )r   Údatar   r	   r	   r
   Úset_dataE   s    	zPdfAttachment.set_datac                 C   s   t  | |d  d¡¡S )zæ
        Parameters:
            key (str):
                A key to look for in the attachment's params dictionary.
        Returns:
            bool: True if *key* is contained in the params dictionary, False otherwise.
        ú úutf-8)r   ZFPDFAttachment_HasKeyÚencode©r   Úkeyr	   r	   r
   Úhas_keyR   s    zPdfAttachment.has_keyc                 C   s   t  | |d  d¡¡S )zx
        Returns:
            int: Type of the value of *key* in the params dictionary (:attr:`FPDF_OBJECT_*`).
        r   r    )r   ZFPDFAttachment_GetValueTyper!   r"   r	   r	   r
   Úget_value_type\   s    zPdfAttachment.get_value_typec                 C   sx   |d   d¡}t | |dd¡}|dkr6td|› d�ƒ‚|t   }tj| ƒ }t | |||¡ tt|ƒd|d … dƒS )	zÜ
        Returns:
            str: The value of *key* in the params dictionary, if it is a string or name.
            Otherwise, an empty string will be returned. On other failures, an exception will be raised.
        r   r    Nr   zFailed to get value of key 'ú'.r   r   )r!   r   ZFPDFAttachment_GetStringValuer   r   r   r   r   )r   r#   Úenc_keyr   r   r   r	   r	   r
   Úget_str_valuec   s    zPdfAttachment.get_str_valuec                 C   sV   |d   d¡}|d   d¡}t |tj¡}t | ||¡}|sRtd|› d|› d�ƒ‚dS )z§
        Set the attribute specified by *key* to the string *value*.
        
        Parameters:
            value (str): New string value for the attribute.
        r   r    r   z Failed to set attachment param 'z' to 'r&   N)r!   r   Úcastr   ÚFPDF_WIDESTRINGZFPDFAttachment_SetStringValuer   )r   r#   r   r'   Z	enc_valueZenc_value_ptrr   r	   r	   r
   Úset_str_valueu   s    zPdfAttachment.set_str_valuec                 C   sf   t  | dd¡}|tkrdS |s&tdƒ‚|t   }t j| ƒ }t  | ||¡ tt|ƒd|d … dƒS )av  
        Returns:
            str: The attachment's MIME type (``/Subtype``), as string.
            Returns an empty string if the attachment does not specify the subtype key.
        Hint:
            Checking the attached file's signature with ``libmagic`` may be a more reliable way of determining the MIME type; however, this implies calling :meth:`.get_data`.
        Nr   Ú z Failed to get attachment subtyper   r   )r   ZFPDFAttachment_GetSubtyper   r   r   r   r   r   r	   r	   r
   Úget_subtype…   s    zPdfAttachment.get_subtypec                 C   sf   t  | dd¡}|tkrdS |s&tdƒ‚|t   }t j| ƒ }t  | ||¡ tt|ƒd|d … dƒS )z‚
        Returns:
            str: The attachment's description, or an empty string if the attachment has no description.
        Nr   r,   z$Failed to get attachment descriptionr   r   )r   ZFPDFAttachment_GetDescriptionr   r   r   r   r   r   r	   r	   r
   Úget_desc—   s    zPdfAttachment.get_descc                 C   s8   |d   d¡}t |tj¡}t | |¡}|s4tdƒ‚dS )zÔ
        Parameters:
            string (str): Set the attachment description to this value.
        
        .. versionadded:: 5.14
            :meth:`.get_subtype`, :meth:`.get_desc`, :meth:`.set_desc`
        r   r   z$Failed to set attachment descriptionN)r!   r   r)   r   r*   ZFPDFAttachment_SetDescriptionr   )r   ÚstringZ
enc_stringZenc_string_ptrr   r	   r	   r
   Úset_desc§   s
    zPdfAttachment.set_descN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r$   r%   r(   r+   r-   r.   r0   r	   r	   r	   r
   r      s   
r   )Ú__all__r   Úcodecsr   Zpypdfium2.rawr   r   Zpypdfium2.internalZinternalZpdfium_ir   Zpypdfium2._helpers.miscr   ZAutoCastabler   r	   r	   r	   r
   Ú<module>   s   