
    ]j*                        d dl Z d dlZd dlmZ d dlmZ d dlmZ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mZmZmZmZ 	 d dlmZ d dlmZmZ d dlmZ dZ! G d de
      Z"e G d de             Z#y# e $ r	  ed      w xY w)    N)	timedelta)SpooledTemporaryFile)ImproperlyConfiguredSuspiciousOperation)File)timezone)deconstructible)force_bytes)BaseStorage)check_location
clean_nameget_available_overwrite_name	safe_joinsetting)NotFound)BlobClient)_quotezfCould not load Google Cloud Storage bindings.
See https://github.com/GoogleCloudPlatform/gcloud-pythoncontent_typec                   d     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                 F   || _         t        j                  |      d   | _        || _        || _        |j                  j                  |      | _        | j                  s5d|v r1t        | j                   |j                  |j                        | _        d | _        d| _        y )Nr   w)
chunk_sizeF)name	mimetypes
guess_type	mime_type_mode_storagebucketget_blobblobr   blob_chunk_size_file	_is_dirty)selfr   modestorages       B/root/env/lib/python3.12/site-packages/storages/backends/gcloud.py__init__zGoogleCloudFile.__init__   s    	"--d3A6
NN++D1	yySD[		7>>"224DI 
    c                 .    | j                   j                  S N)r#   sizer'   s    r*   r/   zGoogleCloudFile.size,   s    yy~~r,   c                 <   | j                   t        | j                  j                  dt	        d            | _         d| j
                  v rGd| _        | j                  j                  | j                          | j                   j                  d       | j                   S )Nz.GSStorageFileFILE_UPLOAD_TEMP_DIR)max_sizesuffixdirrFr   )
r%   r   r    max_memory_sizer   r   r&   r#   download_to_fileseekr0   s    r*   	_get_filezGoogleCloudFile._get_file0   sv    ::-66'23DJ
 djj !&		**4::6

"zzr,   c                     || _         y r.   )r%   )r'   values     r*   	_set_filezGoogleCloudFile._set_file=   s	    
r,   c                 \    d| j                   vrt        d      |d}t        |   |      S )Nr6   z!File was not opened in read mode.)r   AttributeErrorsuperread)r'   	num_bytes	__class__s     r*   rB   zGoogleCloudFile.readB   s5    djj  !DEEIw|I&&r,   c                 t    d| j                   vrt        d      d| _        t        |   t        |            S )Nr   z"File was not opened in write mode.T)r   r@   r&   rA   writer
   )r'   contentrD   s     r*   rF   zGoogleCloudFile.writeK   s6    djj  !EFFw}[122r,   c           	      p   | j                   | j                  r|| j                  j                  | j                        }| j
                  j                  | j                  d| j                  |j                  d| j                  j                               | j                   j                          d | _         y y )NTacl)rewindr   predefined_acl)r%   r&   r    get_object_parametersr   r#   upload_from_filefiler   getdefault_aclclose)r'   blob_paramss     r*   rQ   zGoogleCloudFile.closeQ   s    ::!~~"mmAA$))L		**IId#.??5$--:S:S#T + V JJDJ "r,   r.   )__name__
__module____qualname__r+   propertyr/   r:   r=   rN   rB   rF   rQ   __classcell__rD   s   @r*   r   r      sC       Iy)D'3r,   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 Zd fd	Z xZS )GoogleCloudStoragec                 V    t        |   di | t        |        d | _        d | _        y )N )rA   r+   r   _bucket_client)r'   settingsrD   s     r*   r+   zGoogleCloudStorage.__init__^   s)    $8$tr,   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      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_FILE_OVERWRITEGS_CACHE_CONTROLGS_OBJECT_PARAMETERSGS_MAX_MEMORY_SIZEr   GS_BLOB_CHUNK_SIZE)
project_idcredentialsbucket_namecustom_endpointlocationrP   querystring_auth
expirationfile_overwritecache_controlobject_parametersr7   r$   )r   r   r0   s    r*   get_default_settingsz'GoogleCloudStorage.get_default_settingsf   s    !/2"#34"#34&';TBr2"#34 '(=t D!/9U3KL%&94@$%78!()?!D  '';Q?&';<!
 	
