Skip to content

Commit

Permalink
Unquote the filename from URL in fetch_http
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 11, 2024
1 parent 2eb587d commit 7f3ad87
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
10 changes: 7 additions & 3 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,10 +1514,14 @@ def fetch(self):
raise Exception("No `download_url` value to be fetched.")

downloaded = fetch_url(url=self.download_url)
self.filename = downloaded.filename
self.save()

destination = self.project.move_input_from(downloaded.path)

# Force a commit to the database to ensure the file on disk is not rendered
# as "manually uploaded" in the UI.
with transaction.atomic():
self.filename = downloaded.filename
self.save()

return destination


Expand Down
3 changes: 2 additions & 1 deletion scanpipe/pipes/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import tempfile
from collections import namedtuple
from pathlib import Path
from urllib.parse import unquote
from urllib.parse import urlparse

import requests
Expand Down Expand Up @@ -85,7 +86,7 @@ def fetch_http(uri, to=None):
if not filename:
# Using `response.url` in place of provided `Scan.uri` since the former
# will be more accurate in case of HTTP redirect.
filename = Path(urlparse(response.url).path).name
filename = unquote(Path(urlparse(response.url).path).name)

download_directory = to or tempfile.mkdtemp()
output_file = Path(download_directory, filename)
Expand Down
6 changes: 3 additions & 3 deletions scanpipe/tests/pipes/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def test_scanpipe_pipes_fetch_http(self, mock_get):
downloaded_file = fetch.fetch_http(url)
self.assertTrue(Path(downloaded_file.directory, "filename.zip").exists())

redirect_url = "https://example.com/redirect.zip"
url_with_spaces = "https://example.com/space%20in%20name.zip"
mock_get.return_value = mock.Mock(
content=b"\x00", headers={}, status_code=200, url=redirect_url
content=b"\x00", headers={}, status_code=200, url=url_with_spaces
)
downloaded_file = fetch.fetch_http(url)
self.assertTrue(Path(downloaded_file.directory, "redirect.zip").exists())
self.assertTrue(Path(downloaded_file.directory, "space in name.zip").exists())

headers = {
"content-disposition": 'attachment; filename="another_name.zip"',
Expand Down

0 comments on commit 7f3ad87

Please sign in to comment.