Spaces:
Sleeping
Sleeping
File size: 11,478 Bytes
cb3f46f 35e0008 99a7487 2de6d2c 35e0008 a199af2 99a7487 cb3f46f af2b875 49b1764 af2b875 426ff68 7f86101 af2b875 99a7487 426ff68 99a7487 cb3f46f af2b875 a6fe6f9 af2b875 35e0008 af2b875 35e0008 af2b875 35e0008 af2b875 fcba48d a199af2 fcba48d 35e0008 fcba48d 35e0008 a199af2 35e0008 fcba48d a199af2 35e0008 fcba48d 35e0008 af2b875 35e0008 af2b875 a6fe6f9 af2b875 49b1764 f2eb73c |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
from quart import Blueprint, Response, request, render_template, redirect, jsonify
from math import ceil
import time
import asyncio
from re import match as re_match
from .error import abort
from bot import TelegramBot, StartTime, utils
from bot.config import Telegram, Server
from bot.modules.telegram import get_message, get_file_properties
bp = Blueprint('main', __name__)
CHUNK_SIZE_MB = 4 # Default to 4MB chunks
@bp.route('/')
async def home():
system_info = utils.get_system_info()
# Pass system_info to the template
return await render_template("index.html", system_info=system_info)
@bp.route('/bot')
async def bot():
return redirect(f'https://t.me/{Telegram.BOT_USERNAME}')
@bp.route('/status')
async def status():
return jsonify({
"status": "running",
"uptime": utils.get_readable_time(time.time() - StartTime),
"telegram_bot": "@" + Telegram.BOT_USERNAME,
})
@bp.route('/health')
async def health():
return jsonify({
"message": "running",
})
@bp.route('/dl/<int:file_id>', methods=['GET', 'POST', 'HEAD'])
async def transmit_file(file_id):
file = await get_message(file_id) or abort(404)
code = request.args.get('code') or abort(401)
range_header = request.headers.get('Range')
if code != file.caption.split('/')[0]:
abort(403)
file_name, file_size, mime_type = get_file_properties(file)
start = 0
end = file_size - 1
chunk_size = 1 * 1024 * 1024 # 1 MB
if range_header:
range_match = re_match(r'bytes=(\d+)-(\d*)', range_header)
if range_match:
start = int(range_match.group(1))
end = int(range_match.group(2)) if range_match.group(2) else file_size - 1
if start > end or start >= file_size:
abort(416, 'Requested range not satisfiable')
else:
abort(400, 'Invalid Range header')
offset_chunks = start // chunk_size
total_bytes_to_stream = end - start + 1
chunks_to_stream = ceil(total_bytes_to_stream / chunk_size)
content_length = total_bytes_to_stream
headers = {
'Content-Type': mime_type,
'Content-Disposition': f'attachment; filename={file_name}',
'Content-Range': f'bytes {start}-{end}/{file_size}',
'Accept-Ranges': 'bytes',
'Content-Length': str(content_length),
}
status_code = 206 if range_header else 200
async def file_stream():
bytes_streamed = 0
chunk_index = 0
async for chunk in TelegramBot.stream_media(
file,
offset=offset_chunks,
limit=chunks_to_stream,
):
if chunk_index == 0: # Trim the first chunk if necessary
trim_start = start % chunk_size
if trim_start > 0:
chunk = chunk[trim_start:]
remaining_bytes = content_length - bytes_streamed
if remaining_bytes <= 0:
break
if len(chunk) > remaining_bytes: # Trim the last chunk if necessary
chunk = chunk[:remaining_bytes]
yield chunk
bytes_streamed += len(chunk)
chunk_index += 1
return Response(file_stream(), headers=headers, status=status_code)
@bp.route('/stream/<int:file_id>', methods=['GET', 'POST', 'HEAD'])
async def stream_file(file_id):
code = request.args.get('code') or abort(401)
return await render_template('player.html', mediaLink=f'{Server.BASE_URL}/dl/{file_id}?code={code}')
@bp.route('/ssgen/<int:file_id>', methods=['GET', 'POST', 'HEAD'])
async def transmit_file_without_code(file_id):
file = await get_message(file_id) or abort(404)
range_header = request.headers.get('Range')
file_name, file_size, mime_type = get_file_properties(file)
chunk_size = min(file_size // 50, CHUNK_SIZE_MB * 1024 * 1024) # Adaptive chunk size
start = 0
end = file_size - 1
if range_header:
range_match = re_match(r'bytes=(\d+)-(\d*)', range_header)
if range_match:
start = int(range_match.group(1))
end = int(range_match.group(2)) if range_match.group(2) else file_size - 1
if start > end or start >= file_size:
abort(416, 'Requested range not satisfiable')
else:
abort(400, 'Invalid Range header')
offset_chunks = start // chunk_size
total_bytes_to_stream = end - start + 1
chunks_to_stream = ceil(total_bytes_to_stream / chunk_size)
content_length = total_bytes_to_stream
headers = {
'Content-Type': mime_type,
'Content-Disposition': f'attachment; filename={file_name}',
'Content-Range': f'bytes {start}-{end}/{file_size}',
'Accept-Ranges': 'bytes',
'Content-Length': str(content_length),
}
status_code = 206 if range_header else 200
async def file_stream():
"""Streams the file efficiently using parallel chunk fetching."""
bytes_streamed = 0
chunk_index = 0
async def fetch_chunk(offset, limit):
"""Fetches a chunk of the file asynchronously."""
async for chunk in TelegramBot.stream_media(file, offset=offset, limit=limit):
return chunk
return None
chunk_tasks = [
fetch_chunk(offset_chunks + i, 1) for i in range(chunks_to_stream)
]
fetched_chunks = await asyncio.gather(*chunk_tasks)
for i, chunk in enumerate(fetched_chunks):
if chunk_index == 0: # Trim the first chunk if necessary
trim_start = start % chunk_size
if trim_start > 0:
chunk = chunk[trim_start:]
remaining_bytes = content_length - bytes_streamed
if remaining_bytes <= 0:
break
if len(chunk) > remaining_bytes: # Trim the last chunk if necessary
chunk = chunk[:remaining_bytes]
yield chunk
bytes_streamed += len(chunk)
chunk_index += 1
return Response(file_stream(), headers=headers, status=status_code)
@bp.route('/noauth/<int:file_id>', methods=['GET', 'POST', 'HEAD'])
async def transmit_file_without(file_id):
file = await get_message(file_id) or abort(404)
range_header = request.headers.get('Range')
file_name, file_size, mime_type = get_file_properties(file)
# Defaults for full file
start = 0
end = file_size - 1
# Use adaptive chunk size: up to 4 MB (or smaller if the file is small)
max_chunk_size = 4 * 1024 * 1024 # 4 MB
chunk_size = min(max_chunk_size, file_size)
if range_header:
range_match = re_match(r'bytes=(\d+)-(\d*)', range_header)
if range_match:
start = int(range_match.group(1))
end = int(range_match.group(2)) if range_match.group(2) else file_size - 1
if start > end or start >= file_size:
abort(416, 'Requested range not satisfiable')
else:
abort(400, 'Invalid Range header')
total_bytes_to_stream = end - start + 1
# Determine the starting chunk index and number of chunks needed.
offset_chunks = start // chunk_size
chunks_to_stream = int(ceil(total_bytes_to_stream / chunk_size))
headers = {
'Content-Type': mime_type,
'Content-Disposition': f'attachment; filename={file_name}',
'Content-Range': f'bytes {start}-{end}/{file_size}',
'Accept-Ranges': 'bytes',
'Content-Length': str(total_bytes_to_stream),
}
status_code = 206 if range_header else 200
async def fetch_chunk(chunk_idx):
"""
Fetch a single chunk using TelegramBot.stream_media.
The offset is measured in chunk units.
"""
async for chunk in TelegramBot.stream_media(file, offset=offset_chunks + chunk_idx, limit=1):
return chunk
async def file_stream():
# Launch concurrent tasks for all chunks needed.
tasks = [asyncio.create_task(fetch_chunk(i)) for i in range(chunks_to_stream)]
all_chunks = await asyncio.gather(*tasks)
# Apply trimming to the first and last chunks so that the combined bytes equal total_bytes_to_stream.
# Trim the first chunk if needed.
first_chunk = all_chunks[0][start % chunk_size:]
if chunks_to_stream == 1:
# Only one chunk: yield exactly total_bytes_to_stream bytes.
yield first_chunk[:total_bytes_to_stream]
else:
yield first_chunk
for chunk in all_chunks[1:-1]:
yield chunk
# For the last chunk, compute how many bytes remain after the previous chunks.
bytes_yielded = len(first_chunk) + sum(len(chunk) for chunk in all_chunks[1:-1])
remaining_bytes = total_bytes_to_stream - bytes_yielded
last_chunk = all_chunks[-1][:remaining_bytes]
yield last_chunk
return Response(file_stream(), headers=headers, status=status_code)
@bp.route('/nocode/<int:file_id>', methods=['GET', 'POST', 'HEAD'])
async def transmit_file_stream(file_id):
file = await get_message(file_id) or abort(404)
range_header = request.headers.get('Range')
file_name, file_size, mime_type = get_file_properties(file)
start = 0
end = file_size - 1
chunk_size = 1 * 1024 * 1024 # 1 MB
if range_header:
range_match = re_match(r'bytes=(\d+)-(\d*)', range_header)
if range_match:
start = int(range_match.group(1))
end = int(range_match.group(2)) if range_match.group(2) else file_size - 1
if start > end or start >= file_size:
abort(416, 'Requested range not satisfiable')
else:
abort(400, 'Invalid Range header')
offset_chunks = start // chunk_size
total_bytes_to_stream = end - start + 1
chunks_to_stream = ceil(total_bytes_to_stream / chunk_size)
content_length = total_bytes_to_stream
headers = {
'Content-Type': mime_type,
'Content-Disposition': f'attachment; filename={file_name}',
'Content-Range': f'bytes {start}-{end}/{file_size}',
'Accept-Ranges': 'bytes',
'Content-Length': str(content_length),
}
status_code = 206 if range_header else 200
async def file_stream():
bytes_streamed = 0
chunk_index = 0
async for chunk in TelegramBot.stream_media(
file,
offset=offset_chunks,
limit=chunks_to_stream,
):
if chunk_index == 0: # Trim the first chunk if necessary
trim_start = start % chunk_size
if trim_start > 0:
chunk = chunk[trim_start:]
remaining_bytes = content_length - bytes_streamed
if remaining_bytes <= 0:
break
if len(chunk) > remaining_bytes: # Trim the last chunk if necessary
chunk = chunk[:remaining_bytes]
yield chunk
bytes_streamed += len(chunk)
chunk_index += 1
return Response(file_stream(), headers=headers, status=status_code)
|