r,   c                 ~    | j                   &t        | j                  | j                        | _         | j                   S )N)projectrq   )r^   r   rp   rq   r0   s    r*   clientzGoogleCloudStorage.clientz   s4    <<! ,,DL ||r,   c                     | j                   *| j                  j                  | j                        | _         | j                   S r.   )r]   r}   r!   rr   r0   s    r*   r!   zGoogleCloudStorage.bucket   s2    <<;;--d.>.>?DL||r,   c                 f    	 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   rt   
ValueErrorr   r'   r   s     r*   _normalize_namez"GoogleCloudStorage._normalize_name   s?    	,T]]D11 	,%&H&*'+ , ,	,s    0c                     | j                  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   sE    ##Jt$45%dD$7#$=$DEEr,   c                    t        |      }| j                  |      }||_        t        |d|       }i }| j	                  |      }|j                  d| j                        |d<   t        |vr|j                  |t        <   |j                         D ]  \  }}t        |j                  ||         |j                  j                  |fd|j                  d| |S )NrwrI   rK   T)rJ   r/   )r   r   r   r   rL   poprP   CONTENT_TYPEr   itemssetattrr#   rM   r/   )	r'   r   rG   cleaned_namer   upload_paramsrR   propvals	            r*   _savezGoogleCloudStorage._save   s    !$'##L1#%dD$7006*5//%AQAQ*R&'{**5*?*?M,'$**, 	1ID#K$$dC0	1 	*))'c$W\\cUbcr,   c                     | j                   j                         }d|vr5| j                  r)t        j                  dt
               | j                  |d<   |S )zOverride this to return a dictionary of overwritable blob-property to value.

        Returns GS_OBJECT_PARAMETRS by default. See the docs for all possible options.
        rx   zThe GS_CACHE_CONTROL setting is deprecated. Use GS_OBJECT_PARAMETERS to set any writable blob property or override GoogleCloudStorage.get_object_parameters to vary the parameters per object.)ry   copyrx   warningswarnDeprecationWarning)r'   r   ry   s      r*   rL   z(GoogleCloudStorage.get_object_parameters   sW    
 !22779"338J8JMM23E
 261C1Co.  r,   c                     | j                  t        |            }	 | j                  j                  |       y # t        $ r Y y w xY wr.   )r   r   r!   delete_blobr   r   s     r*   deletezGoogleCloudStorage.delete   s@    ##Jt$45	KK##D) 		s   8 	AAc                     |s'	 | j                   j                  | j                         y| j	                  t        |            }t        | j                  j                  |            S # t        $ r Y yw xY w)NTF)r}   
get_bucketr!   r   r   r   boolr"   r   s     r*   existszGoogleCloudStorage.exists   sh    &&t{{3 ##Jt$45DKK((.//	  s   %A( (	A43A4c                    | j                  t        |            }|r|j                  d      s|dz  }| j                  j	                  |d      }t        |      }|j                  }g }g }|D ]1  }|j                  j                  d      }|j                  |d          3 |D ]'  }	|	j                  d      }|j                  |d          ) t        |      |fS )N/)prefix	delimiterr?   )
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5 c*CKD;;)))EX$$ 	$DIIOOC(ELLr#	$ $ 	#K%%c*EKKb	"	# Dz5  r,   c                 t    | j                   j                  |      }|t        dj                  |            |S )NzFile does not exist: {})r!   r"   r   formatr'   r   r#   s      r*   	_get_blobzGoogleCloudStorage._get_blob   s7    {{##D)<4;;DABBr,   c                 p    | j                  t        |            }| j                  |      }|j                  S r.   )r   r   r   r/   r   s      r*   r/   zGoogleCloudStorage.size   s/    ##Jt$45~~d#yyr,   c                     | j                  t        |            }| j                  |      }t        j                  |j
                        S r.   )r   r   r   r   
make_naiveupdatedr   s      r*   modified_timez GoogleCloudStorage.modified_time   s:    ##Jt$45~~d#""4<<00r,   c                     | j                  t        |            }| j                  |      }|j                  }t	        d      r|S t        j                  |      S )NUSE_TZ)r   r   r   r   r   r   r   )r'   r   r#   r   s       r*   get_modified_timez$GoogleCloudStorage.get_modified_time   sK    ##Jt$45~~d#,,!(+wM1D1DW1MMr,   c                     | j                  t        |            }| j                  |      }|j                  }t	        d      r|S 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  sO    
 ##Jt$45~~d###!(+wM1D1DW1MMr,   c                    | j                  t        |            }| j                  j                  |      }| j	                  |      }|j                  d| j                        dk(  xs | j                   }| j                  s|r|j                  S |r(dj                  | j                  t        |d            S | j                  s|j                  | j                  d      S |j                  | j                  | j                  d	      S )
z
        Return public url or a signed url for the Blob.
        This DOES NOT check for existance of Blob - that makes codes too slow
        for many use cases.
        rI   
publicReadz {storage_base_url}/{quoted_name}s   /~)safe)storage_base_urlquoted_namev4)rv   version)bucket_bound_hostnamerv   r   )r   r   r!   r#   rL   rO   rP   ru   rs   
public_urlr   r   generate_signed_urlrv   )r'   r   r#   rR   no_signed_urls        r*   urlzGoogleCloudStorage.url  s    ##Jt$45{{%006OOE4#3#34DaDLaLaHa 	 ##??"5<<!%!5!5"4e4 =   %%++??D ,   ++&*&:&:?? ,  r,   c                 j    t        |      }| j                  rt        ||      S t        |   ||      S r.   )r   rw   r   rA   get_available_name)r'   r   
max_lengthrD   s      r*   r   z%GoogleCloudStorage.get_available_name,  s5    $/jAAw)$
;;r,   )rbr.   )rS   rT   rU   r+   rz   rV   r}   r!   r   r   r   rL   r   r   r   r   r/   r   r   r   r   r   rW   rX   s   @r*   rZ   rZ   \   s    
(    
,(!"	0!.
1
NN<< <r,   rZ   )$r   r   datetimer   tempfiler   django.core.exceptionsr   r   django.core.files.baser   django.utilsr   django.utils.deconstructr	   django.utils.encodingr
   storages.baser   storages.utilsr   r   r   r   r   google.cloud.exceptionsr   google.cloud.storager   r   google.cloud.storage.blobr   ImportErrorr   r   rZ   r\   r,   r*   <module>r      s       ) L ' ! 4 - % 
[010 ;d ;| S< S< S<O  [
  Z [ [[s   
A< <B
