in container-filetransfer/cloud-transfer.py [0:0]
def download_http(url, path):
def filename_from_content_disposition(requests_response):
"""
Parses the RFC6266 content-disposition header to determine the server-
suggested filename for content.
"""
components = urlparse(requests_response.url)
head, tail = posixpath.split(components.path)
expected_extension = posixpath.splitext(tail)[1]
cd = rfc6266.parse_requests_response(requests_response)
return cd.filename_sanitized(expected_extension.lstrip('.') or 'dat')
def determine_filename_for_requests(requests_response):
"""
Determines filename from a response from the 'requests' module. Prefers
content-disposition when available, falling back to parsing the URL.
"""
filename = filename_from_content_disposition(requests_response)
if not filename:
filename = filename_from_url(requests_response.url)
return filename
if urlparse(url).scheme not in ['http', 'https']:
logger.warning(f"[http] Skipping download of '{url}' because of incorrect scheme")
return False
with requests.get(url, allow_redirects=True, stream=True) as response:
response.raw.read = functools.partial(response.raw.read, decode_content=True)
if os.path.isdir(path):
local_file = os.path.join(path, determine_filename_for_requests(response))
else:
local_file = path
with open(local_file, 'wb') as fh:
shutil.copyfileobj(response.raw, fh)