
    R}bg.                        d dl 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	 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 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 d dl m!Z! d dl"m#Z# n# e$$ r  ed          w xY wdZ%dZ& G d dee          Z'e G d de                      Z(dS )    N)	timedelta)SpooledTemporaryFile)ImproperlyConfigured)SuspiciousOperation)File)timezone)deconstructible)BaseStorage)CompressedFileMixin)check_location)
clean_name)get_available_overwrite_name)	safe_join)setting)to_bytes)NotFound)Blob)Client)_quote)DEFAULT_RETRYzfCould not load Google Cloud Storage bindings.
See https://github.com/GoogleCloudPlatform/gcloud-pythoncontent_encodingcontent_typec                   p     e Zd Zd Zed             Zd Zd Z eee          Zd	 fd	Z	 fdZ
d Z xZS )
GoogleCloudFilec                 <   || _         t          j        |          \  | _        | _        || _        || _        |j                            ||j	                  | _
        | j
        s*d|v r&t          | j         |j        |j	                  | _
        d | _        d| _        d S )N)
chunk_sizewF)name	mimetypes
guess_type	mime_typemime_encoding_mode_storagebucketget_blobblob_chunk_sizeblobr   _file	_is_dirty)selfr   modestorages       k/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/storages/backends/gcloud.py__init__zGoogleCloudFile.__init__(   s    	-6-A$-G-G**
N++DW=T+UU	y 	SD[[	7>g6M  DI 
    c                     | j         j        S N)r(   sizer+   s    r.   r3   zGoogleCloudFile.size5   s    y~r0   c                    | j         t          | j        j        dt	          d                    | _         d| j        v r@d| _        | j                            | j                    | j         	                    d           | j        j
        r6| j        j        dk    r&|                     | j        | j                   | _         | j         S )	Nz.GSStorageFileFILE_UPLOAD_TEMP_DIR)max_sizesuffixdirrFr   gzip)r,   file)r)   r   r$   max_memory_sizer   r#   r*   r(   download_to_fileseekr;   r   _decompress_filer4   s    r.   	_get_filezGoogleCloudFile._get_file9   s    :-6'233  DJ
 dj  !&	**4:666
