Browse Source

Allow partial content on bytesio

pull/2957/head
Ignasi Bosch 6 years ago
parent
commit
ab530ea35c
  1. 11
      flask/helpers.py
  2. 18
      tests/test_helpers.py

11
flask/helpers.py

@ -8,7 +8,7 @@
:copyright: © 2010 by the Pallets team. :copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import io
import os import os
import socket import socket
import sys import sys
@ -510,6 +510,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
Filenames are encoded with ASCII instead of Latin-1 for broader Filenames are encoded with ASCII instead of Latin-1 for broader
compatibility with WSGI servers. compatibility with WSGI servers.
.. versionadded:: 1.1
Partial content supports bytesIO
:param filename_or_fp: the filename of the file to send. :param filename_or_fp: the filename of the file to send.
This is relative to the :attr:`~Flask.root_path` This is relative to the :attr:`~Flask.root_path`
if a relative path is specified. if a relative path is specified.
@ -593,6 +596,12 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
mtime = os.path.getmtime(filename) mtime = os.path.getmtime(filename)
fsize = os.path.getsize(filename) fsize = os.path.getsize(filename)
headers['Content-Length'] = fsize headers['Content-Length'] = fsize
elif isinstance(file, io.BytesIO):
try:
fsize = file.getbuffer().nbytes
except AttributeError:
fsize = len(file.getvalue())
headers['Content-Length'] = fsize
data = wrap_file(request.environ, file) data = wrap_file(request.environ, file)
rv = current_app.response_class(data, mimetype=mimetype, headers=headers, rv = current_app.response_class(data, mimetype=mimetype, headers=headers,

18
tests/test_helpers.py

@ -10,6 +10,7 @@
""" """
import datetime import datetime
import io
import os import os
import uuid import uuid
@ -586,6 +587,23 @@ class TestSendfile(object):
assert rv.status_code == 200 assert rv.status_code == 200
rv.close() rv.close()
@pytest.mark.skipif(
not callable(getattr(Range, 'to_content_range_header', None)),
reason="not implemented within werkzeug"
)
def test_send_file_range_request_bytesio(self, app, client):
@app.route('/')
def index():
file = io.BytesIO(b'somethingsomething')
return flask.send_file(
file, attachment_filename='filename', conditional=True
)
rv = client.get('/', headers={'Range': 'bytes=4-15'})
assert rv.status_code == 206
assert rv.data == b'somethingsomething'[4:16]
rv.close()
@pytest.mark.skipif( @pytest.mark.skipif(
not callable(getattr(Range, 'to_content_range_header', None)), not callable(getattr(Range, 'to_content_range_header', None)),
reason="not implemented within werkzeug" reason="not implemented within werkzeug"

Loading…
Cancel
Save