File size: 1,697 Bytes
00c68f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from src.Classes.Sanity import Sanity
from aiohttp.web import Request
from aiohttp import web
from telethon import TelegramClient
from telethon.tl.types import Message
from src.Classes.Download import Download


class Handler:
    req: Request
    client: TelegramClient
    chat_id = -1001278111932
    message: Message
    route: str
    head = False
    sanity: Sanity

    def __init__(self, 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 = int(self.req.match_info["id"])

    async def sanity_checks(self):
        self.message = await self.sanity.file_exists()

        try:
            if not self.message.media:
                return web.json_response(
                    status=404,
                    data={"Error": "File Does not Exist", "route": self.route},
                )
        except:
            return web.json_response(
                status=404, data={"Error": "File Does not Exist", "route": self.route}
            )

        if self.sanity.check_ranges() == False:
            return web.json_response(
                status=416,
                text="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 web.Response:
            return response

        # download/stream
        return await Download(self).handle_request()