Repository: Project-MONAI/MONAI
File: monai/apps/utils.py (Python)

Code before:
    progress: bool = True,
) -> None:
    """
    Download file from URL and extract it to the output directory.

    Args:
        url: source URL link to download file.
        filepath: the file path of the downloaded compressed file.
            use this option to keep the directly downloaded compressed file, to avoid further repeated downloads.
        output_dir: target directory to save extracted files.
            default is the current directory.
        hash_val: expected hash value to validate the downloaded file.
            if None, skip hash validation.
        hash_type: 'md5' or 'sha1', defaults to 'md5'.
        file_type: string of file type for decompressing. Leave it empty to infer the type from url's base file name.
        has_base: whether the extracted files have a base folder. This flag is used when checking if the existing
            folder is a result of `extractall`, if it is, the extraction is skipped. For example, if A.zip is unzipped
            to folder structure `A/*.png`, this flag should be True; if B.zip is unzipped to `*.png`, this flag should
            be False.
        progress: whether to display progress bar.
    """
    urlFilenameExtension = ''.join(Path(".", _basename(url)).resolve().suffixes)
    if filepath:
        FilepathExtenstion = ''.join(Path(".", _basename(filepath)).resolve().suffixes)
        if urlFilenameExtension != FilepathExtenstion:
            raise NotImplementedError(
                f'The file types do not match: url={urlFilenameExtension}, but filepath={FilepathExtenstion}'
            )
    with tempfile.TemporaryDirectory() as tmp_dir:
        if filepath:
            filename = filepath
        else:
            filename = Path(tmp_dir, _basename(url)).resolve()
        download_url(url=url, filepath=filename, hash_val=hash_val, hash_type=hash_type, progress=progress)
        extractall(filepath=filename, output_dir=output_dir, file_type=file_type, has_base=has_base)


Review comment (refactor):
In my opinion, the issue [here](https://github.com/Project-MONAI/MONAI/issues/5463) is that if filepath can directly get the name of the downloaded file, the `download_and_extract` function would work as expected, rather than only raising an error at this point.

Code after:
        response = requests.head(data_url, allow_redirects=True)
        content_disposition = response.headers.get("Content-Disposition")
        if content_disposition:
            filename = re.findall('filename="?([^";]+)"?', content_disposition)
            if filename:
                return str(filename[0])
        if "drive.google.com" in data_url:
            response = requests.get(data_url)
            if "text/html" in response.headers.get("Content-Type", ""):
                soup = BeautifulSoup(response.text, "html.parser")
                filename_div = soup.find("span", {"class": "uc-name-size"})
                if filename_div:
                    return str(filename_div.find("a").text)
        return _basename(data_url)
    except Exception as e:
        raise Exception(f"Error processing URL: {e}") from e


def download_and_extract(
    url: str,
    filepath: PathLike = "",
    output_dir: PathLike = ".",
    hash_val: str | None = None,
    hash_type: str = "md5",
    file_type: str = "",
    has_base: bool = True,
    progress: bool = True,
) -> None:
    """
    Download file from URL and extract it to the output directory.

    Args:
        url: source URL link to download file.
        filepath: the file path of the downloaded compressed file.
            use this option to keep the directly downloaded compressed file, to avoid further repeated downloads.
        output_dir: target directory to save extracted files.
            default is the current directory.
        hash_val: expected hash value to validate the downloaded file.
            if None, skip hash validation.
        hash_type: 'md5' or 'sha1', defaults to 'md5'.
        file_type: string of file type for decompressing. Leave it empty to infer the type from url's base file name.
        has_base: whether the extracted files have a base folder. This flag is used when checking if the existing
            folder is a result of `extractall`, if it is, the extraction is skipped. For example, if A.zip is unzipped
            to folder structure `A/*.png`, this flag should be True; if B.zip is unzipped to `*.png`, this flag should
            be False.
        progress: whether to display progress bar.
    """
    url_filename_ext = "".join(Path(get_filename_from_url(url)).suffixes)
    filepath_ext = "".join(Path(_basename(filepath)).suffixes)
    if filepath not in ["", "."]:
        if filepath_ext == "":