Skip to content

Commit 304ef40

Browse files
committed
Merge branch 'feature/legacy-redirect-2' into develop
[ENG-10136]
2 parents 682420c + f34a1b8 commit 304ef40

1 file changed

Lines changed: 29 additions & 47 deletions

File tree

mfr/providers/osf/provider.py

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -160,35 +160,11 @@ async def download(self):
160160

161161
return streams.ResponseStreamReader(response)
162162

163-
@staticmethod
164-
def _is_osf_download_route(url):
165-
"""True when ``url`` is OSF's persistent file download path ``/download/<id>/``."""
166-
path = urlparse(url).path or ''
167-
osf_download_path_re = re.compile(r'^/download/[^/]+', re.IGNORECASE)
168-
return bool(osf_download_path_re.match(path))
169-
170-
async def _fetch_redirect(self, url, headers):
171-
request = await self._make_request(
172-
'GET',
173-
url,
174-
allow_redirects=False,
175-
headers=headers,
176-
)
177-
status = request.status
178-
location = request.headers.get('Location') or request.headers.get('location')
179-
reason = request.reason
180-
await request.release()
181-
return location, status, reason
182-
183163
async def _fetch_download_url(self):
184164
"""Provider needs a WaterButler URL to download and get metadata. If ``url`` is already
185165
a WaterButler url, return that. If not, then the url points to an OSF endpoint that will
186166
redirect to WB. Issue a GET request against it, then return the WB url stored in the
187167
Location header.
188-
189-
First ``GET`` may return **301** with ``Location`` for legacy download route. If ``Location``
190-
points at OSF's ``/download/<id>/`` route, perform a second ``GET`` there and expect **302**
191-
to WaterButler. If ``Location`` is not that route (e.g. already WaterButler), use it as ``download_url``.
192168
"""
193169
if not self.download_url:
194170
# v1 Waterbutler url provided
@@ -197,33 +173,39 @@ async def _fetch_download_url(self):
197173
self.download_url = self.url
198174
self.metrics.add('download_url.orig_type', 'wb_v1')
199175
else:
200-
self.metrics.add('download_url.orig_type', 'osf')
201-
# make request to osf, store waterbutler download url
202-
headers = {'Content-Type': 'application/json'}
176+
normalized_url = None
177+
if re.compile(r'^/[a-z0-9]{5}/[^/]*', re.IGNORECASE).match(path):
178+
# In the past the OSF would redirect urls of the form https://osf.io/<file_guid>/ to
179+
# https://osf.io/download/<file_guid>/. It no longer does this, so MFR must detect
180+
# such urls and manually prepend /download/ to the path.
181+
self.metrics.add('download_url.orig_type', 'osf-preangular')
182+
temp_url = furl.furl(self.url)
183+
temp_url.path.segments = ['download'] + temp_url.path.segments
184+
normalized_url = temp_url.url
185+
else:
186+
self.metrics.add('download_url.orig_type', 'osf')
187+
normalized_url = self.url
188+
189+
# make request to osf, don't follow, store waterbutler download url
190+
request = await self._make_request(
191+
'GET',
192+
normalized_url,
193+
allow_redirects=False,
194+
headers={
195+
'Content-Type': 'application/json'
196+
}
197+
)
198+
await request.release()
203199

204-
location, status, reason = await self._fetch_redirect(self.url, headers)
205-
logger.debug(f'osf-download-resolver: url={self.url} status={status} location={location}')
206-
if status not in (301, 302) or not location:
200+
logger.debug(f'osf-download-resolver: request.status::{request.status}')
201+
if request.status != 302:
207202
raise exceptions.MetadataError(
208-
reason,
209-
metadata_url=self.url,
203+
request.reason,
204+
metadata_url=normalized_url,
210205
provider=self.NAME,
211-
code=status,
206+
code=request.status,
212207
)
213-
214-
if self._is_osf_download_route(location):
215-
location, status, reason = await self._fetch_redirect(location, headers)
216-
217-
logger.debug(f'osf-download-resolver follow /download/: url={location} status={status} location={location}')
218-
if status != 302 or not location:
219-
raise exceptions.MetadataError(
220-
reason,
221-
metadata_url=location,
222-
provider=self.NAME,
223-
code=status,
224-
)
225-
226-
self.download_url = location
208+
self.download_url = request.headers['location']
227209

228210
self.metrics.add('download_url.derived_url', str(self.download_url))
229211

0 commit comments

Comments
 (0)