3M/cef_3M/util.py
CEF Server 6a69c5a34d add video streaming endpoints
adjust config for bypassing nginx
add communication to ergo
add cachebusting + fixing icons
2024-07-13 00:31:43 +00:00

69 lines
1.7 KiB
Python

import asyncio
import hashlib
import re
import config
from minio import Minio
from redis import Redis
from fastapi import UploadFile
safeName = re.compile(r"[^\w\d\.-]")
# If this gets too out of hand, put an async breakpoint to allow other things to be handled while the hash occurs
async def SHA256(f: UploadFile) -> str:
sha = hashlib.sha256()
while data := await f.read(65535):
sha.update(data)
await f.seek(0)
return sha.hexdigest()
minioClient = Minio(
config.MINIO_INTERNAL_ADDR,
secure=False, # you will probably not have SSL
access_key=config.MINIO_ACCESS_KEY,
secret_key=config.MINIO_SECRET_KEY,
)
redis = Redis(host='localhost', port=6379, db=0, protocol=3)
class ErgoClient:
def __init__(self):
self.reader = None
self.writer = None
asyncio.get_running_loop().create_task(self.init())
@staticmethod
def retry(f):
async def wrapper(self, *args, **kwargs):
i = 30
while i:
try:
return await f(self, *args, **kwargs)
except RuntimeError:
self.init()
i -= 1
print("Couldn't connect")
return wrapper
@retry
async def init(self):
self.reader, self.writer = await asyncio.open_connection(config.ERGO_ADDR, config.ERGO_PORT)
@retry
async def write(self, msg):
self.writer.write(msg+b"\n")
await self.writer.drain()
async def broadcastAs(self, user, *message):
await self.write(f"BROADCASTAS {user} {' '.join(message)}".encode("utf8"))
async def broadcastTo(self, user, *message):
await self.write(f"BROADCASTTO {user} {' '.join(message)}".encode("utf8"))
ergo = ErgoClient()