"""}! Udi&@F&J&J!22
2TT
zr0   c                     || _         d S r2   )r)   )r+   values     r.   	_set_filezGoogleCloudFile._set_fileH   s    


r0   Nc                 ~    d| j         vrt          d          |d}t                                          |          S )Nr:   z!File was not opened in read mode.)r#   AttributeErrorsuperread)r+   	num_bytes	__class__s     r.   rI   zGoogleCloudFile.readM   s@    dj   !DEEEIww||I&&&r0   c                     d| j         vrt          d          d| _        t                                          t          |                    S )Nr   z"File was not opened in write mode.T)r#   rG   r*   rH   writer   )r+   contentrK   s     r.   rM   zGoogleCloudFile.writeV   sD    dj   !EFFFww}}Xg..///r0   c           
      <   | j         | j        rk| j                            | j                  }| j                            | j        d| j        t          |
                    d| j        j                             | j                                          d | _         d S d S )NTacl)rewindr   retrypredefined_acl)r)   r*   r$   get_object_parametersr   r(   upload_from_filer<   r!   r   getdefault_aclclose)r+   blob_paramss     r.   rX   zGoogleCloudFile.close\   s    :!~ "mAA$)LL	**I!%'#.??5$-:S#T#T +    JDJJJ "!r0   r2   )__name__
__module____qualname__r/   propertyr3   rA   rD   r<   rI   rM   rX   __classcell__rK   s   @r.   r   r   '   s             X     8Iy))D' ' ' ' ' '0 0 0 0 0      r0   r   c                        e Zd Z fdZd Zed             Zed             Zd ZddZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZddZd fd	Z xZS )GoogleCloudStoragec                 t     t                      j        di | t          |            d | _        d | _        d S )N )rH   r/   r   _bucket_client)r+   settingsrK   s     r.   r/   zGoogleCloudStorage.__init__m   s@    $$8$$$tr0   c                    t          d          t          d          t          d          t          dd           t          dd          t          d          t          dd	          t          d
t          d                    t          dd          t          dd          t          dd	          t          di           t          dd          t          d          dS )NGS_PROJECT_IDGS_CREDENTIALSGS_BUCKET_NAMEGS_CUSTOM_ENDPOINTGS_LOCATION GS_DEFAULT_ACLGS_QUERYSTRING_AUTHTGS_EXPIRATIONiQ )secondsGS_IS_GZIPPEDFGZIP_CONTENT_TYPES)ztext/cssztext/javascriptzapplication/javascriptzapplication/x-javascriptzimage/svg+xmlGS_FILE_OVERWRITEGS_OBJECT_PARAMETERSGS_MAX_MEMORY_SIZEr   GS_BLOB_CHUNK_SIZE)
project_idcredentialsbucket_namecustom_endpointlocationrW   querystring_auth
expirationr;   gzip_content_typesfile_overwriteobject_parametersr=   r'   )r   r   r4   s    r.   get_default_settingsz'GoogleCloudStorage.get_default_settingsu   s    !/22"#344"#344&';TBBr22"#344 '(=t D D!/9U3K3K3KLLOU33")$	# 	# &&94@@!()?!D!D  '';Q??&';<<5
 
 	
r0   c                 ^    | j          t          | j        | j                  | _         | j         S )N)projectry   )re   r   rx   ry   r4   s    r.   clientzGoogleCloudStorage.client   s+    <!$/tGWXXXDL|r0   c                 f    | j         $| j                            | j                  | _         | j         S r2   )rd   r   r%   rz   r4   s    r.   r%   zGoogleCloudStorage.bucket   s,    <;--d.>??DL|r0   c                 n    	 t          | j        |          S # t          $ r t          d|z            w xY w)aA  
        Normalizes the name so that paths like /path/to/ignored/../something.txt
        and ./file.txt work.  Note that clean_name adds ./ to some paths so
        they need to be fixed here. We check to make sure that the path pointed
        to is not outside the directory specified by the LOCATION setting.
        z Attempted access to '%s' denied.)r   r|   
ValueErrorr   r+   r   s     r.   _normalize_namez"GoogleCloudStorage._normalize_name   sM    	QT]D111 	Q 	Q 	Q%&H4&OPPP	Qs    4rbc                     |                      t          |                    }t          |||           }|j        st	          d|z            |S )NzFile does not exist: %s)r   r   r   r(   FileNotFoundError)r+   r   r,   file_objects       r.   _openzGoogleCloudStorage._open   sT    ##Jt$4$455%dD$77 	F#$=$DEEEr0   c                 F   |                     d           t          j                    }t          j        d|d          5 }|                    t          |                                                     d d d            n# 1 swxY w Y   |                     d           |S )Nr   wbg        )r,   fileobjmtime)r?   ioBytesIOr;   GzipFilerM   r   rI   )r+   rN   zbufzfiles       r.   _compress_contentz$GoogleCloudStorage._compress_content   s    Qz||]d#>>> 	2%KK00111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2		!s    5BBBc           
         t          |          }|                     |          }||_        t          |d|           }|                     |          }|j        rt          |vr|j        |t          <   i }|                    d| j                  |d<   |                    t          |j
                  |t          <   | j        r<|t                   | j        v r(t          |vr|                     |          }d|t          <   |                                D ]\  }}t          |j        ||            |j        j        |fdt$          t'          |dd           d| |S )NrwrP   rS   r;   Tr3   )rQ   rR   r3   )r   r   r   r   rT   r"   CONTENT_ENCODINGpoprW   CONTENT_TYPEr!   r;   r   r   itemssetattrr(   rU   r   getattr)	r+   r   rN   cleaned_namer   rY   upload_paramspropvals	            r.   _savezGoogleCloudStorage._save   s   !$''##L11#%dD$770066$ 	F)9)L)L,7,EK()*5//%AQ*R*R&'&1oo+/'
 '
l#
 I	3l+t/FFF 33,,W55G,2K()$**,, 	1 	1ID#K$dC0000))	
&$//		
 	

 	
 	
 	
 r0   c                 8    | j                                         }|S )zOverride this to return a dictionary of overwritable blob-property to value.

        Returns GS_OBJECT_PARAMETERS by default. See the docs for all possible options.
        )r   copy)r+   r   r   s      r.   rT   z(GoogleCloudStorage.get_object_parameters   s    
 !27799  r0   c                     |                      t          |                    }	 | j                            |t                     d S # t
          $ r Y d S w xY w)N)rR   )r   r   r%   delete_blobr   r   r   s     r.   deletezGoogleCloudStorage.delete   se    ##Jt$4$455	K##D#>>>>> 	 	 	DD	s   !A 
AAc                     |s3	 | j                             | j                   dS # t          $ r Y dS w xY w|                     t          |                    }t          | j                            |                    S )NTF)r   
get_bucketr%   r   r   r   boolr&   r   s     r.   existszGoogleCloudStorage.exists   s     	&&t{333t   uu ##Jt$4$455DK((..///s   % 
33c                    |                      t          |                    }|r|                    d          s|dz  }| j                            |d          }t          |          }|j        }g }g }|D ]7}|j                            d          }|	                    |d                    8|D ]2}	|	                    d          }|	                    |d                    3t          |          |fS )N/)prefix	delimiterrF   )
r   r   endswithr%   
list_blobslistprefixesr   splitappend)
r+   r   iteratorblobsr   filesdirsr(   partsfolder_paths
             r.   listdirzGoogleCloudStorage.listdir   s   ##Jt$4$455  	c** 	CKD;)))EEX$ 	$ 	$DIOOC((ELLr##### 	# 	#K%%c**EKKb	""""Dzz5  r0   c                     | j                             |          }|"t          d                    |                    |S )NzFile does not exist: {})r%   r&   r   formatr+   r   r(   s      r.   	_get_blobzGoogleCloudStorage._get_blob  s>    {##D))<4;;DAABBBr0   c                 ~    |                      t          |                    }|                     |          }|j        S r2   )r   r   r   r3   r   s      r.   r3   zGoogleCloudStorage.size  s6    ##Jt$4$455~~d##yr0   c                     |                      t          |                    }|                     |          }|j        }t	          d          r|nt          j        |          S )NUSE_TZ)r   r   r   updatedr   r   
make_naive)r+   r   r(   r   s       r.   get_modified_timez$GoogleCloudStorage.get_modified_time  sW    ##Jt$4$455~~d##,!(++Mww1DW1M1MMr0   c                     |                      t          |                    }|                     |          }|j        }t	          d          r|nt          j        |          S )z
        Return the creation time (as a datetime) of the file specified by name.
        The datetime will be timezone-aware if USE_TZ=True.
        r   )r   r   r   time_createdr   r   r   )r+   r   r(   createds       r.   get_created_timez#GoogleCloudStorage.get_created_time$  sZ    
 ##Jt$4$455~~d###!(++Mww1DW1M1MMr0   Nc                    |                      t          |                    }| j                            |          }|                     |          }|                    d| j                  dk    p| j         }| j        s	|r|j	        S |r+d
                    | j        t          |d                    S | j        | j        dd}|pi }|                                D ]\  }}	|	r	||vr|	||<    |j        d
