Fix bugs
This commit is contained in:
parent
91a25b6bd6
commit
2765fd36e3
27
streamtg.py
27
streamtg.py
|
@ -66,17 +66,23 @@ async def handler(request):
|
||||||
return web.Response(status=400, text='At least one of the messages does not exist')
|
return web.Response(status=400, text='At least one of the messages does not exist')
|
||||||
if any(True for i in messages if not i.media):
|
if any(True for i in messages if not i.media):
|
||||||
return web.Response(status=400, text='At least one of the messages do not contain media')
|
return web.Response(status=400, text='At least one of the messages do not contain media')
|
||||||
http_range = request.http_range
|
|
||||||
offset = http_range.start or 0
|
|
||||||
end = http_range.stop
|
|
||||||
max_size = 0
|
max_size = 0
|
||||||
for i in messages:
|
for i in messages:
|
||||||
max_size += _get_file_info(i).size
|
max_size += _get_file_info(i).size
|
||||||
|
http_range = request.http_range
|
||||||
|
offset = http_range.start or 0
|
||||||
|
if offset < 0:
|
||||||
|
end = offset
|
||||||
|
offset = max_size - offset
|
||||||
|
else:
|
||||||
|
end = http_range.stop
|
||||||
if end is None:
|
if end is None:
|
||||||
end = max_size
|
end = max_size
|
||||||
elif end > max_size:
|
elif end > max_size:
|
||||||
return web.Response(status=416, text='Range end size is bigger than file sizes', headers={'Content-Range': f'bytes */{max_size}'})
|
return web.Response(status=416, text='Range end size is bigger than file sizes', headers={'Content-Range': f'bytes */{max_size}'})
|
||||||
length = end - offset
|
else:
|
||||||
|
end -= 1
|
||||||
|
length = end - offset + 1
|
||||||
|
|
||||||
async def download():
|
async def download():
|
||||||
tmp_offset = offset
|
tmp_offset = offset
|
||||||
|
@ -88,23 +94,18 @@ async def handler(request):
|
||||||
if tmp_offset > size:
|
if tmp_offset > size:
|
||||||
tmp_offset -= size
|
tmp_offset -= size
|
||||||
continue
|
continue
|
||||||
stream = client._iter_download(i, offset=tmp_offset, request_size=131072, msg_data=(chat_id, i.id))
|
async for chunk in client._iter_download(i, offset=tmp_offset, msg_data=(chat_id, i.id)):
|
||||||
if isinstance(stream, _GenericDownloadIter):
|
|
||||||
__load_next_chunk = stream._load_next_chunk
|
|
||||||
def _load_next_chunk(mask=131071):
|
|
||||||
return __load_next_chunk(mask=mask)
|
|
||||||
stream._load_next_chunk = _load_next_chunk
|
|
||||||
async for chunk in stream:
|
|
||||||
yield chunk[:tmp_length]
|
yield chunk[:tmp_length]
|
||||||
tmp_length -= len(chunk)
|
tmp_length -= len(chunk)
|
||||||
if tmp_length < 1:
|
if tmp_length < 1:
|
||||||
break
|
break
|
||||||
|
tmp_offset = 0
|
||||||
|
|
||||||
return web.Response(status=206 if (end - offset != max_size) else 200,
|
return web.Response(status=206 if (length != max_size) else 200,
|
||||||
body=download(),
|
body=download(),
|
||||||
headers={
|
headers={
|
||||||
'Content-Range': f'bytes {offset}-{end}/{max_size}',
|
'Content-Range': f'bytes {offset}-{end}/{max_size}',
|
||||||
'Content-Length': str(end - offset),
|
'Content-Length': str(length),
|
||||||
'Accept-Ranges': 'bytes'
|
'Accept-Ranges': 'bytes'
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue