Support large downloads in send_file (fixes #3)

This commit is contained in:
Miguel Grinberg
2020-02-19 00:01:51 +00:00
parent 1aacb3cf46
commit 3e29af5775
3 changed files with 33 additions and 16 deletions

View File

@@ -59,7 +59,17 @@ class Response(BaseResponse):
# body
if self.body:
await stream.awrite(self.body)
if hasattr(self.body, 'read'):
while True:
buf = self.body.read(self.send_file_buffer_size)
if len(buf):
await stream.awrite(buf)
if len(buf) < self.send_file_buffer_size:
break
if hasattr(self.body, 'close'):
self.body.close()
else:
await stream.awrite(self.body)
class Microdot(BaseMicrodot):