privateone commited on
Commit
59e7379
·
1 Parent(s): a88b8c8

Code Updates & Optimisations :Server Upgrade Test

Browse files
FileStream/server/Functions/downloader.py CHANGED
@@ -21,7 +21,7 @@ from FileStream.utils.FileProcessors.custom_ul import TeleUploader
21
 
22
  async def media_streamer(request: web.Request, db_id: str, speed: str):
23
  # Get the Range header from the request, default to 0 if not present
24
- range_header = request.headers.get("Range", "0")
25
  client = await req_client()
26
  # Log client info if multi-client mode
27
  if Telegram.MULTI_CLIENT:
@@ -49,7 +49,7 @@ async def media_streamer(request: web.Request, db_id: str, speed: str):
49
  from_bytes, until_bytes = parse_range(range_header, file_size)
50
 
51
  # If range is invalid, return a 416 error
52
- if from_bytes is None or until_bytes is None:
53
  return web.Response(
54
  status=416,
55
  body="416: Range not satisfiable",
@@ -58,7 +58,7 @@ async def media_streamer(request: web.Request, db_id: str, speed: str):
58
 
59
  # Set chunk size based on speed
60
  chunk_size = 1024 * 1024 if speed == "FAST" else 512 * 1024
61
-
62
 
63
  # Ensure we don't go past the file size
64
  until_bytes = min(until_bytes, file_size - 1)
@@ -97,10 +97,13 @@ async def media_streamer(request: web.Request, db_id: str, speed: str):
97
  def parse_range(range_header: str, file_size: int):
98
  """Helper function to parse the range header."""
99
  try:
100
- range_str = range_header.replace("bytes=", "")
101
- from_bytes, until_bytes = range_str.split("-")
102
- from_bytes = int(from_bytes)
103
- until_bytes = int(until_bytes) if until_bytes else file_size - 1
 
 
 
104
  return from_bytes, until_bytes
105
  except ValueError:
106
  return None, None
 
21
 
22
  async def media_streamer(request: web.Request, db_id: str, speed: str):
23
  # Get the Range header from the request, default to 0 if not present
24
+ range_header = request.headers.get("Range", 0)
25
  client = await req_client()
26
  # Log client info if multi-client mode
27
  if Telegram.MULTI_CLIENT:
 
49
  from_bytes, until_bytes = parse_range(range_header, file_size)
50
 
51
  # If range is invalid, return a 416 error
52
+ if (until_bytes > file_size) or (from_bytes < 0) or (until_bytes < from_bytes):
53
  return web.Response(
54
  status=416,
55
  body="416: Range not satisfiable",
 
58
 
59
  # Set chunk size based on speed
60
  chunk_size = 1024 * 1024 if speed == "FAST" else 512 * 1024
61
+
62
 
63
  # Ensure we don't go past the file size
64
  until_bytes = min(until_bytes, file_size - 1)
 
97
  def parse_range(range_header: str, file_size: int):
98
  """Helper function to parse the range header."""
99
  try:
100
+ if range_header:
101
+ from_bytes, until_bytes = range_header.replace("bytes=", "").split("-")
102
+ from_bytes = int(from_bytes)
103
+ until_bytes = int(until_bytes) if until_bytes else file_size - 1
104
+ else:
105
+ from_bytes = request.http_range.start or 0
106
+ until_bytes = (request.http_range.stop or file_size) - 1
107
  return from_bytes, until_bytes
108
  except ValueError:
109
  return None, None