Exemplo n.º 1
0
    def __init__(self, database, *args, **kwargs):
        if not isinstance(database, Database):
            raise TypeError(
                "Expected Database, got %s" % repr(database))

        self.delegate = motor.MotorGridFSBucket(
            database.delegate, *args, **kwargs)
Exemplo n.º 2
0
    async def test_iteration(self):
        fs = motor.MotorGridFSBucket(self.db)
        _id = await fs.upload_from_stream('filename', b'foo')
        g = motor.MotorGridOut(self.db.fs, _id)

        # Iteration is prohibited.
        self.assertRaises(TypeError, iter, g)
Exemplo n.º 3
0
    def __init__(self, database, bucket_name='fs', disable_md5=False):
        if not isinstance(database, Database):
            raise TypeError(
                "Expected Database, got %s" % repr(database))

        self.delegate = motor.MotorGridFSBucket(
            database.delegate, bucket_name, disable_md5)
Exemplo n.º 4
0
 def get(self, filename):
     db = self.settings['db']
     fs = motor.MotorGridFSBucket(db)
     try:
         #file_id = yield fs.upload_from_stream(filename)
         grid_out = yield fs.open_download_stream_by_name(filename)
     except gridfs.NoFile:
         raise tornado.web.HTTPError(404)
     yield grid_out.stream_to_handler(self)
     self.finish()
Exemplo n.º 5
0
    async def test_stream_to_handler(self):
        fs = motor.MotorGridFSBucket(self.db)

        for content_length in (0, 1, 100, 100 * 1000):
            _id = await fs.upload_from_stream('filename', b'a' * content_length)
            gridout = await fs.open_download_stream(_id)
            handler = MockRequestHandler()
            await gridout.stream_to_handler(handler)
            self.assertEqual(content_length, handler.n_written)
            await fs.delete(_id)
Exemplo n.º 6
0
 def setUp(self):
     super().setUp()
     self.io_loop.run_sync(self._reset)
     self.bucket = motor.MotorGridFSBucket(self.db)
Exemplo n.º 7
0
    async def get(self, path, include_body=True):
        fs = motor.MotorGridFSBucket(self.database, self.root_collection)

        try:
            gridout = await self.get_gridfs_file(fs, path, self.request)
        except gridfs.NoFile:
            raise tornado.web.HTTPError(404)

        # If-Modified-Since header is only good to the second.
        modified = gridout.upload_date.replace(microsecond=0)
        self.set_header("Last-Modified", modified)

        # MD5 is calculated on the MongoDB server when GridFS file is created
        self.set_header("Etag", '"%s"' % gridout.md5)

        mime_type = gridout.content_type

        # If content type is not defined, try to check it with mimetypes
        if mime_type is None:
            mime_type, encoding = mimetypes.guess_type(path)

        # Starting from here, largely a copy of StaticFileHandler
        if mime_type:
            self.set_header("Content-Type", mime_type)

        cache_time = self.get_cache_time(path, modified, mime_type)

        if cache_time > 0:
            self.set_header(
                "Expires",
                datetime.datetime.utcnow() +
                datetime.timedelta(seconds=cache_time))
            self.set_header("Cache-Control", "max-age=" + str(cache_time))
        else:
            self.set_header("Cache-Control", "public")

        self.set_extra_headers(path, gridout)

        # Check the If-Modified-Since, and don't send the result if the
        # content has not been modified
        ims_value = self.request.headers.get("If-Modified-Since")
        if ims_value is not None:
            date_tuple = email.utils.parsedate(ims_value)

            # If our MotorClient is tz-aware, assume the naive ims_value is in
            # its time zone.
            if_since = datetime.datetime.fromtimestamp(
                time.mktime(date_tuple)).replace(tzinfo=modified.tzinfo)

            if if_since >= modified:
                self.set_status(304)
                return

        # Same for Etag
        etag = self.request.headers.get("If-None-Match")
        if etag is not None and etag.strip('"') == gridout.md5:
            self.set_status(304)
            return

        self.set_header("Content-Length", gridout.length)
        if include_body:
            await gridout.stream_to_handler(self)

        # Needed until fix for Tornado bug 751 is released, see
        # https://github.com/facebook/tornado/issues/751 and
        # https://github.com/facebook/tornado/commit/5491685
        self.finish()