async def process(self, message: IncomingMessage): async with message.process(): call_id, path = message.body.decode().splitlines() call = await Call.get_or_none(id=call_id) if not call: logger.warning(f'Bad call_id: {call_id}. Call not found') return if not Path(path).exists(): logger.warning(f'Converted record file not found. File: {path}') return async with async_open(path, 'rb') as f: data = await f.read() link = await upload_record( self.s3client, data, call.create_record_filename(), ) await CallRecord.create( call_id=call_id, file_name=link, attempts_count=1, )
async def capture_message(manager: Manager, message: Message): now = datetime.utcnow().strftime('%Y-%m-%d') try: async with async_open(f'{app_config.log_path}/messages-{now}.txt', 'a') as f: await f.write(json.dumps(dict(message)) + '\n') except Exception: pass
async def upload_record(headers: dict, file_name: str) -> bool: if not app_config.record.enabled or not app_config.record.upload_url: return False async with async_open(file_name, 'rb') as afp: data = await afp.read() headers.update(app_config.record.upload_headers) async with ClientSession(headers=headers) as ahttp: resp = await ahttp.put(app_config.record.upload_url, data=data) return 199 < resp.status < 300
async def handler(request: Request) -> Response: path = STATIC_PATH / request.path if path.is_dir(): path = path / "index.html" if path.exists(): content_type = path.suffix[1:] async with async_open(path.resolve(), "rb") as file: response = Response(await file.read(), content_type=content_type) else: response = ResponseException(404) return response
async def get_mp3_file_duration(path: str) -> int: if not path: return 0 try: async with async_open(path, 'rb') as afp: data = await afp.read() return MPEGInfo(BytesIO(data)).length except Exception as err: logger.warning(f'Error on read mp3 file: {err}') return 0
async def capture_message(msg: str): async with async_open(f'{DATA_PATH}/logs/messages.txt', 'a') as f: await f.write(msg)