Dr.Caduceus commited on
Commit
fcba48d
·
unverified ·
1 Parent(s): 04a916b

Bump to v1.3

Browse files
Files changed (1) hide show
  1. bot/server/main.py +55 -10
bot/server/main.py CHANGED
@@ -1,8 +1,9 @@
1
  from quart import Blueprint, Response, request, render_template, redirect
2
- from .error import abort
3
- from bot import version, TelegramBot
4
  from bot.config import Telegram, Server
5
  from bot.modules.telegram import get_message, get_file_properties
 
6
 
7
  bp = Blueprint('main', __name__)
8
 
@@ -14,19 +15,63 @@ async def home():
14
  async def transmit_file(file_id):
15
  file = await get_message(int(file_id)) or abort(404)
16
  code = request.args.get('code') or abort(401)
 
17
 
18
  if code != file.caption:
19
  abort(403)
20
-
21
- file_name, mime_type = await get_file_properties(file)
22
- headers = {
23
- 'Content-Type': mime_type,
24
- 'Content-Disposition': f'attachment; filename="{file_name}"'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
 
27
- file_stream = TelegramBot.stream_media(file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- return Response(file_stream, headers=headers)
30
 
31
  @bp.route('/stream/<int:file_id>')
32
  async def stream_file(file_id):
@@ -38,4 +83,4 @@ async def stream_file(file_id):
38
  async def file_deeplink(file_id):
39
  code = request.args.get('code') or abort(401)
40
 
41
- return redirect(f'https://t.me/{Telegram.BOT_USERNAME}?start=file_{file_id}_{code}')
 
1
  from quart import Blueprint, Response, request, render_template, redirect
2
+ from math import ceil, floor
3
+ from bot import TelegramBot
4
  from bot.config import Telegram, Server
5
  from bot.modules.telegram import get_message, get_file_properties
6
+ from .error import abort
7
 
8
  bp = Blueprint('main', __name__)
9
 
 
15
  async def transmit_file(file_id):
16
  file = await get_message(int(file_id)) or abort(404)
17
  code = request.args.get('code') or abort(401)
18
+ range_header = request.headers.get('Range', 0)
19
 
20
  if code != file.caption:
21
  abort(403)
22
+
23
+ file_name, file_size, mime_type = await get_file_properties(file)
24
+
25
+ if range_header:
26
+ from_bytes, until_bytes = range_header.replace("bytes=", "").split("-")
27
+ from_bytes = int(from_bytes)
28
+ until_bytes = int(until_bytes) if until_bytes else file_size - 1
29
+ else:
30
+ from_bytes = 0
31
+ until_bytes = file_size -1
32
+
33
+ if (until_bytes > file_size) or (from_bytes < 0) or (until_bytes < from_bytes):
34
+ abort(416, 'Invalid range.')
35
+
36
+ chunk_size = 1024 * 1024
37
+ until_bytes = min(until_bytes, file_size - 1)
38
+
39
+ offset = from_bytes - (from_bytes % chunk_size)
40
+ first_part_cut = from_bytes - offset
41
+ last_part_cut = until_bytes % chunk_size + 1
42
+
43
+ req_length = until_bytes - from_bytes + 1
44
+ part_count = ceil(until_bytes / chunk_size) - floor(offset / chunk_size)
45
+
46
+ disposition = 'inline' if 'video' in mime_type or 'audio' in mime_type or 'html' in mime_type else 'attachment'
47
+ headers={
48
+ 'Content-Type': f'{mime_type}',
49
+ 'Content-Range': f'bytes {from_bytes}-{until_bytes}/{file_size}',
50
+ 'Content-Length': str(req_length),
51
+ 'Content-Disposition': f'{disposition}; filename="{file_name}"',
52
+ 'Accept-Ranges': 'bytes',
53
  }
54
 
55
+ async def file_streamer():
56
+ current_part = 1
57
+ async for chunk in TelegramBot.stream_media(file, offset = from_bytes // chunk_size):
58
+
59
+ if not chunk:
60
+ break
61
+ elif part_count == 1:
62
+ yield chunk[first_part_cut:last_part_cut]
63
+ elif current_part == 1:
64
+ yield chunk[first_part_cut:]
65
+ elif current_part == part_count:
66
+ yield chunk[:last_part_cut]
67
+ else:
68
+ yield chunk
69
+ current_part += 1
70
+
71
+ if current_part > part_count:
72
+ break
73
 
74
+ return Response(file_streamer(), headers=headers, status=206 if range_header else 200)
75
 
76
  @bp.route('/stream/<int:file_id>')
77
  async def stream_file(file_id):
 
83
  async def file_deeplink(file_id):
84
  code = request.args.get('code') or abort(401)
85
 
86
+ return redirect(f'https://t.me/{Telegram.BOT_USERNAME}?start=file_{file_id}_{code}')