a
    ˜ëjúL  ã                   @  sÞ   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZmZmZmZmZmZmZmZmZ erÌd dl Z d dl!m"Z" d dl#m$Z$ G dd„ dƒZ%dS )é    )ÚannotationsN)Úcached_property)ÚTYPE_CHECKINGÚcast)Úformat_iso8824_dateÚlogger_warningÚparse_iso8824_date)ÚCatalogAttributes)Ú"FileSpecificationDictionaryEntries)ÚPageAttributes)ÚPdfReadErrorÚ
PyPdfError)
ÚArrayObjectÚByteStringObjectÚDecodedStreamObjectÚDictionaryObjectÚ
NameObjectÚ
NullObjectÚNumberObjectÚStreamObjectÚTextStringObjectÚis_null_or_none)ÚIterator)Ú	PdfWriterc                   @  sŒ  e Zd ZdZdZdddddœdd	„Zedd
œdd„ƒZedddd dœdd„ƒZedddœdd„ƒZ	eddddœdd„ƒZ
edd
œdd„ƒZejdddœdd„ƒZedd
œd d!„ƒZejdddœd"d!„ƒZedd
œd#d$„ƒZejd%ddœd&d$„ƒZed'd
œd(d)„ƒZedd
œd*d+„ƒZedd
œd,d-„ƒZedd
œd.d/„ƒZejd0ddœd1d/„ƒZed2d
œd3d4„ƒZejdddœd5d4„ƒZed6d
œd7d8„ƒZejd9ddœd:d8„ƒZed;d
œd<d=„ƒZejd;ddœd>d=„ƒZed;d
œd?d@„ƒZejd;ddœdAd@„ƒZedBd
œdCdD„ƒZejdEddœdFdD„ƒZedGd
œdHdI„ƒZdd
œdJdK„Zdd
œdLdM„ZeddNddOœdPdQ„ƒZed[ddNdSdTœdUdV„ƒZed\ddNdSdWœdXdY„ƒZdS )]ÚEmbeddedFilezØ
    Container holding the information on an embedded file.

    Attributes are evaluated lazily if possible.

    Further information on embedded files can be found in section 7.11 of the PDF 2.0 specification.
    NÚstrr   zArrayObject | NoneÚNone)ÚnameÚ
pdf_objectÚparentÚreturnc                 C  s   || _ || _|| _dS )zÖ
        Args:
            name: The (primary) name as provided in the name tree.
            pdf_object: The corresponding PDF object to allow retrieving further data.
            parent: The parent list.
        N)Ú_namer   Ú_parent)Úselfr   r   r   © r$   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/generic/_files.pyÚ__init__(   s    zEmbeddedFile.__init__)r    c                 C  s   | j S )a  
        The (primary) name of the embedded file as provided in the name tree.

        .. warning::

            This value can contain arbitrary characters. Please make sure to sanitize it before
            using it to write the file content to the disk for example.
        )r!   ©r#   r$   r$   r%   r   3   s    
zEmbeddedFile.namer   zstr | bytes)Úwriterr   Úcontentr    c                 C  sì   t |tƒr| d¡}tƒ }| |¡ | ttjƒtdƒi¡ t	ƒ }| tdƒ| 
|¡i¡ ddlm} t	ƒ }| 
|¡}tt||ƒƒ}	| ttjƒtdƒttjƒ|	ttjƒ|i¡ |  |¡}
|  |
|	¡}|
 ||¡ |
 ||	¡ | |||
d�S )aO  
        Create a new embedded file and add it to the PdfWriter.

        Args:
            writer: The PdfWriter instance to add the embedded file to.
            name: The filename to display.
            content: The data in the file.

        Returns:
            EmbeddedFile instance for the newly created embedded file.
        úlatin-1z/EmbeddedFilez/Fr   )Úcreate_string_objectz	/Filespec©r   r   r   )Ú
isinstancer   Úencoder   Úset_dataÚupdater   ÚPGZTYPEr   Ú_add_objectÚpypdf.genericr+   r   r   r
   ÚFZEFÚ_get_names_arrayÚ_get_insertion_indexÚinsert)Úclsr(   r   r)   Z
