
    df                         d Z ddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ  G d dej                  j                        Zy)z8Utilities for using Motor with Tornado web applications.    N)_hash_gridoutc                   8    e Zd ZdZd	dZd Zd
dZd Zd Zd Z	y)GridFSHandleraz  A handler that can serve content from GridFS, very similar to
    :class:`tornado.web.StaticFileHandler`.

    .. code-block:: python

        db = motor.MotorClient().my_database
        application = web.Application(
            [
                (r"/static/(.*)", web.GridFSHandler, {"database": db}),
            ]
        )

    By default, requests' If-Modified-Since headers are honored, but no
    specific cache-control timeout is sent to clients. Thus each request for
    a GridFS file requires a quick check of the file's ``uploadDate`` in
    MongoDB. Override :meth:`get_cache_time` in a subclass to customize this.
    c                      || _         || _        y )N)databaseroot_collection)selfr   r   s      B/home/api-vastappli/venv/lib/python3.12/site-packages/motor/web.py
initializezGridFSHandler.initialize9   s     .    c                 $    |j                  |      S )a#  Overridable method to choose a GridFS file to serve at a URL.

        By default, if a URL pattern like ``"/static/(.*)"`` is mapped to this
        ``GridFSHandler``, then the trailing portion of the URL is used as the
        filename, so a request for "/static/image.png" results in a call to
        :meth:`MotorGridFSBucket.open_download_stream_by_name` with "image.png"
        as the ``filename`` argument. To customize the mapping of path to
        GridFS file, override ``get_gridfs_file`` and return a Future
        :class:`~motor.MotorGridOut` from it.

        For example, to retrieve the file by ``_id`` instead of filename::

            class CustomGridFSHandler(motor.web.GridFSHandler):
                def get_gridfs_file(self, bucket, filename, request):
                    # Path is interpreted as _id instead of name.
                    # Return a Future MotorGridOut.
                    return fs.open_download_stream(file_id=ObjectId(path))

        :Parameters:
          - `bucket`: A :class:`~motor.motor_tornado.MotorGridFSBucket`
          - `filename`: A string, the matched group of the URL pattern
          - `request`: An :class:`tornado.httputil.HTTPServerRequest`

        .. versionchanged:: 1.0
          **BREAKING CHANGE**: Now takes a
          :class:`~motor.motor_tornado.MotorGridFSBucket`, not a
          ``MotorGridFS``.
          Also takes an additional ``request`` parameter.

        .. versionchanged:: 0.2
           ``get_gridfs_file`` no longer accepts a callback, instead returns
           a Future.
        )open_download_stream_by_name)r	   bucketfilenamerequests       r
   get_gridfs_filezGridFSHandler.get_gridfs_file=   s    D 228<<r   c                 @  K   t        j                  | j                  | j                        }	 | j	                  ||| j
                         d {   }|j                  j                  d      }| j                  d|       t        |      }| j                  dd|z         |j                  }|t!        j"                  |      \  }}|r| j                  d|       | j%                  |||      }	|	dkD  r| j                  dt&        j&                  j)                  t&        j*                  j,                        j                  d 	      t'        j.                  |	
      z          | j                  ddt1        |	      z          n| j                  dd       | j3                  ||       | j
                  j4                  j7                  d      }
|
t8        j:                  j=                  |
      }t&        j&                  j?                  tA        jB                  |            j                  |jD                  	      }||k\  r| jG                  d       y | j
                  j4                  j7                  d      }|&|jI                  d      |k(  r| jG                  d       y | j                  d|jJ                         |r|jM                  |        d {    | jO                          y 7 # t        j                  $ r! t        j                  j                  d      d w xY w7 Ow)Ni  r   )microsecondzLast-ModifiedEtagz"%s"zContent-TypeExpires)tzinfo)secondszCache-Controlzmax-age=publiczIf-Modified-Sincei0  zIf-None-Match"zContent-Length)(motorMotorGridFSBucketr   r   r   r   gridfsNoFiletornadoweb	HTTPErrorupload_datereplace
set_headerr   content_type	mimetypes
guess_typeget_cache_timedatetimenowtimezoneutc	timedeltastrset_extra_headersheadersgetemailutils	parsedatefromtimestamptimemktimer   
set_statusstriplengthstream_to_handlerfinish)r	   pathinclude_bodyfsgridoutmodifiedchecksum	mime_typeencoding
cache_time	ims_value
date_tupleif_sinceetags                 r
   r1   zGridFSHandler.geta   s    $$T]]D4H4HI	7 00T4<<HHG
 &&..1.=2 !) 12((	 "+"6"6t"<Ix OONI6((xC
>OO!!%%h&7&7&;&;<DDDDQ$$Z89
 OOOZ#j/-IJOOOX6tW- LL((,,-@A	 ..y9J  ((66t{{:7NOWW X H 8#$ ||##''8

38 ;OOC ('..9++D111
 	E I}} 	7++'',$6	7x 2s;   +L K% K"K% I9LLL"K% %4LLc                 (    | j                  |d      S )NF)r>   )r1   )r	   r=   s     r
   headzGridFSHandler.head   s    xx5x11r   c                      y)zOverride to customize cache control behavior.

        Return a positive number of seconds to trigger aggressive caching or 0
        to mark resource as cacheable, only. 0 is the default.
        r    )r	   r=   rA   rC   s       r
   r(   zGridFSHandler.get_cache_time   s     r   c                      y)z1For subclass to add extra headers to the responseNrM   )r	   r=   r@   s      r
   r/   zGridFSHandler.set_extra_headers   s    r   N)r?   )T)
__name__
__module____qualname____doc__r   r   r1   rK   r(   r/   rM   r   r
   r   r   &   s)    $/"=HFP2@r   r   )rR   r)   email.utilsr2   r&   r6   r   tornado.webr   r   motor.motor_gridfsr   r    RequestHandlerr   rM   r   r
   <module>rW      s<    ?        ,P@GKK.. P@r   