
    h                         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mZ d Ze G d de
             Zy)    N)urljoin)ObjectId)	InvalidId)ImproperlyConfigured)Storage)deconstructible)filepath_to_uri)connections)GridFSNoFilec              #      K   | j                   j                         D ]F  }|d|j                  d       }|| j                  k7  s'|j	                  | j                        sC| H yw)z6
    Returns all sub-collections of `collection`.
    N.)databasecollection_namesrfindname
startswith)
collectionr   cleaneds      Q/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/djongo/storage.py_get_subcollectionsr      s]     
 ##446 '

3(joo%'*<*<Z__*MMs   AA'A' A'c                   l    e Zd ZdZ	 	 d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 Zy)GridFSStoragea  
    GridFS Storage backend for Django.
    Based on https://github.com/django-nonrel/mongodb-engine/blob/master/django_mongodb_engine/storage.py

    This backend aims to add a GridFS storage to upload files to
    using Django's file fields.

    For performance, the file hierarchy is represented as a tree of
    MongoDB sub-collections.

    (One could use a flat list, but to list a directory '/this/path/'
    we would have to execute a search over the whole collection and
    then filter the results to exclude those not starting by
    '/this/path' using that model.)

    :param location:
       (optional) Name of the top-level node that holds the files. This
       value of `location` is prepended to all file paths, so it works
       like the `location` setting for Django's built-in
       :class:`~django.core.files.storage.FileSystemStorage`.
    :param collection:
        Name of the collection the file tree shall be stored in.
        Defaults to 'storage'.
    :param database:
        Alias of the Django database to use. Defaults to 'default' (the
        default Django database).
    :param base_url:
        URL that serves the files in GridFS (for instance, through
        nginx-gridfs).
        Defaults to None (file not accessible through a URL).
    Nc                 
   |j                  t        j                        | _        || _        || _        || _        | j                  st        d      | j                  r'| j                  j                  d      st        d      y y )Nz'collection' may not be empty./z)If set, 'base_url' must end with a slash.)	striposseplocationr   r   base_urlr   endswith)selfr   r   r   r    s        r   __init__zGridFSStorage.__init__<   so     rvv.$  &'GHH==!7!7!<& (0 1 1 "==    c                     | j                  |      \  }}	 |j                  |      S # t        $ r d|v r|j                  |      cY S  w xY w)z
        Returns a :class:`~gridfs.GridOut` file opened in `mode`, or
        raises :exc:`~gridfs.errors.NoFile` if the requested file
        doesn't exist and mode is not 'w'.
        wfilename)_get_gridfsget_last_versionr   new_file)r"   pathmodegridfsr(   s        r   _openzGridFSStorage._openJ   sY      ++D1	**844 	d{99		s   ' A
A
c                 j    | j                  |      \  }}|j                  |||j                         |S )z:
        Saves `content` into the file at `path`.
        )r(   contentType)r)   putcontent_type)r"   r,   contentr.   r(   s        r   _savezGridFSStorage._saveY   s6      ++D1

7X7;O;O
Pr$   c                     | j                  |      \  }}	 |j                  |j                  |      j                  d             y# t        $ r Y yw xY w)z:
        Deletes the file at `path` if it exists.
        r'   _idN)r)   deleter*   __getattribute__r   r"   r,   r.   r(   s       r   r8   zGridFSStorage.deletea   sV      ++D1	MM&1181DUUV[\] 		s   0A 	AAc                 N    | j                  |      \  }}|j                  |      S )zH
        Returns `True` if the file at `path` exists in GridFS.
        r'   )r)   existsr:   s       r   r<   zGridFSStorage.existsk   s*      ++D1}}h}//r$   c                     | j                  |      \  }}|rJ t        |j                  d            }t        d |D              |j	                         fS )zc
        Returns a tuple (folders, lists) that are contained in the
        folder `path`.
        __collectionc              3   D   K   | ]  }|j                  d       d     yw)r   N)split).0cs     r   	<genexpr>z(GridFSStorage.listdir.<locals>.<genexpr>z   s     <1773<#<s    )r)   r   r9   setlist)r"   r,   r.   r(   subcollectionss        r   listdirzGridFSStorage.listdirr   sR    
  ++D1|,V-D-D^-TU<^<<fkkmKKr$   c                 b    | j                  |      \  }}|j                  |      j                  S )z9
        Returns the size of the file at `path`.
        r'   )r)   r*   lengthr:   s       r   sizezGridFSStorage.size|   s3      ++D1&&&9@@@r$   c                    | j                   t        d      | j                  |      \  }}	 |j                  |      j	                  d      }t        | j                   t        t        |                  S # t
        $ r> 	 t        |      }n# t        t        t        f$ r Y Y y w xY w|j                  |      sY y Y nw xY w)Nz&This file is not accessible via a URL.r'   r7   )r    
ValueErrorr)   r*   __getattr__r   r   r   	TypeErrorr<   r   r	   str)r"   r   r.   r(   file_oids        r   urlzGridFSStorage.url   s    == EFF++D1	...AMMeTH t}}oc(m&DEE  		#D>y*5  ==* +		s5   !A6 6	B= BB=B$ B=#B$$B=<B=c                 b    | j                  |      \  }}|j                  |      j                  S )zF
        Returns the datetime the file at `path` was created.
        r'   )r)   r*   upload_dater:   s       r   created_timezGridFSStorage.created_time   s3      ++D1&&&9EEEr$   c                 r   t         j                  j                  |      \  }}t         j                  j                  | j                  | j
                  |j                  t         j                              }|j                  t         j                  d      j                  d      }t        | d      sqt        | j                     j                  | _        | j                  Ct        | j                     j                          t        | j                     j                  | _        t        | j                  |      |fS )z`
        Returns a :class:`~gridfs.GridFS` using the sub-collection for
        `path`.
        r   _db)r   r,   rA   joinr   r   r   r   replacehasattrr
   r   
connectionrW   connectr   )r"   r,   r(   collection_names       r   r)   zGridFSStorage._get_gridfs   s    
 t,hww||DOOT]]DJJrvv<NO,,rvvs399#>tU#"4==1<<DHxxDMM*224&t}}5@@dhh0(::r$   c                      y N r"   r   s     r   get_accessed_timezGridFSStorage.get_accessed_time       r$   c                      y r_   r`   ra   s     r   get_created_timezGridFSStorage.get_created_time   rc   r$   c                      y r_   r`   ra   s     r   get_modified_timezGridFSStorage.get_modified_time   rc   r$   c                      y r_   r`   ra   s     r   r,   zGridFSStorage.path   rc   r$   ) storagedefaultN)rb)__name__
__module____qualname____doc__r#   r/   r5   r8   r<   rH   rK   rR   rU   r)   rb   re   rg   r,   r`   r$   r   r   r      s\    @ DM10LAF$F;"r$   r   )r   urllib.parser   bsonr   bson.errorsr   django.core.exceptionsr   django.core.files.storager   django.utils.deconstructr   django.utils.encodingr	   	django.dbr
   r.   r   r   r   r   r`   r$   r   <module>ry      sF    	    ! 7 - 4 1 ! ! \G \ \r$   