Ë
    ¼vëjb  ã                  óê   — d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZ  ej                  d«      ZdZd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efdd„Zdd„Zef	 	 	 dd„Zdd„Zy)aÊ  Each organisation's files in its own Azure container, the same way in every product.

The container is the one the platform mapped to the tenant (master `Tenants.bucketName`, normalised as Node's
normalizeAzureContainerName does): never derived from an id, never taken from a request, never a shared fallback.
Files sit at the container's root by category (hero/, brand/, chat/<thread>/, ...). Containers are private; a file a
customer's mail client or browser must load is served by Node through a permanent signed link
(`sign_asset_url`), so no Azure address or credential leaves a product for a new file.

Products pass their own connection, Azure client, base URL and signing key; nothing here reads settings.
é    )ÚannotationsN)ÚIterableÚOptionalÚTuplez>^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$)Úmediazmy-templatesÚchatÚrenders)ÚpngÚjpgÚjpegÚwebpÚgifÚmp4z/api/public/tenant-assets/z/api/public/template-assets/c                óÒ   — t        | xs d«      j                  «       j                  «       }|j                  d«      r|dd n|}t        j                  |«      st        | ›d�«      ‚|S )z]The tenant's id as the master table holds it; a schema name (org_<id>) names the same tenant.Ú Úorg_é   Nz is not a tenant id)ÚstrÚstripÚlowerÚ
startswithÚUUIDÚmatchÚ
ValueError)Ú	tenant_idÚtids     úb/var/www/html/strategist-ai/venv/lib/python3.12/site-packages/galaxiq_utils/storage/tenant_blob.pyÚtenant_uuidr      s]   € ä
ˆiŠo˜2Ó
×
$Ñ
$Ó
&×
,Ñ
,Ó
.€CØ—^‘^ FÔ+ˆ#ˆaˆb‰'°€CÜ�:‰:�cŒ?Ü˜I˜=Ð(;Ð<Ó=Ð=Ø€Jó    c                ó:  — t        j                  ddt        | xs d«      j                  «       j	                  «       j                  dd«      «      }t        j                  dd|«      j                  d«      }dt        |«      cxk  rdk  sn t        | ›d�«      ‚|S )	z¿Node's normalizeAzureContainerName without its fallback: lowercase, underscores and anything not a letter,
    digit or hyphen become hyphens, runs collapse, ends trimmed, 3 to 63 characters.z
[^a-z0-9-]ú-r   Ú_z-+é   é?   z% is not a usable Azure container name)ÚreÚsubr   r   r   ÚreplaceÚlenr   )ÚrawÚss     r   Úcontainer_namer+   (   sƒ   € ô 	�‰ˆ}˜c¤3 s¢y¨b£>×#7Ñ#7Ó#9×#?Ñ#?Ó#A×#IÑ#IÈ#ÈsÓ#SÓT€AÜ
�‰ˆu�c˜1Ó×#Ñ# CÓ(€AØ”�A“Ô˜"ÔÜ˜C˜7Ð"GÐHÓIÐIØ€Hr   c                óP  — t        |«      }| j                  d|f«      j                  «       }|st        d|› d�«      ‚t	        |t
        «      r
|d   |d   fn|\  }}t        |xs d«      j                  «       dk7  rt        d|› d�«      ‚|st        d|› d	�«      ‚t        |«      S )
z¨The container mapped to this tenant, read with a connection to the master database: an Azure tenant that is
    not deleted and has a mapping. Anything else is refused.zoSELECT "bucketName", "storageProvider" FROM public."Tenants" WHERE id = %s AND NOT coalesce("isDeleted", false)ztenant z is not an active tenantÚ
bucketNameÚstorageProviderr   Úazurez does not store on Azurez  has no storage container mapped)	r   ÚexecuteÚfetchoner   Ú
isinstanceÚdictr   r   r+   )Úconnr   r   ÚrowÚbucketÚproviders         r   Úread_tenant_containerr8   2   sÆ   € ô �iÓ
 €CØ
