Open filwaline opened 4 years ago
@filwaline that's an interesting question. Do you have a way to fix this in mind?
use @contextlib.contextmanager
@contextlib.contextmanager
def get_file(self):
fileobject = open(...)
try:
yield fileobject
finally:
fileobject.close()
try:
with self.get_file() as f:
self.file_instance = f
# Respect the If-Modified-Since header.
since = self.request.META.get("HTTP_IF_MODIFIED_SINCE", None)
if since is not None:
if not self.was_modified_since(self.file_instance, since):
return self.not_modified_response(**response_kwargs)
# Return download response.
return self.download_response(*response_args, **response_kwargs)
except exceptions.FileNotFound:
return self.file_not_found_response()
https://github.com/jazzband/django-downloadview/blob/master/django_downloadview/views/base.py#L144
code not tested, but it should work
Do you mind creating a pull-request with your fix?
Maybe later
DownloadMixin need a File object to serve, and open file in
get_file
method, but I never found aclose
call(by global text search). Did you close file somewhere? Or Using context managerwith
?https://docs.djangoproject.com/en/2.2/topics/files/#the-file-object
If open too many file and never close them, IOError will raise.