i |S )z
        Return public URL or a signed URL for the Blob.

        The existnce of blobs are not verified for public URLs, it makes the code too
        slow.
        rP   
publicReadz {storage_base_url}/{quoted_name}s   /~)safe)storage_base_urlquoted_namev4)bucket_bound_hostnamer~   versionNrc   )r   r   r%   r(   rT   rV   rW   r}   r{   
public_urlr   r   r~   r   generate_signed_url)
r+   r   
parametersr(   rY   no_signed_urldefault_paramsparamskeyrC   s
             r.   urlzGoogleCloudStorage.url.  sF    ##Jt$4$455{%%0066OOE4#344D )(( 	
 # 	6 	6?" 	65<<!%!5"4e444 =    *.)="o N
  %2F,2244 ( (
U (S.."'F3K+4+55f555r0   c                     t          |          }| j        rt          ||          S t                                          ||          S r2   )r   r   r   rH   get_available_name)r+   r   
max_lengthrK   s      r.   r   z%GoogleCloudStorage.get_available_nameR  sF    $ 	B/jAAAww))$
;;;r0   )r   r2   )rZ   r[   r\   r/   r   r]   r   r%   r   r   r   r   rT   r   r   r   r   r3   r   r   r   r   r^   r_   s   @r.   ra   ra   k   ss           
 
 
<   X
   X

Q 
Q 
Q     # # #J! ! !  	0 	0 	0! ! !.    
N N NN N N"6 "6 "6 "6H< < < < < < < < < <r0   ra   ))r;   r   r   datetimer   tempfiler   django.core.exceptionsr   r   django.core.files.baser   django.utilsr   django.utils.deconstructr	   storages.baser
   storages.compressr   storages.utilsr   r   r   r   r   r   google.cloud.exceptionsr   google.cloud.storager   r   google.cloud.storage.blobr   google.cloud.storage.retryr   ImportErrorr   r   r   ra   rc   r0   r.   <module>r      sV    				           ) ) ) ) ) ) 7 7 7 7 7 7 6 6 6 6 6 6 ' ' ' ' ' ' ! ! ! ! ! ! 4 4 4 4 4 4 % % % % % % 1 1 1 1 1 1 ) ) ) ) ) ) % % % % % % 7 7 7 7 7 7 $ $ $ $ $ $ " " " " " " # # # # # #
000000))))))++++++0000008888888   

	C   & A A A A A)4 A A AH j< j< j< j< j< j< j< j< j< j<s   (B B