async def post(self): db = self.settings["db"] fsdb = MotorGridFSBucket(db) success = False """Gather file data.""" file_info = self.request.files['file'][0] file_name = file_info['filename'] file_content = file_info['body'] file_checksum = util.checksum(file_content) """Check if file exists already.""" document = await db["fs.files"].find_one( {"metadata.checksum": file_checksum}) if document: document_id = str(document["_id"]) uploaded = False else: """Esure that the uploaded file is parsable as PCAP.""" try: ftmp = tempfile.NamedTemporaryFile(delete=True) ftmp.write(file_content) ftmp.flush() rdpcap(ftmp.name) ftmp.close() except Scapy_Exception as e: self.write( dict(success=False, data=dict( message= "Submitted file does not qualify as a PCAP file.", exception=str(e)))) self.set_status(500) return """File does not exist, proceed with upload.""" async with fsdb.open_upload_stream(file_name, metadata=dict( sha256=file_checksum, analyzed=False, )) as grid_in: document_id = str(grid_in._id) uploaded = True await grid_in.write(file_content) await db.tasks.insert_one( dict(type="pcap_inserted", data=grid_in._id, date=datetime.utcnow())) """Send success=true due to upload success. also refer to sha256.""" success = True """Send success=false due to the upload failure, however, send the sha256 in return.""" self.write( dict(success=success, data=dict( filename=file_name, id=document_id, sha256=file_checksum, uploaded=uploaded, ))) return
def test_gridout_attrs(self): motor_gridout_only = set(['open', 'stream_to_handler']).union(motor_only) fs = MotorGridFSBucket(self.cx.test) motor_gridout = yield fs.open_download_stream(1) self.assertEqual(attrs(self.sync_fs.open_download_stream(1)), attrs(motor_gridout) - motor_gridout_only)
def test_gridout_attrs(self): motor_gridout_only = set([ 'open', 'stream_to_handler' ]).union(motor_only) fs = MotorGridFSBucket(self.cx.test) motor_gridout = yield fs.open_download_stream(1) self.assertEqual( attrs(self.sync_fs.open_download_stream(1)), attrs(motor_gridout) - motor_gridout_only)
def test_gridfs_attrs(self): motor_gridfs_only = set(["collection"]).union(motor_only) self.assertEqual( attrs(GridFSBucket(env.sync_cx.test)), attrs(MotorGridFSBucket(self.cx.test)) - motor_gridfs_only, )
def run(self): logger.suricata.info("Starting Suricata-watch Worker.") loop = asyncio.get_event_loop() self.loop = loop self.fsdb = MotorGridFSBucket(self.db) # Prevent "Task was destroyed but it is pending!" task = loop.create_task(self.watch_handler()) loop.run_until_complete(task) loop.run_forever()
async def test_gridout_attrs(self): motor_gridout_only = set(["open", "stream_to_handler"]).union(motor_only) gridin_only = set( [ "md5", "readlines", "truncate", "flush", "fileno", "closed", "writelines", "isatty", "writable", ] ) fs = MotorGridFSBucket(self.cx.test) motor_gridout = await fs.open_download_stream(1) self.assertEqual( attrs(self.sync_fs.open_download_stream(1)) - gridin_only, attrs(motor_gridout) - motor_gridout_only, )
def test_gridout_cursor_attrs(self): self.assertEqual( attrs(self.sync_fs.find()) - pymongo_cursor_only, attrs(MotorGridFSBucket(self.cx.test).find()) - motor_cursor_only)
def verify_bucket(self, bucket: MotorGridFSBucket) -> bool: return bucket.get_io_loop() == self._io_loop
def getGridFs(self, collection: str = 'fs') -> MotorGridFSBucket: if not self._file_client: self._file_client = MotorGridFSBucket(self.getDatabase(), collection)
def getFileBucket(self, bucket_name: str = 'fs') -> MotorGridFSBucket: return MotorGridFSBucket(self.getDatabase(), collection=bucket_name)