Ë
    ‹ÎùjŸ	  ã                   óˆ   — d Z ddl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
  G d„ d«      Zd	eeed
f      fd„Zdedefd„Zy)u¡  
zip_stream.py â€” a zip built WHILE it is sent, from files that live in the photo storage.

Nothing is written to this disk (the app server has little room since the photos moved to MinIO), so a family's
download or the lab's folder of an order never waits for the whole archive. Deflate level 1: a stored entry in a
streamed zip needs a data descriptor that some unzip tools refuse, and JPEGs barely shrink anyway.
é    N)ÚCallable)Úquote)Ústorage)ÚKeyc                   ó6   — e Zd ZdZd„ Zdefd„Zdd„Zdefd„Z	y)	ÚZipSinkzhWrite-only, non-seekable target for zipfile: what it writes is handed out in chunks as the zip is built.c                 ó"   — t        «       | _        y ©N)Ú	bytearrayÚbuf©Úselfs    ú./var/www/photos/backend/services/zip_stream.pyÚ__init__zZipSink.__init__   s   € Ü“;ˆ�ó    Úreturnc                 óB   — | xj                   |z  c_         t        |«      S r
   )r   Úlen)r   Úbs     r   ÚwritezZipSink.write   s   € Ø�Š�A‰�Ü�1‹vˆr   Nc                  ó   — y r
   © r   s    r   ÚflushzZipSink.flush   s   € Ør   c                 ód   — t        | j                  «      }| j                  j                  «        |S r
   )Úbytesr   Úclear)r   Úouts     r   ÚtakezZipSink.take   s"   € Ü�D—H‘H‹oˆØ�‰�‰ÔØˆ
r   )r   N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úintr   r   r   r   r   r   r   r   r      s'   „ Ùròð˜#ó óð�eô r   r   Úentriesz!Key | bytes | Callable[[], bytes]c           
   #   óL  K  — t        «       }t        j                  |dt        j                  d¬«      5 }| D �]5  \  }}t	        |«      r |«       }t        |t        t        f«      }t        j                  |t        j                  |rt        j                  «       nt        j                  |«      j                  «      dd ¬«      }t        j                  |_        |j                  |d«      5 }|rt        |«      gnt        j                   |«      D ]>  }|j#                  |«       t%        |j&                  «      dk\  sŒ-|j)                  «       –— Œ@ 	 ddd«       |j)                  «       –— �Œ8 	 ddd«       |j)                  «       –— y# 1 sw Y   Œ:xY w# 1 sw Y   Œ(xY w­w)u&  Yield the zip of `entries` = [(name inside the zip, storage key â€” or bytes made on the fly, like a recap PDF,
    or a function that makes them when their turn comes, like a montage)] in chunks. Files are copied byte for byte
    (deflate is lossless): the lab gets the originals as uploaded.Úwé   )ÚcompresslevelNé   )Ú	date_timei   )r   ÚzipfileÚZipFileÚZIP_DEFLATEDÚcallableÚ
isinstancer   r   ÚZipInfoÚtimeÚ	localtimer   ÚstatÚmtimeÚcompress_typeÚopenÚiter_chunksr   r   r   r   )	r$   ÚsinkÚzfÚarcnameÚkÚmadeÚinfoÚentryÚchunks	            r   Ústreamr@   $   sJ  è ø€ ô ‹9€DÜ	�‰˜˜s¤G×$8Ñ$8ÈÔ	Jð ÈbØ!ó 	‰JˆG�QÜ˜Œ{Ù“C�Ü˜a¤%¬Ð!3Ó4ˆDÜ—?‘? 7´d·n±nÑTXÄTÇYÁYÄ[Ô^e×^jÑ^jÐklÓ^m×^sÑ^sÓ6tÐuwÐvwÐ6xÔyˆDÜ!(×!5Ñ!5ˆDÔØ—‘˜˜sÓ#ð * uÙ,0œu Q›x™j´g×6IÑ6IÈ!Ó6Lò *�EØ—K‘K Ô&Ü˜4Ÿ8™8“}¨
Ó2Ø"Ÿi™i›kÓ)ñ*÷*ð
 —)‘)“+Ôñ	÷ð �)‰)‹+Ó÷*ð *ú÷ð üs<   ‚2F$´B9FÃ-AFÄ>FÅFÅ1F$ÆFÆFÆF!ÆF$Únamer   c                 ó¤   — | j                  dd«      j                  «       j                  dd«      j                  dd«      }d|› dt        | «      › �S )zQContent-Disposition for a download: an ASCII fallback plus the real (UTF-8) name.ÚasciiÚreplaceú"Ú_Ú?zattachment; filename="z"; filename*=utf-8'')ÚencodeÚdecoderD   r   )rA   Ú
ascii_names     r   Ú
attachmentrK   9   sO   € à—‘˜W iÓ0×7Ñ7Ó9×AÑAÀ#ÀsÓK×SÑSÐTWÐY\Ó]€JØ$ Z LÐ0EÄeÈDÃkÀ]ÐSÐSr   )r"   r1   r+   Útypingr   Úurllib.parser   Úcorer   Úcore.storager   r   ÚlistÚtupleÚstrr@   rK   r   r   r   ú<module>rS      sZ   ðñó Û Ý Ý å Ý ÷ñ ð&�D˜˜sÐ$GÐGÑHÑIó ð*T�Sð T˜Sô Tr   