Skip to content

Commit

Permalink
Add handling for requests to download unavilable formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccacremona committed Dec 3, 2024
1 parent 4c5137d commit 432782b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 12 additions & 0 deletions perma_web/api/tests/test_link_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def test_public_download_wacz(self, get_wacz):
def test_public_download_unsupported_format(self):
self.rejected_get(f"{self.public_link_download_url}?file_format=asdf", expected_status_code=400)

@patch('perma.models.Link.get_warc', autospec=True)
def test_download_nonexistent_warc(self, get_warc):
get_warc.side_effect = RuntimeError
self.rejected_get(self.public_link_download_url, expected_status_code=404)
self.assertEqual(get_warc.call_count, 1)

@patch('perma.models.Link.get_wacz', autospec=True)
def test_download_nonexistent_wacz(self, get_wacz):
get_wacz.side_effect = RuntimeError
self.rejected_get(f"{self.public_link_download_url}?file_format=wacz", expected_status_code=404)
self.assertEqual(get_wacz.call_count, 1)

def test_private_download_at_public_url(self):
self.rejected_get(self.public_link_download_url_for_private_link, expected_status_code=404)

Expand Down
19 changes: 12 additions & 7 deletions perma_web/perma/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,18 @@ def stream_archive(link, stream=True, file_format='warc'):
if link.user_deleted or not link.can_play_back():
raise Http404

match file_format:
case 'warc':
return get_warc_stream(link, stream)
case 'wacz':
return get_wacz_stream(link, stream)
case _:
raise NotImplementedError("Unsupported file format.")
try:
match file_format:
case 'warc':
return get_warc_stream(link, stream)
case 'wacz':
return get_wacz_stream(link, stream)
case _:
raise NotImplementedError("Unsupported file format.")
except RuntimeError:
# If the requested format is not available, return 404
# just like with deleted and failed Perma Links
raise Http404


def stream_archive_if_permissible(link, user, stream=True, file_format='warc'):
Expand Down

0 comments on commit 432782b

Please sign in to comment.