Skip to content

Commit

Permalink
Set Content-Length header field in the download response.
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Jul 20, 2023
1 parent d452aa0 commit 59dac9c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
13 changes: 8 additions & 5 deletions deposit/deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
}
)
Expand Down
13 changes: 8 additions & 5 deletions research/research.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
}
)
Expand Down
13 changes: 8 additions & 5 deletions vault/vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
}
)
Expand Down

0 comments on commit 59dac9c

Please sign in to comment.