
    Phn?                        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
 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      Z$y)    )annotationsN)cached_property)TYPE_CHECKINGcast)format_iso8824_dateparse_iso8824_date)CatalogAttributes)"FileSpecificationDictionaryEntries)PageAttributes)PdfReadError
PyPdfError)
ArrayObjectByteStringObjectDecodedStreamObjectDictionaryObject
NameObject
NullObjectNumberObjectStreamObjectTextStringObjectis_null_or_none)	Generator)	PdfWriterc                     e Zd ZdZd!d"dZed#d       Zed$d       Zed%d       Z	ed&d       Z
ed'd       Zej                  d(d	       Zed'd
       Zej                  d(d       Zed#d       Zej                  d)d       Zed*d       Zed+d       Zed+d       Zed'd       Zej                  d,d       Zed-d       Zej                  d.d       Zed/d       Zej                  d0d       Zed1d       Zej                  d2d       Zed1d       Zej                  d2d       Zed3d       Zej                  d4d       Zd5dZd#dZed6d       Zed7d        Zy)8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.
    Nc                .    || _         || _        || _        y)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)_name
pdf_object_parent)selfnamer   parents       N/var/www/html/saasai/venv/lib/python3.12/site-packages/pypdf/generic/_files.py__init__zEmbeddedFile.__init__(   s     
$    c                    | j                   S )zEThe (primary) name of the embedded file as provided in the name tree.)r   r    s    r#   r!   zEmbeddedFile.name3   s     zzr%   c           	     &   t        |t              r|j                  d      }t               }|j	                  |       |j                  t        t        j                        t        d      i       t               }|j                  t        d      |j                  |      i       ddlm} t               }|j                  |      }t        t         ||            }	|j                  t        t        j                        t        d      t        t        j                         |	t        t        j"                        |i       | j%                  |      }
| j'                  |
|	      }|
j)                  ||       |
j)                  ||	        | |||
      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"   )
isinstancestrencoder   set_dataupdater   PGTYPEr   _add_objectpypdf.genericr*   r   r   r
   FEF_get_names_array_get_insertion_indexinsert)clswriterr!   content
file_entryef_entryr*   filespecfilespec_referencename_objectnames_arrayinsertion_indexs               r#   _create_newzEmbeddedFile._create_new8   sM    gs#nnY/G )*
G$:bgg.
?0KLM $%D)6+=+=j+IJK 	7#%#//9+-A$-GH277#Z%<=??@+=@@A8	
 **6222;L?,>??K8 +FFr%   c                   t         j                  |j                  vr=|j                  t	                     |j                  t        t         j                        <   t        t        |j                  t         j                           }d|vrMt               }t	        t        t         j                        |i      }|j                  |      |t        d      <   |S t        t        |d         }d|v r*t        t        |t        t         j                                 S d|vrt        d      t               }t        t        |d   j                               }t	        t        t         j                        |i      }|j                  |      |t        d      <   |D ]8  }|j                         j                  dg       D ]  }|j                  |        : |S )zLGet the names array for embedded files, possibly creating and flattening it./EmbeddedFiles/Names/Kidsz2Got neither Names nor Kids in embedded files tree.)CANAMESroot_objectr3   r   r   r   r   r   
get_objectgetappend)	r:   r;   
names_dictnamesembedded_files_names_dictionaryembedded_files_names_treekidskidr!   s	            r#   r7   zEmbeddedFile._get_names_arrayj   s    886---7=7I7IJZJ\7]Fz"((34*F,>,>rxx,HI
:-ME.>BHH%u-/+ 8>7I7IJi7jJz"234L %))9:FV;W$X!00%>z"((?S%TUU33STT
 K!:7!C!N!N!PQ*:!5)+
' 4:3E3EFe3f
:./0C
 (,,Xr:T" ;  r%   c                D   t        dt        |      d      D cg c]  }||   j                  d       }}|j                  d      }t        j                  ||      }t        j
                  ||      }||k7  r|dz  S |dk(  ry|t        |      x}k(  r|dz  S |dz  S c c}w )Nr      zutf-8)rangelenr.   bisectbisect_leftbisect_right)	r:   rB   r!   ikeys
