
    ej
                     ~    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dS )z8Utilities for using Motor with Tornado web applications.    N)_hash_gridoutc                   :    e Zd ZdZddZd ZddZd Zd Zd	 Z	d
S )GridFSHandlera\  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.
    fsc                 "    || _         || _        d S )N)databaseroot_collection)selfr   r	   s      M/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/motor/web.py
initializezGridFSHandler.initialize6   s     .    c                 ,    |                     |          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   Tc                   K   t          j        | j        | j                  }	 |                     ||| j                   d {V }n2# t          j        $ r  t          j	        
                    d          w xY w|j                            d          }|                     d|           t          |          }|                     dd|z             |j        }|t!          j        |          \  }}|r|                     d|           |                     |||          }	|	dk    ro|                     dt&          j                                        t'          j        |		          z              |                     d
dt-          |	          z              n|                     d
d           |                     ||           | j        j                            d          }
|
t4          j                            |
          }t&          j                            t=          j        |                                        |j                   }||k    r| !                    d           d S | j        j                            d          }|0|"                    d          |k    r| !                    d           d S |                     d|j#                   |r|$                    |            d {V  | %                                 d S )Ni  r   )microsecondzLast-ModifiedEtagz"%s"zContent-TypeExpires)secondszCache-Controlzmax-age=publiczIf-Modified-Since)tzinfoi0  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utcnow	timedeltastrset_extra_headersheadersgetemailutils	parsedatefromtimestamptimemktimer   
set_statusstriplengthstream_to_handlerfinish)r
   pathinclude_bodyr   gridoutmodifiedchecksum	mime_typeencoding
cache_time	ims_value
date_tupleif_sinceetags                 r   r0   zGridFSHandler.get^   s     $T]D4HII	- 00T4<HHHHHHHHGG} 	- 	- 	-+'',,,	- &..1.==222 !)) 1222(	 "+"6t"<"<Ix  	7OONI666((xCC
>>OO8,33558JS]8^8^8^^   OOOZ#j//-IJJJJOOOX666tW--- L(,,-@AA	 ..y99J  (66t{:7N7NOOWW X  H 8##$$$ |#''88

38 ; ;OOC   F('.999 	2++D111111111
 	s   "A /A5c                 0    |                      |d          S )NF)r=   )r0   )r
   r<   s     r   headzGridFSHandler.head   s    xx5x111r   c                     dS )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<   r?   rA   s       r   r)   zGridFSHandler.get_cache_time   s	     qr   c                     dS )z1For subclass to add extra headers to the responseNrK   )r
   r<   r>   s      r   r.   zGridFSHandler.set_extra_headers   s    r   N)r   )T)
__name__
__module____qualname____doc__r   r   r0   rI   r)   r.   rK   r   r   r   r   %   s          / / / /"= "= "=HD D D DL2 2 2      r   r   )rP   r*   email.utilsr1   r'   r5   r   tornado.webr    r   motor.motor_gridfsr   r!   RequestHandlerr   rK   r   r   <module>rU      s    ? >                 , , , , , ,M M M M MGK. M M M M Mr   