MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / get_filename_from_url

Function get_filename_from_url

monai/apps/utils.py:358–378  ·  view source on GitHub ↗

Get the filename from the URL link.

(data_url: str)

Source from the content-addressed store, hash-verified

356
357
358def get_filename_from_url(data_url: str) -> str:
359 """
360 Get the filename from the URL link.
361 """
362 try:
363 response = requests.head(data_url, allow_redirects=True)
364 content_disposition = response.headers.get("Content-Disposition")
365 if content_disposition:
366 filename = re.findall('filename="?([^";]+)"?', content_disposition)
367 if filename:
368 return str(filename[0])
369 if "drive.google.com" in data_url:
370 response = requests.get(data_url)
371 if "text/html" in response.headers.get("Content-Type", ""):
372 soup = BeautifulSoup(response.text, "html.parser")
373 filename_div = soup.find("span", {"class": "uc-name-size"})
374 if filename_div:
375 return str(filename_div.find("a").text)
376 return _basename(data_url)
377 except Exception as e:
378 raise Exception(f"Error processing URL: {e}") from e
379
380
381def download_and_extract(

Callers 1

download_and_extractFunction · 0.85

Calls 2

_basenameFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…