name_bytesstartend	key_counts	            r#   r8   z!EmbeddedFile._get_insertion_index   s    8=a[AQST8UV8U1A%%g.8UV[[)
""44!!$
3C<7NA:#d)+Y,q= Qw Ws   Bc                    t         j                  t         j                  fD ]K  }|| j                  v s| j                  |   j	                         }t        |      r;t        t        |      c S  y)z3Retrieve the alternative name (file specification).N)r
   UFr5   r   rL   r   r   r-   )r    keyvalues      r#   alternative_namezEmbeddedFile.alternative_name   s`     799;];_;_`C doo%,779&u-U++ a r%   c                   |t         j                  | j                  v r.t               | j                  t	        t         j                        <   t         j
                  | j                  v r/t               | j                  t	        t         j
                        <   yy|| j                  t	        t         j                        <   || j                  t	        t         j
                        <   y)z.Set the alternative name (file specification).N)r
   rc   r   r   r   r5   r    re   s     r#   rf   zEmbeddedFile.alternative_name   s     =144GU_Ua
+M+P+P QR133tFT^T`
+M+O+O PQ G RWDOOJ'I'L'LMNPUDOOJ'I'K'KLMr%   c                p    | j                   j                  t        j                        }t	        |      ry|S )zRetrieve the description.N)r   rM   r
   DESCr   rh   s     r#   descriptionzEmbeddedFile.description   s/     ##$F$K$KL5!r%   c                    |/t               | j                  t        t        j                        <   y|| j                  t        t        j                        <   y)zSet the description.N)r   r   r   r
   rj   rh   s     r#   rk   zEmbeddedFile.description   s@     =S]S_DOOJ'I'N'NOPSXDOOJ'I'N'NOPr%   c                :    | j                   j                  dd      S )zJRetrieve the relationship of the referring document to this embedded file./AFRelationshipz/Unspecified)r   rM   r'   s    r#   associated_file_relationshipz)EmbeddedFile.associated_file_relationship   s     ""#4nEEr%   c                4    || j                   t        d      <   y)zESet the relationship of the referring document to this embedded file.rn   N)r   r   rh   s     r#   ro   z)EmbeddedFile.associated_file_relationship   s     :?
#456r%   c                <   d| j                   vrt        d| j                          t        t        | j                   d         }t        j
                  t        j                  fD ])  }||v st        t        ||   j                               c S  t        d|       )z)Retrieve the actual embedded file stream.z/EFz/EF entry not found: z'No /(U)F key found in file dictionary: )	r   r   r   r   r
   rc   r5   r   rL   )r    efrd   s      r#   _embedded_filezEmbeddedFile._embedded_file   s     '!6t6GHII"DOOE$:;699;];_;_`CbyL"S'*<*<*>?? a DRDIJJr%   c                f    | j                   j                  dt                     j                         S )z&Retrieve the file-specific parameters./Params)rs   rM   r   rL   r'   s    r#   _paramszEmbeddedFile._params   s*     ""&&y2B2DEPPRRr%   c                t    | j                   }d|vrt               |t        d      <   t        t        |d         S )z3Ensure the /Params dictionary exists and return it.ru   )rs   r   r   r   )r    embedded_files     r#   _ensure_paramszEmbeddedFile._ensure_params   s<     ++M)3C3EM*Y/0$mI&>??r%   c                T    | j                   j                  d      }t        |      ry|S )zERetrieve the subtype. This is a MIME media type, prefixed by a slash./SubtypeN)rs   rM   r   rh   s     r#   subtypezEmbeddedFile.subtype   s*     ##''
