Ë
    §‘ˆjP  ã                   ó‚   — d dl Z d dlmZmZ d dlmZmZmZ d dlmZ  e j                  e
«      Z G d„ d«      Z e«       Zy)é    N)ÚdatetimeÚ	timedelta)ÚBlobSasPermissionsÚBlobServiceClientÚgenerate_blob_sas)Úsettingsc            	       ó^   — e Zd Zd„ Zd„ Zdedededefd„Zdededefd„Z	ddeded	e
defd
„Zy)ÚAzureStorageServicec                 óÒ   — 	 t        j                  t        j                  «      | _        d| _        y # t        $ r+}t        j                  d|› �d¬«       d | _        Y d }~y d }~ww xY w)Nzknowledge-basez0Failed to initialize Azure Blob Storage client: T©Úexc_info)	r   Úfrom_connection_stringr   ÚAZURE_STORAGE_CONNECTION_STRINGÚblob_service_clientÚcontainer_nameÚ	ExceptionÚloggerÚerror)ÚselfÚes     ú9/var/www/html/strategist-ai/app/services/infra/storage.pyÚ__init__zAzureStorageService.__init__   s]   € ð	,Ü'8×'OÑ'OÔPX×PxÑPxÓ'yˆDÔ$Ø"2ˆDÕøÜò 	,Ü�L‰LÐKÈAÈ3ÐOÐZ^ˆLÔ_Ø'+ˆD×$Ñ$ûð	,ús   ‚/2 ²	A&»!A!Á!A&c                 ó4  — | j                   sy	 | j                   j                  | j                  «      }|j                  «       s%| j                   j	                  | j                  «       y# t
        $ r$}t        j                  d|› �d¬«       Y d }~yd }~ww xY w)NFTz'Error ensuring Azure container exists: r   )r   Úget_container_clientr   ÚexistsÚcreate_containerr   r   r   )r   Úcontainer_clientr   s      r   Ú_ensure_containerz%AzureStorageService._ensure_container   s…   € Ø×'Ò'Øð	Ø#×7Ñ7×LÑLÈT×M`ÑM`ÓaÐØ#×*Ñ*Ô,Ø×(Ñ(×9Ñ9¸$×:MÑ:MÔNØøÜò 	Ü�L‰LÐBÀ1À#ÐFÐQUˆLÔVÜûð	ús   �AA* Á*	BÁ3BÂBÚ	tenant_idÚ	file_nameÚcontent_bytesÚreturnc                 óB  — | j                  «       sy	 d|› d|› �}| j                  j                  | j                  |¬«      }|j	                  |d¬«       t
        j                  d|› d|› �«       y# t        $ r$}t
        j                  d	|› �d¬
«       Y d}~yd}~ww xY w)zT
        Uploads a file to Azure Blob Storage under a tenant-specific path.
        Fútenant/ú/files/©Ú	containerÚblobT)Ú	overwritezSuccessfully uploaded z to Azure for tenant z Failed to upload file to Azure: r   N)	r   r   Úget_blob_clientr   Úupload_blobr   Úinfor   r   )r   r   r    r!   Ú	blob_pathÚblob_clientr   s          r   Úupload_filezAzureStorageService.upload_file   sª   € ð ×%Ñ%Ô'Øð
	à! ) ¨G°I°;Ð?ˆIØ×2Ñ2×BÑBÈT×M`ÑM`ÐgpÐBÓqˆKà×#Ñ# M¸TÐ#ÔBÜ�K‰KÐ0°°Ð;PÐQZÐP[Ð\Ô]ØøÜò 	Ü�L‰LÐ;¸A¸3Ð?È$ˆLÔOÜûð	ús   “AA1 Á1	BÁ:BÂBc                 óT  — | j                   sy	 d|› d|› �}| j                   j                  | j                  |¬«      }|j                  «       r+|j	                  «        t
        j                  d|› d|› �«       y# t        $ r$}t
        j                  d|› �d¬	«       Y d
}~yd
}~ww xY w)z9
        Deletes a file from Azure Blob Storage.
        Fr$   r%   r&   zSuccessfully deleted z from Azure for tenant Tz"Failed to delete file from Azure: r   N)	r   r*   r   r   Údelete_blobr   r,   r   r   )r   r   r    r-   r.   r   s         r   Údelete_filezAzureStorageService.delete_file2   s¬   € ð ×'Ò'Øð
	Ø! ) ¨G°I°;Ð?ˆIØ×2Ñ2×BÑBÈT×M`ÑM`ÐgpÐBÓqˆKà×!Ñ!Ô#Ø×'Ñ'Ô)Ü—‘Ð3°I°;Ð>UÐV_ÐU`ÐaÔbØøÜò 	Ü�L‰LÐ=¸a¸SÐAÈDˆLÔQÜûð	ús   �A*A: Á:	B'ÂB"Â"B'Úhoursc                 óB  — | j                   sy	 d|› d|› �}| j                   j                  | j                  |¬«      }|j                  «       st        j                  d|› �«       yt        | j                   j                  | j                  || j                   j                  j                  t        d¬«      t        j                  «       t        |¬«      z   ¬	«      }|j                  › d
|› �S # t        $ r'}t        j!                  d|› d|› �d¬«       Y d}~yd}~ww xY w)a  A temporary read-only link to a stored file, or '' if it cannot be made.

        The container is private, so visitors cannot be handed a plain blob URL.
        A SAS token grants read access to this one blob and nothing else, and
        stops working once it expires.
        Ú r$   r%   r&   zNo stored file at T)Úread)r3   )Úaccount_namer   Ú	blob_nameÚaccount_keyÚ
permissionÚexpiryú?z$Could not build a download link for z: r   N)r   r*   r   r   r   Úwarningr   r7   Ú
credentialr9   r   r   Úutcnowr   Úurlr   r   )r   r   r    r3   r-   r.   Útokenr   s           r   Údownload_urlz AzureStorageService.download_urlE   s  € ð ×'Ò'Øð	Ø! ) ¨G°I°;Ð?ˆIØ×2Ñ2×BÑBØ×-Ñ-°Ið Có ?ˆKà×%Ñ%Ô'Ü—‘Ð!3°I°;Ð?Ô@Øä%Ø!×5Ñ5×BÑBØ#×2Ñ2Ø#Ø ×4Ñ4×?Ñ?×KÑKÜ-°4Ô8Ü—‘Ó(¬9¸5Ô+AÑAôˆEð "—o‘oÐ& a¨ wÐ/Ð/øÜò 	Ü�L‰LÐ?À	¸{È"ÈQÈCÐPÐ[_ˆLÔ`Üûð	ús   �AC. Á'BC. Ã.	DÃ7DÄDN)é   )Ú__name__Ú
__module__Ú__qualname__r   r   ÚstrÚbytesÚboolr/   r2   ÚintrB   © ó    r   r
   r
   
   si   „ ò,ò
ð Sð °Sð Èð ÐSWó ð& Sð °Sð ¸Tó ñ& cð °cð À#ð Èsô rL   r
   )Úloggingr   r   Úazure.storage.blobr   r   r   Úapp.core.configr   Ú	getLoggerrD   r   r
   Ústorage_servicerK   rL   r   ú<module>rR      s=   ðÛ ß (ç WÑ Wå $à	ˆ×	Ñ	˜8Ó	$€÷Wñ Wñv &Ó'�rL   