Skip to content

Commit

Permalink
Unquote the response.url to obtain a better filename #26
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 8, 2024
1 parent e4d1ed4 commit 86146db
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions component_catalog/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ def test_collect_package_data(self, mock_get):
with self.assertRaisesMessage(DataCollectionException, expected_message):
collect_package_data("ftp://ftp.denx.de/pub/u-boot/u-boot-2017.11.tar.bz2")

download_url = "http://domain.com/a.zip;<params>?<query>#<fragment>"
download_url = "http://domain.com/a%20b.zip;<params>?<query>#<fragment>"

default_max_length = download.CONTENT_MAX_LENGTH
download.CONTENT_MAX_LENGTH = 0
Expand All @@ -2233,8 +2233,8 @@ def test_collect_package_data(self, mock_get):
url=download_url,
)
expected_data = {
"download_url": "http://domain.com/a.zip;<params>?<query>#<fragment>",
"filename": "a.zip",
"download_url": download_url,
"filename": "a b.zip",
"size": 1,
"sha1": "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
"md5": "93b885adfe0da089cdf634904fd59f71",
Expand Down Expand Up @@ -2264,7 +2264,7 @@ def test_collect_package_data(self, mock_get):
}
mock_get.return_value = mock.Mock(content=b"\x00", headers=headers, status_code=200)
expected_data = {
"download_url": "http://domain.com/a.zip;<params>?<query>#<fragment>",
"download_url": download_url,
"filename": "another_name.zip",
"size": 1,
"sha1": "5ba93c9db0cff93f52b521d7420e43f6eda2784f",
Expand Down
9 changes: 6 additions & 3 deletions dejacode_toolkit/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cgi
import socket
from pathlib import Path
from urllib.parse import unquote
from urllib.parse import urlparse

from django.template.defaultfilters import filesizeformat
Expand Down Expand Up @@ -56,9 +57,11 @@ def collect_package_data(url):
content_disposition = response.headers.get("content-disposition", "")
_, params = cgi.parse_header(content_disposition)

# Using ``response.url`` in place of provided ``url`` arg since the former
# will be more accurate in case of HTTP redirect.
filename = params.get("filename") or Path(urlparse(response.url).path).name
filename = params.get("filename")
if not filename:
# Using ``response.url`` in place of provided ``url`` arg since the former
# will be more accurate in case of HTTP redirect.
filename = unquote(Path(urlparse(response.url).path).name)

package_data = {
"download_url": url,
Expand Down

0 comments on commit 86146db

Please sign in to comment.