�,‰,ð LØNQÈVóUßU]ÑU]ÓU_ð áÜ˜7 3 %Ð'?Ð@ÓAÐAÜFPÐQTÔVZÔF[˜˜LÑ)¨3Ð/@Ñ+AÑBÐadÑ€FˆHÜ
ˆ8Š>�rÓ× Ñ Ó" gÒ-Ü˜7 3 %Ð'?Ð@ÓAÐAÙÜ˜7 3 %Ð'GÐHÓIÐIÜ˜&Ó!Ð!r   c                ó°   — ddl m}m} | j                  |«      }	 |j	                  «        |S # |$ r# 	 | j                  |«       Y |S # |$ r Y Y |S w xY ww xY w)z�The container's client, created privately when missing (another request creating it first is fine). An
    existing container's access is never changed here.r   )ÚResourceExistsErrorÚResourceNotFoundError)Úazure.core.exceptionsr:   r;   Úget_container_clientÚget_container_propertiesÚcreate_container)ÚserviceÚ	containerr:   r;   Úclients        r   Úrequire_accessrC   B   st   € ÷ QØ×)Ñ)¨)Ó4€FðØ×'Ñ'Ô)ð €Møð !ò ð	Ø×$Ñ$ YÕ/ð €Møð #ò 	ÙØ€Mð	úðús&   ›- ­A³AÁAÁAÁAÁAc                ó*   — d| › d|› dt        |«      › �S )zSHow a product records a file: its container, its key and its tenant, in one string.úazure://ú/z#tenant=)r   )rA   Úkeyr   s      r   Ústored_locationrH   Q   s!   € à�i�[  #  h¬{¸9Ó/EÐ.FÐGÐGr   c                óº   — t        | xs d«      } | j                  d«      r8| t        d«      d j                  dd«      d   j	                  d«      \  }}}||fS || fS )zh(container, key) of a recorded file: a container-qualified location, or a legacy path in the shared one.r   rE   Nú#é   r   rF   )r   r   r(   ÚsplitÚ	partition)ÚstoredÚlegacy_containerrA   r"   rG   s        r   Úattachment_locationrP   V   si   € ä�’˜2Ó€FØ×Ñ˜Ô$Ø"¤3 z£?Ð#3Ð4×:Ñ:¸3ÀÓBÀ1ÑE×OÑOÐPSÓTÑˆ	�1�cØ˜#ˆ~ÐØ˜VÐ#Ð#r   c                óô   — t        | xs d«      } | j                  d«      r+t        j                  d| «      }|r|j	                  d«      S dS 	 t        | j                  dd«      d   «      S # t        $ r Y yw xY w)ziThe tenant a recorded file belongs to: written into a new location, or the first folder of a legacy path.r   rE   z#tenant=([0-9a-f-]{36})$rK   NrF   r   )r   r   r%   ÚsearchÚgroupr   rL   r   )rN   Úfounds     r   Úattachment_tenantrU   _   sv   € ä�’˜2Ó€FØ×Ñ˜Ô$Ü—	‘	Ð5°vÓ>ˆÙ!&ˆu�{‰{˜1‹~Ð0¨DÐ0ðÜ˜6Ÿ<™<¨¨QÓ/°Ñ2Ó3Ð3øÜò Ùðús   ÁA+ Á+	A7Á6A7c                ó<  — t        | xs d«      j                  d«      }| r7| j                  d«      s&d| v s"t        d„ |D «       «      s|d   t	        |«      vrt        | ›d�«      ‚| j                  dd«      d	   j                  «       t        vrt        | ›d
�«      ‚y)z‘Only images and videos of the shared categories, by a plain path: never a chat original, a document, or a
    path that climbs out of its folder.r   rF   ú\c              3  ó$   K  — | ]  }|d v –— Œ
 y­w))r   ú.z..N© )Ú.0Úps     r   ú	<genexpr>z"check_shareable.<locals>.<genexpr>o   s   è ø€ Ò<aÐVW¸QÀ/Ô=QÑ<aùs   ‚r   z! is not a file that may be sharedrY   rK   éÿÿÿÿz is not an image or a videoN)	r   rL   r   ÚanyÚtupler   Úrsplitr   ÚSHARED_EXTENSIONS)rG   Ú
categoriesÚpartss      r   Úcheck_shareablere   k   sœ   € ô �’	�r‹N× Ñ  Ó%€EÙ�3—>‘> #Ô&¨$°#©+¼Ñ<aÐ[`Ô<aÔ9aØ�Q‰xœu ZÓ0Ñ0Ü˜C˜7Ð"CÐDÓEÐEØ
‡z�z�#�qÓ˜"Ñ×#Ñ#Ó%Ô->Ñ>Ü˜C˜7Ð"=Ð>Ó?Ð?ð ?r   c                óh   — t        j                  | «      j                  d«      j                  d«      S )NÚasciiú=)Úbase64Úurlsafe_b64encodeÚdecodeÚrstrip)Údatas    r   Ú_b64rn   v   s)   € Ü×#Ñ# DÓ)×0Ñ0°Ó9×@Ñ@ÀÓEÐEr   c                ón  — t        ||«       t        | xs d«      j                  d«      }|r|j                  d«      rt	        d«      ‚t        t        |xs d«      j                  «       «      dk  rt	        d«      ‚t        t        j                  t        |«      |dœd¬	«      j                  «       «      }t        t        j                  |j                  «       |j                  «       t        j                  «      j                  «       «      }|j!                  d
«      rt"        nt$        }|› |› |› d|› �S )zÒThe permanent link Node serves this file at: base64url JSON {tenantId, key}, a dot, base64url HMAC-SHA256 of
    that text with the shared key. Anyone holding it can view the file; nothing about Azure is in it.r   rF   z/apiz5the tenant asset base URL is not set, or ends in /apié    z@the tenant asset signing key is not set or shorter than 32 bytes)ÚtenantIdrG   )ú,ú:)Ú
separatorszmy-templates/rY   )re   r   rl   Úendswithr   r(   Úencodern   ÚjsonÚdumpsr   ÚhmacÚnewÚhashlibÚsha256Údigestr   ÚTEMPLATE_LINK_PATHÚ	LINK_PATH)	Úbase_urlÚsecretr   rG   rc   ÚbaseÚpayloadÚ	signatureÚpaths	            r   Úsign_asset_urlr†   z   sò   € ô �C˜Ô$ÜˆxŠ~˜2Ó×%Ñ% cÓ*€DÙ�4—=‘= Ô(ÜÐPÓQÐQÜ
Œ3ˆvŠ|˜Ó×#Ñ#Ó%Ó&¨Ò+ÜÐ[Ó\Ð\Ü”4—:‘:¬;°yÓ+AÈ#ÑNÐ[eÔf×mÑmÓoÓp€GÜ”T—X‘X˜fŸm™m›o¨w¯~©~Ó/?ÄÇÁÓP×WÑWÓYÓZ€IØ!$§¡°Ô!@ÕÄi€DØˆV�D�6˜'˜ ! I ;Ð/Ð/r   c           	     ó”  — t        j                  dt        j                  t        «      › dt        j                  t        «      › d�dz   t        | xs d«      «      }|sy|j                  d«      }	 t        j                  t        j                  |dt        |«       d	z  z  z   «      «      j                  d
«      S # t        $ r Y yw xY w)zYThe file a signed link points at, read back from its payload; None for any other address.z(?:ú|ú)z ([A-Za-z0-9_-]+)\.[A-Za-z0-9_-]+r   NrK   rh   r   rG   )r%   rR   Úescaper   r~   r   rS   rw   Úloadsri   Úurlsafe_b64decoder(   Úgetr   )ÚurlrT   rƒ   s      r   Úkey_of_asset_urlr�   Š   s®   € ä�I‰I˜œBŸI™I¤iÓ0Ð1°´2·9±9Ô=OÓ3PÐ2QÐQRÐSÐVyÑyÜ˜#š) “nó&€EáØØ�k‰k˜!‹n€GðÜ�z‰zœ&×2Ñ2°7¸SÄSÈÃ\ÀMÐTUÑDUÑ=VÑ3VÓWÓX×\Ñ\Ð]bÓcÐcøÜò Ùðús   Á1A	B; Â;	CÃC)r   r   Úreturnr   )r)   r   r�   r   )rA   r   )rA   r   rG   r   r   r   r�   r   )rN   r   rO   r   r�   zTuple[str, str])rN   r   r�   úOptional[str])rG   r   rc   úIterable[str]r�   ÚNone)rm   Úbytesr�   r   )r€   r   r�   r   r   r   rG   r   rc   r’   r�   r   )rŽ   r   r�   r‘   )Ú__doc__Ú
__future__r   ri   r{   ry   rw   r%   Útypingr   r   r   Úcompiler   ÚSHARED_CATEGORIESrb   r   r~   r   r+   r8   rC   rH   rP   rU   re   rn   r†   r�   rZ   r   r   ú<module>rš      s¥   ðñ	õ #ã Û Û Û Û 	ß ,Ñ ,à€r‡z�zÐSÓT€ð AÐ Ø@Ð Ø(€	à3Ð óóó"ó óHó
$ó	ð ;Lô @óFð
 0Að0Ø,ð0ØEHó0ô 
r   