Repository: Project-MONAI/MONAI

Issue:
windows download url
**Describe the bug**

[full log](https://github.com/Project-MONAI/MONAI/runs/5009920878?check_suite_focus=true)
```
======================================================================
ERROR: test_senet_shape_0 (tests.test_senet.TestPretrainedSENET)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 616, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\tmp5xni7oqq\\se_resnet50-ce0d4300.pthz1d2jq5ktmp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 802, in onerror
    _os.unlink(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\tmp5xni7oqq\\se_resnet50-ce0d4300.pthz1d2jq5ktmp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\site-packages\parameterized\parameterized.py", line 533, in standalone_func
    return func(*(a + p.args), **p.kwargs)
  File "D:\a\MONAI\MONAI\tests\test_senet.py", line 105, in test_senet_shape
    net = test_pretrained_networks(model, input_param, device)
  File "D:\a\MONAI\MONAI\tests\utils.py", line 115, in test_pretrained_networks
    return network(**input_param).to(device)
  File "D:\a\MONAI\MONAI\monai\networks\nets\senet.py", line 371, in __init__
    _load_state_dict(self, "se_resnet50", progress)
  File "D:\a\MONAI\MONAI\monai\networks\nets\senet.py", line 292, in _load_state_dict
    download_url(model_url["url"], filepath=model_url["filename"])
  File "D:\a\MONAI\MONAI\monai\apps\utils.py", line 202, in download_url
    shutil.move(f"{tmp_name}", f"{filepath}")  # copy the downloaded to a user-specified cache.
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 827, in __exit__
    self.cleanup()
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 831, in cleanup
    self._rmtree(self.name)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 813, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 618, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 805, in onerror
    cls._rmtree(path)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 813, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 599, in _rmtree_unsafe
    onerror(os.scandir, path, sys.exc_info())
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 596, in _rmtree_unsafe
    with os.scandir(path) as scandir_it:
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\tmp5xni7oqq\\se_resnet50-ce0d4300.pthz1d2jq5ktmp'
```


Discussion:

Resolving patch:
diff --git a/monai/apps/utils.py b/monai/apps/utils.py
--- a/monai/apps/utils.py
+++ b/monai/apps/utils.py
@@ -183,24 +183,26 @@ def download_url(
             )
         logger.info(f"File exists: {filepath}, skipped downloading.")
         return
-
-    with tempfile.TemporaryDirectory() as tmp_dir:
-        tmp_name = Path(tmp_dir, _basename(filepath))
-        if urlparse(url).netloc == "drive.google.com":
-            if not has_gdown:
-                raise RuntimeError("To download files from Google Drive, please install the gdown dependency.")
-            gdown.download(url, f"{tmp_name}", quiet=not progress)
-        else:
-            _download_with_progress(url, tmp_name, progress=progress)
-        if not tmp_name.exists():
-            raise RuntimeError(
-                f"Download of file from {url} to {filepath} failed due to network issue or denied permission."
-            )
-        file_dir = filepath.parent
-        if file_dir:
-            os.makedirs(file_dir, exist_ok=True)
-        shutil.move(f"{tmp_name}", f"{filepath}")  # copy the downloaded to a user-specified cache.
-        logger.info(f"Downloaded: {filepath}")
+    try:
+        with tempfile.TemporaryDirectory() as tmp_dir:
+            tmp_name = Path(tmp_dir, _basename(filepath))
+            if urlparse(url).netloc == "drive.google.com":
+                if not has_gdown:
+                    raise RuntimeError("To download files from Google Drive, please install the gdown dependency.")
+                gdown.download(url, f"{tmp_name}", quiet=not progress)
+            else:
+                _download_with_progress(url, tmp_name, progress=progress)
+            if not tmp_name.exists():
+                raise RuntimeError(
+                    f"Download of file from {url} to {filepath} failed due to network issue or denied permission."
+                )
+            file_dir = filepath.parent
+            if file_dir:
+                os.makedirs(file_dir, exist_ok=True)
+            shutil.move(f"{tmp_name}", f"{filepath}")  # copy the downloaded to a user-specified cache.
+    except (PermissionError, NotADirectoryError):  # project-monai/monai issue #3613 #3757 for windows
+        pass
+    logger.info(f"Downloaded: {filepath}")
     if not check_hash(filepath, hash_val, hash_type):
         raise RuntimeError(
             f"{hash_type} check of downloaded file failed: URL={url}, "


Test patch: