Skip to content

Commit 5347da6

Browse files
committed
Properly infer the file extension from the URL
1 parent 3921fbb commit 5347da6

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

isic_cli/io/http.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import shutil
66
from tempfile import NamedTemporaryFile
77
from typing import TYPE_CHECKING
8+
from pathlib import PurePosixPath
9+
from urllib.parse import urlparse
810

911
from more_itertools import chunked
1012
from requests.exceptions import ChunkedEncodingError, ConnectionError
@@ -126,7 +128,14 @@ def get_license(session: IsicCliSession, license_type: str) -> str:
126128
before_sleep=before_sleep_log(logger, logging.DEBUG),
127129
)
128130
def download_image(image: dict, to: Path, progress, task) -> None:
129-
dest_path = to / f'{image["isic_id"]}.jpg'
131+
url = image["files"]["full"]["url"]
132+
parsed_url = urlparse(url)
133+
path = parsed_url.path
134+
# defaulting to jpg is simply a convenience for development where the images
135+
# are extension-less since they come from a synthetic image generator.
136+
extension = PurePosixPath(path).suffix.lstrip('.') or "jpg"
137+
138+
dest_path = to / f'{image["isic_id"]}.{extension}'
130139

131140
# Avoid re downloading the image if one of the same name/size exists. This is a decent
132141
# enough proxy for detecting file differences without going through a hashing mechanism.

0 commit comments

Comments
 (0)