File size: 1,672 Bytes
07fd3f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()