file_entryZef_entryr+   ZfilespecZfilespec_referenceZname_objectÚnames_arrayZinsertion_indexr$   r$   r%   Ú_create_new?   s,    





ýÿ
zEmbeddedFile._create_newr   )r(   r    c           	      C  s  t j|jvr$| tƒ ¡|jtt jƒ< tt|jt j ƒ}d|vrltƒ }ttt jƒ|iƒ}| |¡|tdƒ< |S tt|d ƒ}d|v r–tt|tt jƒ ƒS d|vr¦tdƒ‚tƒ }tt|d  	¡ ƒ}ttt jƒ|iƒ}| |¡|tdƒ< |D ]$}| 	¡  
dg ¡D ]}| |¡ qúqæ|S )zLGet the names array for embedded files, possibly creating and flattening it.ú/EmbeddedFilesú/Namesú/Kidsz2Got neither Names nor Kids in embedded files tree.)ÚCAZNAMESZroot_objectr2   r   r   r   r   r   Ú
get_objectÚgetÚappend)	r8   r(   Z
names_dictÚnamesZembedded_files_names_dictionaryZembedded_files_names_treeÚkidsÚkidr   r$   r$   r%   r5   q   s2    ÿÿzEmbeddedFile._get_names_arrayÚint)r9   r   r    c                   s|   ‡ fdd„t dtˆ ƒdƒD ƒ}| d¡}t ||¡}t ||¡}||krP|d S |dkr\dS |t|ƒ }krt|d S |d S )Nc                   s   g | ]}ˆ |   d ¡‘qS )úutf-8)r.   )Ú.0Úi©r9   r$   r%   Ú
<listcomp>Ÿ   ó    z5EmbeddedFile._get_insertion_index.<locals>.<listcomp>r   é   rF   )ÚrangeÚlenr.   ÚbisectÚbisect_leftÚbisect_right)r8   r9   r   ÚkeysZ
name_bytesÚstartÚendZ	key_countr$   rI   r%   r6   �   s    
z!EmbeddedFile._get_insertion_indexz
str | Nonec                 C  sD   t jt jfD ]2}|| jv r| j|  ¡ }t|ƒstt|ƒ  S qdS )a  
        Retrieve the alternative name (as per the file specification dictionary).

        .. warning::

            This value can contain arbitrary characters. Please make sure to sanitize it before
            using it to write the file content to the disk for example.
        N)r
   ÚUFr4   r   r?   r   r   r   )r#   ÚkeyÚvaluer$   r$   r%   Úalternative_name­   s    

zEmbeddedFile.alternative_namezTextStringObject | None)rW   r    c                 C  sj   |du rFt j| jv r&tƒ | jtt jƒ< t j| jv rftƒ | jtt jƒ< n || jtt jƒ< || jtt jƒ< dS )zDSet the alternative name (as per the file specification dictionary).N)r
   rU   r   r   r   r4   ©r#   rW   r$   r$   r%   rX   À   s    c                 C  s   | j  tj¡}t|ƒrdS |S )zRetrieve the description.N)r   r@   r
   ÚDESCr   rY   r$   r$   r%   ÚdescriptionÌ   s    zEmbeddedFile.descriptionc                 C  s0   |du rt ƒ | jttjƒ< n|| jttjƒ< dS )zSet the description.N)r   r   r   r
   rZ   rY   r$   r$   r%   r[   Ô   s    c                 C  s   t t| j dtdƒ¡ƒS )zJRetrieve the relationship of the referring document to this embedded file.ú/AFRelationshipz/Unspecified)r   r   r   r@   r'   r$   r$   r%   Úassociated_file_relationshipÜ   s    þz)EmbeddedFile.associated_file_relationshipr   c                 C  s   || j tdƒ< dS )zESet the relationship of the referring document to this embedded file.r\   N)r   r   rY   r$   r$   r%   r]   ä   s    r   c                 C  sl   d| j vrtd| j › �ƒ‚tt| j d ƒ}tjtjfD ]"}||v r6tt||  ¡ ƒ  S q6td|› �ƒ‚dS )z)Retrieve the actual embedded file stream.z/EFz/EF entry not found: z'No /(U)F key found in file dictionary: N)	r   r   r   r   r
   rU   r4   r   r?   )r#   ZefrV   r$   r$   r%   Ú_embedded_fileé   s    
