From 59dac9c2d79b805ecf598a85323f1a91af564f13 Mon Sep 17 00:00:00 2001 From: Lazlo Westerhof Date: Thu, 20 Jul 2023 13:25:57 +0200 Subject: [PATCH] Set Content-Length header field in the download response. --- deposit/deposit.py | 13 ++++++++----- research/research.py | 13 ++++++++----- vault/vault.py | 13 ++++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/deposit/deposit.py b/deposit/deposit.py index 5e687bb4..d89836bc 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)), + read_file_chunks(data_object), 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..fa5cc769 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)), + read_file_chunks(data_object), 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..088b1ee1 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)), + read_file_chunks(data_object), headers={ 'Content-Disposition': f'attachment; filename={filename}', + 'Content-Length': f'{size}', 'Content-Type': 'application/octet' } )