Mbonea's picture
added streaming
07fd3f6
raw
history blame
1.67 kB
from .Sanity import Sanity
from fastapi import Request
from telethon import TelegramClient
from telethon.tl.types import Message
from .Download import Download
from fastapi.responses import JSONResponse
class Handler:
req: Request
client: TelegramClient
chat_id = -1001925049183
message: Message
route: str
head = False
sanity: Sanity
def __init__(self, id, req: Request, client, route=None, head=False):
self.head = head
self.req = req
self.client = client
self.sanity = Sanity()
self.sanity.client = self.client
self.sanity.chat_id = self.chat_id
self.sanity.req = self.req
self.sanity.file_id = id
async def sanity_checks(self):
self.message = await self.sanity.file_exists()
try:
if not self.message.media:
return JSONResponse(
status_code=404,
content={"Error": "File Does not Exist"},
)
except:
return JSONResponse(
status=404,
content={"Error": "File Does not Exist", "route": self.route},
)
if self.sanity.check_ranges() == False:
return JSONResponse(
status=416,
content={"Error": "416: Range Not Satisfiable"},
headers={"Content-Range": f"bytes */{self.message.file.size}"},
)
async def process_request(self):
response = await self.sanity_checks()
if type(response) is JSONResponse:
return response
# download/stream
return await Download(self).handle_request()