zEmbeddedFile._embedded_filec                 C  s   t t| j dtƒ ¡ ¡ ƒS )z&Retrieve the file-specific parameters.ú/Params)r   r   r^   r@   r?   r'   r$   r$   r%   Ú_paramsô   s    zEmbeddedFile._paramsc                 C  s*   | j }d|vrtƒ |tdƒ< tt|d ƒS )z3Ensure the /Params dictionary exists and return it.r_   )r^   r   r   r   )r#   Úembedded_filer$   r$   r%   Ú_ensure_paramsù   s    zEmbeddedFile._ensure_paramsc                 C  s   | j  d¡}t|ƒrdS |S )zERetrieve the subtype. This is a MIME media type, prefixed by a slash.ú/SubtypeN)r^   r@   r   rY   r$   r$   r%   Úsubtype  s    zEmbeddedFile.subtypezNameObject | Nonec                 C  s.   | j }|du rtƒ |tdƒ< n||tdƒ< dS )zGSet the subtype. This should be a MIME media type, prefixed by a slash.Nrc   )r^   r   r   )r#   rW   ra   r$   r$   r%   rd   	  s    Úbytesc                 C  s
   | j  ¡ S )z!Retrieve the actual file content.)r^   Úget_datar'   r$   r$   r%   r)     s    zEmbeddedFile.contentc                 C  s$   t |tƒr| d¡}| j |¡ dS )zSet the file content.r*   N)r-   r   r.   r^   r/   rY   r$   r$   r%   r)     s    

z
int | Nonec                 C  s   | j  d¡}t|ƒrdS |S )z4Retrieve the size of the uncompressed file in bytes.ú/SizeN©r`   r@   r   rY   r$   r$   r%   Úsize  s    zEmbeddedFile.sizezNumberObject | Nonec                 C  s.   | j }|du rtƒ |tdƒ< n||tdƒ< dS )z/Set the size of the uncompressed file in bytes.Nrg   ©rb   r   r   ©r#   rW   Úparamsr$   r$   r%   ri   &  s    zdatetime.datetime | Nonec                 C  s   t | j d¡ƒS )z$Retrieve the file creation datetime.ú/CreationDate©r   r`   r@   r'   r$   r$   r%   Úcreation_date/  s    zEmbeddedFile.creation_datec                 C  s:   | j }|du rtƒ |tdƒ< nt|ƒ}t|ƒ|tdƒ< dS )zSet the file creation datetime.Nrm   ©rb   r   r   r   r   ©r#   rW   rl   Zdate_strr$   r$   r%   ro   4  s
    c                 C  s   t | j d¡ƒS )z4Retrieve the datetime of the last file modification.ú/ModDatern   r'   r$   r$   r%   Úmodification_date>  s    zEmbeddedFile.modification_datec                 C  s:   | j }|du rtƒ |tdƒ< nt|ƒ}t|ƒ|tdƒ< dS )z/Set the datetime of the last file modification.Nrr   rp   rq   r$   r$   r%   rs   C  s
    zbytes | Nonec                 C  s   | j  d¡}t|ƒrdS |S )z5Retrieve the MD5 checksum of the (uncompressed) file.ú	/CheckSumNrh   rY   r$   r$   r%   ÚchecksumM  s    zEmbeddedFile.checksumzByteStringObject | Nonec                 C  s.   | j }|du rtƒ |tdƒ< n||tdƒ< dS )z0Set the MD5 checksum of the (uncompressed) file.Nrt   rj   rk   r$   r$   r%   ru   U  s    zIterator[str]c                 c  s*   | j V  | jd ur&| j| j kr&| jV  d S )N)r   rX   r'   r$   r$   r%   Ú_names^  s    zEmbeddedFile._namesc                 C  sˆ   | j stdƒ‚| j| j v r*| j  | j¡}n6t| jddƒ }durX|| j v rX| j  |¡}ntdƒ‚| j  |¡ | j  |d ¡ tƒ | _dS )z"Delete the file from the document.z-Parent required to delete file from document.Úindirect_referenceNz File not found in parent object.é   )r"   r   r   ÚindexÚgetattrÚpopr   )r#   ry   rw   r$   r$   r%   Údeleted  s    ÿþzEmbeddedFile.deletec                 C  s   d| j j› d| j›d�S )Nú<z name=ú>)Ú	__class__Ú__name__r   r'   r$   r$   r%   Ú__repr__u  s    zEmbeddedFile.__repr__Úbool)ÚmessageÚstrictr    c                 C  s   |rt | ƒ‚t| td� dS )z2Raise an exception in strict mode, warn otherwise.)ÚsourceN)r   r   r€   )rƒ   r„   r$   r$   r%   Ú_report_malformedx  s    zEmbeddedFile._report_malformedFzIterator[EmbeddedFile])rB   r„   r    c                 c  st   t |ƒD ]f\}}t|tƒs| ¡ }t|tƒr.qt|tƒsN| jd|› �|d� q||d   ¡ }t|||d�V  qdS )az  
        Convert the given name tree into class instances.

        Args:
            names: The name tree to load the data from.
            strict: Whether to raise an exception on a file specification that is not a dictionary
                instead of issuing a warning and skipping it.

        Returns:
            Iterable of class instances for the files found.
        z1Embedded file specification is not a dictionary: ©r„   rx   r,   N)Ú	enumerater-   r   r?   r   r   r†   r   )r8   rB   r„   rH   r   Zfile_dictionaryZdirect_namer$   r$   r%   Ú_load_from_names  s    