35!r%   c                j    | j                   }|t               |t        d      <   y||t        d      <   y)zGSet the subtype. This should be a MIME media type, prefixed by a slash.Nr{   )rs   r   r   )r    re   rx   s      r#   r|   zEmbeddedFile.subtype   s4     ++=4>LM*Z0149M*Z01r%   c                6    | j                   j                         S )z!Retrieve the actual file content.)rs   get_datar'   s    r#   r<   zEmbeddedFile.content  s     ""++--r%   c                |    t        |t              r|j                  d      }| j                  j	                  |       y)zSet the file content.r)   N)r,   r-   r.   rs   r/   rh   s     r#   r<   zEmbeddedFile.content  s0     eS!LL+E$$U+r%   c                T    | j                   j                  d      }t        |      ry|S )z4Retrieve the size of the uncompressed file in bytes./SizeNrv   rM   r   rh   s     r#   sizezEmbeddedFile.size  s(       )5!r%   c                j    | j                   }|t               |t        d      <   y||t        d      <   y)z/Set the size of the uncompressed file in bytes.Nr   ry   r   r   r    re   paramss      r#   r   zEmbeddedFile.size  s4     $$=*4,F:g&'*/F:g&'r%   c                J    t        | j                  j                  d            S )z$Retrieve the file creation datetime./CreationDater   rv   rM   r'   s    r#   creation_datezEmbeddedFile.creation_date  s     "$,,"2"2?"CDDr%   c                    | j                   }|t               |t        d      <   yt        |      }t	        |      |t        d      <   y)zSet the file creation datetime.Nr   ry   r   r   r   r   r    re   r   date_strs       r#   r   zEmbeddedFile.creation_date#  sC     $$=2<,F:o./*51H2B82LF:o./r%   c                J    t        | j                  j                  d            S )z4Retrieve the datetime of the last file modification./ModDater   r'   s    r#   modification_datezEmbeddedFile.modification_date-  s     "$,,"2"2:">??r%   c                    | j                   }|t               |t        d      <   yt        |      }t	        |      |t        d      <   y)z/Set the datetime of the last file modification.Nr   r   r   s       r#   r   zEmbeddedFile.modification_date2  sC     $$=-7\F:j)**51H-=h-GF:j)*r%   c                T    | j                   j                  d      }t        |      ry|S )z5Retrieve the MD5 checksum of the (uncompressed) file.	/CheckSumNr   rh   s     r#   checksumzEmbeddedFile.checksum<  s(       -5!r%   c                j    | j                   }|t               |t        d      <   y||t        d      <   y)z0Set the MD5 checksum of the (uncompressed) file.Nr   r   r   s      r#   r   zEmbeddedFile.checksumD  s4     $$=.8lF:k*+.3F:k*+r%   c                   | j                   st        d      | j                  | j                   v r&| j                   j                  | j                        }nNt	        | j                  dd      x}*|| j                   v r| j                   j                  |      }nt        d      | j                   j                  |       | j                   j                  |dz
         t               | _        y)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    r   r   s      r#   deletezEmbeddedFile.deleteM  s    ||LMM??dll*LL&&t7E'.t@TVZ'[[#h&$,,6LL&&'9:E?@@#*,r%   c                P    d| j                   j                   d| j                  dS )N<z name=>)	__class____name__r!   r'   s    r#   __repr__zEmbeddedFile.__repr__^  s&    4>>**+6$))a@@r%   c              #     K   t        |      D ]L  \  }}t        |t              r|j                         }||dz
     j                         }t	        |||       N yw)z
        Convert the given name tree into class instances.

        Args:
            names: The name tree to load the data from.

        Returns:
            Iterable of class instances for the files found.
        r   r+   N)	enumerater,   r-   rL   r   )r:   rP   r\   r!   file_dictionarydirect_names         r#   _load_from_nameszEmbeddedFile._load_from_namesa  sW      !'GAtdC("&//"3#AEl557"X]^^ (s
   "A9Ac              #    K   	 t        t        t        t        |d         d         }d|v ret        t        |d   j	                               D ]A  }|j	                         }d|v s| j                  t        t        |d               E d{    C d|v r+| j                  t        t        |d               E d{    yy# t        $ r Y yw xY w7 E7 w)aI  
        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.

        Returns:
            Iterable of class instances for the files found.
        rG   rF   NrH   )r   r   KeyErrorr   rL   r   )r:   catalog	containerrT   s       r#   _loadzEmbeddedFile._loadt  s     	 %wx'89:JKI iK7);)F)F)HI nn&s?"33Dc(m4TUUU J y ++Di>Q,RSSS !  		 VSsK   C$C <C%%C
C/C:C;C	C
CCCC)N)r!   r-   r   r   r"   zArrayObject | NonereturnNone)r   r-   )r;   r   r!   r-   r<   str | bytesr   r   )r;   r   r   r   )rB   r   r!   r-   r   int)r   z
str | None)re   zTextStringObject | Noner   r   )re   r   r   r   )r   r   )r   r   )re   zNameObject | Noner   r   )r   bytes)re   r   r   r   )r   z
int | None)re   zNumberObject | Noner   r   )r   datetime.datetime | None)re   r   r   r   )r   zbytes | None)re   zByteStringObject | Noner   r   )r   r   )rP   r   r   Generator[EmbeddedFile])r   r   r   r   )r   
__module____qualname____doc__r$   propertyr!   classmethodrD   r7   r8   rf   setterrk   ro   rs   rv   r   ry   r|   r<   r   r   r   r   r   r   r   r    r%   r#   r   r       s}   	   /G /Gb ) )V   	 	 	V 	V   Y Y F F "((? )? K K S S @ @   ^^: : . . ^^, ,   
[[0 0 E E M M @ @ H H   __4 4-"A _ _$ T Tr%   r   )%
__future__r   rY   	functoolsr   typingr   r   pypdf._utilsr   r   pypdf.constantsr	   rI   r
   r   r1   pypdf.errorsr   r   r4   r   r   r   r   r   r   r   r   r   r   datetimecollections.abcr   pypdf._writerr   r   r   r%   r#   <module>r      sK    "  % & @ 3 > 0 1   )'qT qTr%   