o
    {l~i6                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ zd d	lmZ d d
lmZ d dlmZ W n eyM   edw eG dd de
ZG dd deZdS )    N)urljoin)settings)ImproperlyConfigured)File)Storage)deconstructible)
clean_name)
get_driver)ObjectDoesNotExistError)ProviderzCould not load libcloudc                   @   sn   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdddZ	dd Z
dd ZdddZdd Zdd ZdS )LibCloudStoragezXDjango storage derived class using apache libcloud to operate
    on supported providersNc           	   
   C   s  |d u r
t tdd}tj|| _| jstd| i }d| jv r(| jd |d< d| jv r4| jd |d< z5| jd }t|trT|dd\}}|d	krOt	d
t t
|}t|}|| jd | jd fi || _W n ty } ztd| jd|f d }~ww | jd | _d S )NDEFAULT_LIBCLOUD_PROVIDERdefaultz,LIBCLOUD_PROVIDERS %s not defined or invalidregionprojecttype.   zlibcloud.storage.types.ProviderzInvalid module pathuserkeyz,Unable to create libcloud driver type %s: %sbucket)getattrr   LIBCLOUD_PROVIDERSgetproviderr   
isinstancestrrsplit
ValueErrorr   r	   driver	Exceptionr   )	selfprovider_nameoptionextra_kwargsprovider_typemodule_pathtagDrivere r*   h/var/www/newdalilibackend/backend/venv/lib/python3.10/site-packages/storages/backends/apache_libcloud.py__init__   sD   




zLibCloudStorage.__init__c                 C   s   | j | jS )z0Helper to get bucket object (libcloud container))r   get_containerr   r!   r*   r*   r+   _get_bucket>   s   zLibCloudStorage._get_bucketc                 C   s   | j | jt|S )zZGet object by its name. ObjectDoesNotExistError will be raised if object not
        found)r   
get_objectr   r   )r!   namer*   r*   r+   _get_objectB   s   zLibCloudStorage._get_objectc                 C   s.   z|  |}| j|W S  ty   Y dS w )zDelete object on remoteN)r2   r   delete_objectr
   r!   r1   objr*   r*   r+   deleteG   s   
zLibCloudStorage.deletec                 C   s&   z|  |}W dS  ty   Y dS w )NFT)r2   r
   )r!   r1   _r*   r*   r+   existsO   s   zLibCloudStorage.exists/c           	      C   s  |   }| j|}t|}|dsd| }g }g }|D ]l}|dkrO|jddkr2||j q|jddkrN|jd|jd }||vrN|| q|j	|r|jd|dkr|jdr{|jdd }|t
|d }|| q|jt
|d }|| q||fS )zLists the contents of the specified path,
        returning a 2-tuple of lists; the first item being
        directories, the second item being files.
        r9   z%s/r   r   Nz	_$folder$i)r/   r   list_container_objectsr   endswithr1   countappendindex
startswithlen)	r!   path	containerobjectsfilesdirsodir_namer1   r*   r*   r+   listdirV   s4   


zLibCloudStorage.listdirc                 C   s   |  |}|r
|jS dS )N)r2   sizer4   r*   r*   r+   rJ   x   s   
zLibCloudStorage.sizec              
   C   s  | j d  }| |}|sd S z	| j|}W |S  ty } z\d| j|j}d|v r:d| jj	j
 }t||}n'd|v rDtd|}n$d|v rUd| j d	  }t||}nd
|v r_td|}n|W Y d }~|S W Y d }~|S W Y d }~|S W Y d }~|S d }~ww )Nr   z{}/{}s3z
https://%sgooglezhttps://storage.googleapis.comazurez https://%s.blob.core.windows.netr   	backblazezapi.backblaze.com/b2api/v1/)r   lowerr2   r   get_object_cdn_urlNotImplementedErrorformatr   r1   
connectionhostr   )r!   r1   r%   r5   urlr)   object_pathbase_urlr*   r*   r+   rU   |   s<   





zLibCloudStorage.urlrbc                 C   s   t || |d}|S )N)mode)LibCloudFile)r!   r1   rY   remote_filer*   r*   r+   _open   s   zLibCloudStorage._openc              
   C   sH   z|  |}W n ty } ztt|d }~ww t| j||jS N)r2   r
   FileNotFoundErrorr   nextr   download_object_as_streamrJ   )r!   r1   r5   r)   r*   r*   r+   _read   s   zLibCloudStorage._readc                 C   s   | j t||  | |S r]   )r   upload_object_via_streamiterr/   )r!   r1   filer*   r*   r+   _save   s   zLibCloudStorage._save)NN)r9   )rX   )__name__
__module____qualname____doc__r,   r/   r2   r6   r8   rH   rJ   rU   r\   ra   re   r*   r*   r*   r+   r      s    
"
"
r   c                   @   sX   e Zd ZdZdd Zdd Zdd ZeeeZedd	 Z	dddZ
dd Zdd Zd
S )rZ   z@File inherited class for libcloud storage objects read and writec                 C   s"   || _ || _|| _d| _d | _d S )NF)r1   _storage_mode	_is_dirty_file)r!   r1   storagerY   r*   r*   r+   r,      s
   
zLibCloudFile.__init__c                 C   s*   | j d u r| j| j}t|| _ | j S r]   )rm   rj   ra   r1   ioBytesIO)r!   datar*   r*   r+   	_get_file   s   
zLibCloudFile._get_filec                 C   s
   || _ d S r]   )rm   )r!   valuer*   r*   r+   	_set_file   s   
zLibCloudFile._set_filec                 C   s    t | ds| j| j| _| jS )N_size)hasattrrj   rJ   r1   ru   r.   r*   r*   r+   rJ      s   
zLibCloudFile.sizeNc                 C   s   | j |S r]   )rd   read)r!   	num_bytesr*   r*   r+   rw      s   zLibCloudFile.readc                 C   s(   d| j vr	tdt|| _d| _d S )Nwz%File was opened for read-only access.T)rk   AttributeErrorro   rp   rd   rl   )r!   contentr*   r*   r+   write   s   

zLibCloudFile.writec                 C   s&   | j r| j| j| j | j  d S r]   )rl   rj   re   r1   rd   closer.   r*   r*   r+   r}      s   zLibCloudFile.closer]   )rf   rg   rh   ri   r,   rr   rt   propertyrd   rJ   rw   r|   r}   r*   r*   r*   r+   rZ      s    


rZ   )ro   urllib.parser   django.confr   django.core.exceptionsr   django.core.files.baser   django.core.files.storager   django.utils.deconstructr   storages.utilsr   libcloud.storage.providersr	   libcloud.storage.typesr
   r   ImportErrorr   rZ   r*   r*   r*   r+   <module>   s&    