From 81485d88ee1a79a2046ace9187b8bd413566f9fc Mon Sep 17 00:00:00 2001 From: Lazlo Westerhof Date: Thu, 20 Jul 2023 13:28:03 +0200 Subject: [PATCH] Set Content-Length header field in the download response. --- deposit/deposit.py | 11 +++++++---- research/research.py | 11 +++++++---- vault/vault.py | 11 +++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/deposit/deposit.py b/deposit/deposit.py index 5e687bb4..1c97a095 100644 --- a/deposit/deposit.py +++ b/deposit/deposit.py @@ -57,12 +57,11 @@ def data() -> Response: def download() -> Response: path = '/' + g.irods.zone + '/home' + request.args.get('filepath') filename = path.rsplit('/', 1)[1] - READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE - def read_file_chunks(path: str) -> Iterator[bytes]: - obj = g.irods.data_objects.get(path) + def read_file_chunks(data_object) -> Iterator[bytes]: + READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE try: - with obj.open('r') as fd: + with data_object.open('r') as fd: while True: buf = fd.read(READ_BUFFER_SIZE) if buf: @@ -76,10 +75,14 @@ def read_file_chunks(path: str) -> Iterator[bytes]: abort(500) if g.irods.data_objects.exists(path): + data_object = g.irods.data_objects.get(path) + size = data_object.replicas[0].size + return Response( stream_with_context(read_file_chunks(path)), headers={ 'Content-Disposition': f'attachment; filename={filename}', + 'Content-Length': f'{size}', 'Content-Type': 'application/octet' } ) diff --git a/research/research.py b/research/research.py index 0d91ade3..7e6575fa 100644 --- a/research/research.py +++ b/research/research.py @@ -42,12 +42,11 @@ def index() -> Response: def download() -> Response: path = '/' + g.irods.zone + '/home' + request.args.get('filepath') filename = path.rsplit('/', 1)[1] - READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE - def read_file_chunks(path: str) -> Iterator[bytes]: - obj = g.irods.data_objects.get(path) + def read_file_chunks(data_object) -> Iterator[bytes]: + READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE try: - with obj.open('r') as fd: + with data_object.open('r') as fd: while True: buf = fd.read(READ_BUFFER_SIZE) if buf: @@ -61,10 +60,14 @@ def read_file_chunks(path: str) -> Iterator[bytes]: abort(500) if g.irods.data_objects.exists(path): + data_object = g.irods.data_objects.get(path) + size = data_object.replicas[0].size + return Response( stream_with_context(read_file_chunks(path)), headers={ 'Content-Disposition': f'attachment; filename={filename}', + 'Content-Length': f'{size}', 'Content-Type': 'application/octet' } ) diff --git a/vault/vault.py b/vault/vault.py index 26977a64..c0229349 100644 --- a/vault/vault.py +++ b/vault/vault.py @@ -36,12 +36,11 @@ def index() -> Response: def download() -> Response: path = '/' + g.irods.zone + '/home' + request.args.get('filepath') filename = path.rsplit('/', 1)[1] - READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE - def read_file_chunks(path: str) -> Iterator[bytes]: - obj = g.irods.data_objects.get(path) + def read_file_chunks(data_object) -> Iterator[bytes]: + READ_BUFFER_SIZE = 1024 * io.DEFAULT_BUFFER_SIZE try: - with obj.open('r') as fd: + with data_object.open('r') as fd: while True: buf = fd.read(READ_BUFFER_SIZE) if buf: @@ -55,10 +54,14 @@ def read_file_chunks(path: str) -> Iterator[bytes]: abort(500) if g.irods.data_objects.exists(path): + data_object = g.irods.data_objects.get(path) + size = data_object.replicas[0].size + return Response( stream_with_context(read_file_chunks(path)), headers={ 'Content-Disposition': f'attachment; filename={filename}', + 'Content-Length': f'{size}', 'Content-Type': 'application/octet' } )