Dr.Caduceus commited on
Commit
a199af2
·
unverified ·
1 Parent(s): b5de3e2

Bump to v1.5

Browse files
Files changed (1) hide show
  1. bot/server/main.py +24 -25
bot/server/main.py CHANGED
@@ -1,9 +1,9 @@
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
 
@@ -13,22 +13,22 @@ async def home():
13
 
14
  @bp.route('/dl/<int:file_id>')
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.')
@@ -36,26 +36,24 @@ async def transmit_file(file_id):
36
  chunk_size = 1024 * 1024
37
  until_bytes = min(until_bytes, file_size - 1)
38
 
39
- offset = from_bytes // chunk_size
40
- first_part_cut = from_bytes - (from_bytes - (from_bytes % chunk_size))
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((from_bytes - (from_bytes % chunk_size)) / 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 = offset):
58
-
59
  if not chunk:
60
  break
61
  elif part_count == 1:
@@ -66,12 +64,13 @@ async def transmit_file(file_id):
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):
 
1
  from quart import Blueprint, Response, request, render_template, redirect
2
+ from .error import abort
3
  from bot import TelegramBot
4
  from bot.config import Telegram, Server
5
+ from math import ceil, floor
6
  from bot.modules.telegram import get_message, get_file_properties
 
7
 
8
  bp = Blueprint('main', __name__)
9
 
 
13
 
14
  @bp.route('/dl/<int:file_id>')
15
  async def transmit_file(file_id):
16
+ file = await get_message(message_id=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.raw_text:
21
  abort(403)
22
 
23
+ file_name, file_size, mime_type = 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.')
 
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
+ headers = {
47
+ "Content-Type": f"{mime_type}",
48
+ "Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
49
+ "Content-Length": str(req_length),
50
+ "Content-Disposition": f'attachment; filename="{file_name}"',
51
+ "Accept-Ranges": "bytes",
52
+ }
53
+
54
+ async def file_generator():
 
55
  current_part = 1
56
+ async for chunk in TelegramBot.iter_download(file, offset=offset, chunk_size=chunk_size, stride=chunk_size, file_size=file_size):
 
57
  if not chunk:
58
  break
59
  elif part_count == 1:
 
64
  yield chunk[:last_part_cut]
65
  else:
66
  yield chunk
67
+
68
  current_part += 1
69
 
70
  if current_part > part_count:
71
  break
72
 
73
+ return Response(file_generator(), headers=headers, status=206 if range_header else 200)
74
 
75
  @bp.route('/stream/<int:file_id>')
76
  async def stream_file(file_id):