rest.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:Telegram-Music-Bot 作者: rexx0520 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号