ÿzEmbeddedFile._load_from_names)Úcatalogr„   r    c           	      c  sÄ  d|vrdS |d }t |tƒr"dS t |tƒsD| jd|› �|d� dS d|vrPdS |d }t |tƒrfdS t |tƒsˆ| jd|› �|d� dS d|v �rb|d  ¡ }t |tƒr°tƒ }n"t |tƒsÒ| jd|› �|d� dS |D ]Š}| ¡ }t |tƒ�s| jd	|› �|d� qÖd|v�rqÖ|d  ¡ }t |tƒ�r*qÖt |tƒ�sL| jd
|› �|d� qÖ| j||d�E dH  qÖd|v �rÀ|d  ¡ }t |tƒ�rˆdS t |tƒ�s¬| jd
|› �|d� dS | j||d�E dH  dS )aÓ  
        Load the embedded files for the given document catalog.

        This method and its signature are considered internal API and thus not exposed publicly for now.

        Args:
            catalog: The document catalog to load from.
            strict: Whether to raise an exception on malformed name trees
                instead of issuing a warning and skipping them.

        Returns:
            Iterable of class instances for the files found.
        r<   Nz Names tree is not a dictionary: r‡   r;   z*Embedded files entry is not a dictionary: r=   z&Embedded files /Kids is not an array: z(Embedded files kid is not a dictionary: z*Embedded files name list is not an array: )r-   r   r   r†   r?   r   r‰   )	r8   rŠ   r„   rB   Ú	containerrC   rD   Z	kid_namesZcontainer_namesr$   r$   r%   Ú_loadœ  sZ    








zEmbeddedFile._load)N)F)F) r€   Ú
__module__Ú__qualname__Ú__doc__r&   Úpropertyr   Úclassmethodr:   r5   r6   rX   Úsetterr[   r]   r^   r`   r   rb   rd   r)   ri   ro   rs   ru   rv   r|   r�   Ústaticmethodr†   r‰   rŒ   r$   r$   r$   r%   r       s|   1+
		r   )&Ú
__future__r   rO   Ú	functoolsr   Útypingr   r   Zpypdf._utilsr   r   r   Zpypdf.constantsr	   r>   r
   r   r1   Zpypdf.errorsr   r   r3   r   r   r   r   r   r   r   r   r   r   ÚdatetimeÚcollections.abcr   Zpypdf._writerr   r   r$   r$   r$   r%   Ú<module>   s   0