
    	]jr                         d dl Z d dlmZmZmZ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 ddlmZmZ dd	lmZmZ dd
lmZmZ  e j2                  e      Zd Zd Zdeeeef   dee	   de	fdZy)    N)DictIterableListUnion)get_object_or_404)ImportStorage)PermissionDeniedValidationError   )AzureBlobExportStorageListAPIAzureBlobImportStorageListAPI)GCSExportStorageListAPIGCSImportStorageListAPI)RedisExportStorageListAPIRedisImportStorageListAPI)S3ExportStorageListAPIS3ImportStorageListAPIc                    |rt        |d      st        d      | j                  j                  d      }d}|r^t	        |j
                  j                  j                  j                         |      }|j                  | j                        s
t                || j                        }|j                  d       |r|j                  ||j                        }n& |j
                  j                  di |j                  }	 |j                          |S # t         $ r(}t"        j%                  d	|        t        d
      d}~ww xY w)aP  
    Preload and prepare a storage instance from request data.

    This function handles the common logic for loading existing storage instances
    or creating new ones from request data, including permission checks and
    serializer validation.

    Args:
        request: The HTTP request containing storage data
        serializer_class: The serializer class to use for validation

    Returns:
        The prepared storage instance

    Raises:
        PermissionDenied: If user doesn't have permission to access the storage
        ValidationError: If serializer validation fails
    Metaz#Invalid or missing serializer classidN)pk)dataT)raise_exceptionz%Error validating storage connection: z#Error validating storage connection )hasattrr
   r   getr   r   modelobjectsallhas_permissionuserr	   is_validupdatevalidated_datavalidate_connection	Exceptionloggererror)requestserializer_class
storage_idinstance
serializerexcs         L/root/env/lib/python3.12/site-packages/label_studio/io_storages/functions.pyvalidate_storage_instancer0      s*   & 7+;V#DCDD!!$'JH$%5%:%:%@%@%H%H%L%L%NS]^&&w||4"$$ "w||4J- $$Xz/H/HI.#((..K1J1JKE$$&
 O	  E<SEBCCDDEs   D 	E#E  Ec                  v    ddt         t        dddt        t        dddt        t
        ddd	t        t        dgS )
Ns3zAWS S3)nametitleimport_list_apiexport_list_apigcszGoogle Cloud StorageazurezMicrosoft AzureredisRedis)r   r   r   r   r   r   r   r   r       r/   get_storage_listr<   C   sX     55		
 +66		
 &<<		
 88		
' r;   urlstorage_objectsreturnc                     |D ](  }t        | t              s|j                  |       s&|c S  t        | t              st        | t              r|D ]  }|j                  |       s|c S  yy)zRFind the first compatible storage and returns storage that can emit pre-signed URLN)
isinstancestrcan_resolve_urldictlist)r=   r>   storage_objects      r/   get_storage_by_urlrG   `   sq     * " c3N$B$B3$G!!	" #t
3 5- 	&N--c2 &%		& !6r;   ) loggingtypingr   r   r   r   django.shortcutsr   io_storages.base_modelsr   rest_framework.exceptionsr	   r
   azure_blob.apir   r   gcs.apir   r   	redis.apir   r   s3.apir   r   	getLogger__name__r'   r0   r<   rB   rG   r   r;   r/   <module>rS      sk     . . . 1 G X E K B			8	$0f:&E#tT/2 &XmE\ &an &r;   