Skip to content

Commit

Permalink
fix partial content range for iOS safari media
Browse files Browse the repository at this point in the history
  • Loading branch information
keotl committed Jun 18, 2021
1 parent 892dfb0 commit 685ec93
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions jivago/wsgi/request/partial_content_handler.py
Expand Up @@ -7,14 +7,15 @@

class PartialContentHandler(object):

def handle_partial_content_request(self, request: Request, filepath: str, *, max_block_size: int = 2000000) -> Response:
def handle_partial_content_request(self, request: Request, filepath: str, *, max_block_size: int = 2000000,
content_type = "application/octet-stream") -> Response:
if request.headers['Range']:
total_filesize = os.path.getsize(filepath)
start, end = self._parse_range_string(request.headers['Range'], max_block_size, total_filesize)
return Response(206, {"Content-Type": "application/octet-stream", "Accept-Ranges": "bytes",
"Content-Range": f"bytes {start}-{end - 1}/{total_filesize}"}, self._read_partial_file(filepath, start, end))
return Response(206, {"Content-Type": content_type, "Accept-Ranges": "bytes",
"Content-Range": f"bytes {start}-{end-1}/{total_filesize}"}, self._read_partial_file(filepath, start, end))

return Response(200, {"Content-Type": "application/octet-stream"}, self._read_whole_file(filepath))
return Response(200, {"Content-Type": content_type}, self._read_whole_file(filepath))

def _parse_range_string(self, range_string: str, default_block_size: int, file_end_offset: int) -> Tuple[int, int]:
start = int(range_string.split("=")[1].split("-")[0])
Expand All @@ -29,7 +30,7 @@ def _parse_range_string(self, range_string: str, default_block_size: int, file_e
def _read_partial_file(self, filepath: str, start: int, end: int) -> bytes:
with open(filepath, 'rb') as f:
f.seek(start)
return f.read(end - start)
return f.read(end - start + 1)

def _read_whole_file(self, filepath: str) -> bytes:
with open(filepath, 'rb') as f:
Expand Down

0 comments on commit 685ec93

Please sign in to comment.