def download_file(self, request):
file_id = request.match_info['file_id']
record = await db.tracks.find_one({ "file_id": file_id })
if not record:
return web.HTTPNotFound()
file = await self.bot.get_file(file_id)
file_path = file["file_path"]
range = request.headers.get("range")
copy_headers = ["content-length", "content-range", "etag", "last-modified"]
async with self.bot.download_file(file_path, range) as r:
# Prepare headers
resp = web.StreamResponse(status=r.status)
resp.content_type = record["mime_type"]
for h in copy_headers:
val = r.headers.get(h)
if val:
resp.headers[h] = val
await resp.prepare(request)
# Send content
while True:
chunk = await r.content.read(chunk_size)
if not chunk:
break
resp.write(chunk)
return resp
评论列表
文章目录