Spaces:
Sleeping
Sleeping
Commit
·
f2eb73c
1
Parent(s):
2de6d2c
update main.py
Browse files- bot/server/main.py +63 -0
bot/server/main.py
CHANGED
|
@@ -250,3 +250,66 @@ async def transmit_file_without(file_id):
|
|
| 250 |
yield last_chunk
|
| 251 |
|
| 252 |
return Response(file_stream(), headers=headers, status=status_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
yield last_chunk
|
| 251 |
|
| 252 |
return Response(file_stream(), headers=headers, status=status_code)
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
@bp.route('/nocode/<int:file_id>', methods=['GET', 'POST', 'HEAD'])
|
| 256 |
+
async def transmit_file_stream(file_id):
|
| 257 |
+
file = await get_message(file_id) or abort(404)
|
| 258 |
+
range_header = request.headers.get('Range')
|
| 259 |
+
|
| 260 |
+
file_name, file_size, mime_type = get_file_properties(file)
|
| 261 |
+
|
| 262 |
+
start = 0
|
| 263 |
+
end = file_size - 1
|
| 264 |
+
chunk_size = 1 * 1024 * 1024 # 1 MB
|
| 265 |
+
|
| 266 |
+
if range_header:
|
| 267 |
+
range_match = re_match(r'bytes=(\d+)-(\d*)', range_header)
|
| 268 |
+
if range_match:
|
| 269 |
+
start = int(range_match.group(1))
|
| 270 |
+
end = int(range_match.group(2)) if range_match.group(2) else file_size - 1
|
| 271 |
+
if start > end or start >= file_size:
|
| 272 |
+
abort(416, 'Requested range not satisfiable')
|
| 273 |
+
else:
|
| 274 |
+
abort(400, 'Invalid Range header')
|
| 275 |
+
|
| 276 |
+
offset_chunks = start // chunk_size
|
| 277 |
+
total_bytes_to_stream = end - start + 1
|
| 278 |
+
chunks_to_stream = ceil(total_bytes_to_stream / chunk_size)
|
| 279 |
+
|
| 280 |
+
content_length = total_bytes_to_stream
|
| 281 |
+
headers = {
|
| 282 |
+
'Content-Type': mime_type,
|
| 283 |
+
'Content-Disposition': f'attachment; filename={file_name}',
|
| 284 |
+
'Content-Range': f'bytes {start}-{end}/{file_size}',
|
| 285 |
+
'Accept-Ranges': 'bytes',
|
| 286 |
+
'Content-Length': str(content_length),
|
| 287 |
+
}
|
| 288 |
+
status_code = 206 if range_header else 200
|
| 289 |
+
|
| 290 |
+
async def file_stream():
|
| 291 |
+
bytes_streamed = 0
|
| 292 |
+
chunk_index = 0
|
| 293 |
+
async for chunk in TelegramBot.stream_media(
|
| 294 |
+
file,
|
| 295 |
+
offset=offset_chunks,
|
| 296 |
+
limit=chunks_to_stream,
|
| 297 |
+
):
|
| 298 |
+
if chunk_index == 0: # Trim the first chunk if necessary
|
| 299 |
+
trim_start = start % chunk_size
|
| 300 |
+
if trim_start > 0:
|
| 301 |
+
chunk = chunk[trim_start:]
|
| 302 |
+
|
| 303 |
+
remaining_bytes = content_length - bytes_streamed
|
| 304 |
+
if remaining_bytes <= 0:
|
| 305 |
+
break
|
| 306 |
+
|
| 307 |
+
if len(chunk) > remaining_bytes: # Trim the last chunk if necessary
|
| 308 |
+
chunk = chunk[:remaining_bytes]
|
| 309 |
+
|
| 310 |
+
yield chunk
|
| 311 |
+
bytes_streamed += len(chunk)
|
| 312 |
+
chunk_index += 1
|
| 313 |
+
|
| 314 |
+
return Response(file_stream(), headers=headers, status=status_code)
|
| 315 |
+
|