Tuesday, October 30, 2018

Regarding FileResponse and compressed file objects. Bug?

Consider this situation:

import gzip
from django.http import FileResponse


def some_request(request):
   
return FileResponse(
        gzip
.open('/path/to/filename.gz'),
        content_type
="text/plain",
        filename
="filename.txt",
        as_attachment
=True
   
)


This theoretically should work fine, because why shouldn't it?


if os.path.isabs(filename):
   
self['Content-Length'] = os.path.getsize(filelike.name)
elif hasattr(filelike, 'getbuffer'):
   
self['Content-Length'] = filelike.getbuffer().nbytes

Which gets the file size of the compressed gzip file. (the elif branch is never run) That means that the Content-Length will be shorter than the uncompressed content that I am actually sending.

My current "dumb" solution is as such:

from io import BytesIO
import shutil


def some_request(request):
    buff
= BytesIO()
    with gzip.open('/path/to/filename.gz') as g:
        shutil.copyfileobj(g, buff)
    buff
.seek(0)

    return FileResponse(
        buff,
        content_type
="text/plain",
        filename
="filename.txt",
        as_attachment
=True
   
)

This reads all the uncompressed content into a separate file-like object that does not have the original file name information. Which forces FileResponse to run the elif branch. Is there a better way to do things?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f53a5bea-1b52-4e22-8e2b-a88c2c1da5ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment