"src/git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "b62d9a1fdc0910bb864340b7bc29e86f6aa31d47"
Unverified Commit 16af6671 authored by Philip Meier's avatar Philip Meier Committed by GitHub
Browse files

improve error handling for GDrive downloads (#5704)

* improve error handling for GDrive downloads

* perform HTML check regardless of MD5 check
parent ac56f52e
import bz2 import bz2
import contextlib
import gzip import gzip
import hashlib import hashlib
import itertools import itertools
...@@ -262,6 +263,26 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[ ...@@ -262,6 +263,26 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[
_save_response_content(content, fpath) _save_response_content(content, fpath)
# In case we deal with an unhandled GDrive API response, the file should be smaller than 10kB and contain only text
if os.stat(fpath).st_size < 10 * 1024:
with contextlib.suppress(UnicodeDecodeError), open(fpath) as fh:
text = fh.read()
# Regular expression to detect HTML. Copied from https://stackoverflow.com/a/70585604
if re.search(r"</?\s*[a-z-][^>]*\s*>|(&(?:[\w\d]+|#\d+|#x[a-f\d]+);)", text):
warnings.warn(
f"We detected some HTML elements in the downloaded file. "
f"This most likely means that the download triggered an unhandled API response by GDrive. "
f"Please report this to torchvision at https://github.com/pytorch/vision/issues including "
f"the response:\n\n{text}"
)
if md5 and not check_md5(fpath, md5):
raise RuntimeError(
f"The MD5 checksum of the download file {fpath} does not match the one on record."
f"Please delete the file and try again. "
f"If the issue persists, please report this to torchvision at https://github.com/pytorch/vision/issues."
)
def _extract_tar(from_path: str, to_path: str, compression: Optional[str]) -> None: def _extract_tar(from_path: str, to_path: str, compression: Optional[str]) -> None:
with tarfile.open(from_path, f"r:{compression[1:]}" if compression else "r") as tar: with tarfile.open(from_path, f"r:{compression[1:]}" if compression else "r") as tar:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment