Fix common.send_file to use pathclass.

This commit is contained in:
voussoir 2018-03-18 21:40:32 -07:00
parent 0de755f138
commit ea7401b4f2

View file

@ -95,19 +95,16 @@ def send_file(filepath, override_mimetype=None):
''' '''
Range-enabled file sending. Range-enabled file sending.
''' '''
if isinstance(filepath, pathclass.Path): filepath = pathclass.Path(filepath)
filepath = filepath.absolute_path
try: if not filepath.is_file:
file_size = os.path.getsize(filepath)
except FileNotFoundError:
flask.abort(404) flask.abort(404)
outgoing_headers = {} outgoing_headers = {}
if override_mimetype is not None: if override_mimetype is not None:
mimetype = override_mimetype mimetype = override_mimetype
else: else:
mimetype = mimetypes.guess_type(filepath)[0] mimetype = mimetypes.guess_type(filepath.absolute_path)[0]
if mimetype is not None: if mimetype is not None:
if 'text/' in mimetype: if 'text/' in mimetype:
@ -125,25 +122,26 @@ def send_file(filepath, override_mimetype=None):
range_max = int_helper(desired_max) range_max = int_helper(desired_max)
else: else:
range_min = int_helper(desired_range) range_min = int_helper(desired_range)
range_max = None
if range_min is None: if range_min is None:
range_min = 0 range_min = 0
if range_max is None: if range_max is None:
range_max = file_size range_max = filepath.size
# because ranges are 0-indexed # because ranges are 0-indexed
range_max = min(range_max, file_size - 1) range_max = min(range_max, filepath.size - 1)
range_min = max(range_min, 0) range_min = max(range_min, 0)
range_header = 'bytes {min}-{max}/{outof}'.format( range_header = 'bytes {min}-{max}/{outof}'.format(
min=range_min, min=range_min,
max=range_max, max=range_max,
outof=file_size, outof=filepath.size,
) )
outgoing_headers['Content-Range'] = range_header outgoing_headers['Content-Range'] = range_header
status = 206 status = 206
else: else:
range_max = file_size - 1 range_max = filepath.size - 1
range_min = 0 range_min = 0
status = 200 status = 200
@ -154,7 +152,7 @@ def send_file(filepath, override_mimetype=None):
outgoing_data = bytes() outgoing_data = bytes()
else: else:
outgoing_data = etiquette.helpers.read_filebytes( outgoing_data = etiquette.helpers.read_filebytes(
filepath, filepath.absolute_path,
range_min=range_min, range_min=range_min,
range_max=range_max, range_max=range_max,
chunk_size=P.config['file_read_chunk'], chunk_size=P.config['file_read_chunk'],