Skip to content

Commit a9a0171

Browse files
committed
Enhance download_and_extract
Signed-off-by: jerome_Hsieh <[email protected]>
1 parent 0441871 commit a9a0171

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

monai/apps/utils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,18 @@ def download_and_extract(
327327
be False.
328328
progress: whether to display progress bar.
329329
"""
330-
urlFilenameExtension = ''.join(Path(".", _basename(url)).resolve().suffixes)
331-
if filepath:
332-
FilepathExtenstion = ''.join(Path(".", _basename(filepath)).resolve().suffixes)
333-
if urlFilenameExtension != FilepathExtenstion:
334-
raise NotImplementedError(
335-
f'The file types do not match: url={urlFilenameExtension}, but filepath={FilepathExtenstion}'
330+
url_filename_ext = "".join(Path(".", _basename(url)).resolve().suffixes)
331+
filepath_ext = "".join(Path(".", _basename(filepath)).resolve().suffixes)
332+
if filepath not in ["", "."]:
333+
if filepath_ext == "":
334+
new_filepath = filepath + url_filename_ext
335+
logger.warning(
336+
f"filepath={filepath}, which missing file extension. Auto-appending extension to: {new_filepath}"
336337
)
338+
filepath = new_filepath
339+
if filepath_ext and filepath_ext != url_filename_ext:
340+
logger.warning(f"Expected extension {url_filename_ext}, but get {filepath_ext}, may cause unexpected errors!")
337341
with tempfile.TemporaryDirectory() as tmp_dir:
338-
if filepath:
339-
filename = filepath
340-
else:
341-
filename = Path(tmp_dir, _basename(url)).resolve()
342+
filename = filepath or Path(tmp_dir, _basename(url)).resolve()
342343
download_url(url=url, filepath=filename, hash_val=hash_val, hash_type=hash_type, progress=progress)
343344
extractall(filepath=filename, output_dir=output_dir, file_type=file_type, has_base=has_base)

0 commit comments

Comments
 (0)