This commit is contained in:
blank X 2021-05-04 19:48:13 +07:00
parent 91a25b6bd6
commit 2765fd36e3
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 18 additions and 17 deletions

View File

@ -66,17 +66,23 @@ async def handler(request):
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):
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
for i in messages:
max_size += _get_file_info(i).size
if end is None:
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}'})
length = end - offset
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:
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}'})
else:
end -= 1
length = end - offset + 1
async def download():
tmp_offset = offset
@ -88,23 +94,18 @@ async def handler(request):
if tmp_offset > size:
tmp_offset -= size
continue
stream = client._iter_download(i, offset=tmp_offset, request_size=131072, 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:
async for chunk in client._iter_download(i, offset=tmp_offset, msg_data=(chat_id, i.id)):
yield chunk[:tmp_length]
tmp_length -= len(chunk)
if tmp_length < 1:
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(),
headers={
'Content-Range': f'bytes {offset}-{end}/{max_size}',
'Content-Length': str(end - offset),
'Content-Length': str(length),
'Accept-Ranges': 'bytes'
}
)