Skip to content

Commit

Permalink
Raise exception when trying to download from private record
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Nov 2, 2023
1 parent 20c08b8 commit 85dbb92
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/galaxy/files/sources/invenio.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,19 @@ def download_file_from_record(
if self._is_api_url(download_file_content_url):
# pass the token as a header only when using the API
headers = self._get_request_headers(user_context)
req = urllib.request.Request(download_file_content_url, headers=headers)
with urllib.request.urlopen(req, timeout=DEFAULT_SOCKET_TIMEOUT) as page:
f = open(file_path, "wb")
return stream_to_open_named_file(
page, f.fileno(), file_path, source_encoding=get_charset_from_http_headers(page.headers)
)
try:
req = urllib.request.Request(download_file_content_url, headers=headers)
with urllib.request.urlopen(req, timeout=DEFAULT_SOCKET_TIMEOUT) as page:
f = open(file_path, "wb")
return stream_to_open_named_file(
page, f.fileno(), file_path, source_encoding=get_charset_from_http_headers(page.headers)
)
except urllib.error.HTTPError as e:
# TODO: We can only download files from published records for now
if e.code in [401, 403, 404]:
raise Exception(
f"Cannot download file '{filename}' from record '{record_id}'. Please make sure the record exists and it is public."
)

def _get_download_file_url(self, record_id: str, filename: str, user_context: OptionalUserContext = None):
"""Get the URL to download a file from a record.
Expand Down

0 comments on commit 85dbb92

Please sign in to comment.