Skip to content

Commit db5cfa3

Browse files
authored
Merge pull request #94 from ImageMarkup/infer-file-extensions
Properly infer the file extension from the URL
2 parents 3921fbb + 34ccca6 commit db5cfa3

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
@@ -2,9 +2,11 @@
22

33
import logging
44
import os
5+
from pathlib import PurePosixPath
56
import shutil
67
from tempfile import NamedTemporaryFile
78
from typing import TYPE_CHECKING
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)