Skip to content

resolve redirection for HTTP resources #5151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions torchvision/prototype/datasets/utils/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
extract_archive,
_decompress,
download_file_from_google_drive,
_get_redirect_url,
_get_google_drive_file_id,
)


Expand Down Expand Up @@ -134,9 +136,41 @@ def __init__(
super().__init__(file_name=file_name or pathlib.Path(urlparse(url).path).name, **kwargs)
self.url = url
self.mirrors = mirrors
self._resolved = False

def resolve(self) -> OnlineResource:
if self._resolved:
return self

redirect_url = _get_redirect_url(self.url)
if redirect_url == self.url:
self._resolved = True
return self

meta = {
attr.lstrip("_"): getattr(self, attr)
for attr in (
"file_name",
"sha256",
"_preprocess",
"_loader",
)
}

gdrive_id = _get_google_drive_file_id(redirect_url)
if gdrive_id:
return GDriveResource(gdrive_id, **meta)
Comment on lines +160 to +162
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have current cases where an HTTP resource redirects to a Google drive?

Copy link
Collaborator Author

@pmeier pmeier Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It is one of the datasets not yet ported to prototype. I've also hit this while doing #5154.


http_resource = HttpResource(redirect_url, **meta)
http_resource._resolved = True
return http_resource

def _download(self, root: pathlib.Path) -> None:
if not self._resolved:
return self.resolve()._download(root)

for url in itertools.chain((self.url,), self.mirrors):

try:
download_url(url, str(root), filename=self.file_name, md5=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your first comment you wrote

This is far from perfect, since the mirrors do not get resolved

Could we instead do the redirection here and handle the mirrors?

Copy link
Collaborator Author

@pmeier pmeier Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but that would make stuff more complicated. Given that the plan is to refactor this anyway with iopath and we currently only have a single datasets that has official mirrors, I wouldn't sweat it at the moment.

# TODO: make this more precise
Expand Down