misc: download to tmp file, to avoid leftover 0-size file on error

This commit is contained in:
Dan Streetman 2021-09-16 19:24:00 -04:00
parent a1b56ac31f
commit a3ff68be5a

View File

@ -327,14 +327,17 @@ def download(src, dst, size=0):
(src, username, password) = extract_authentication(src) (src, username, password) = extract_authentication(src)
auth = (username, password) if username or password else None auth = (username, password) if username or password else None
try: with tempfile.TemporaryDirectory() as d:
with requests.get(src, stream=True, auth=auth) as fsrc, dst.open('wb') as fdst: tmpdst = Path(d) / 'dst'
fsrc.raise_for_status() try:
_download(fsrc, fdst, size) with requests.get(src, stream=True, auth=auth) as fsrc, tmpdst.open('wb') as fdst:
except requests.RequestException as e: fsrc.raise_for_status()
if e.response and e.response.status_code == 404: _download(fsrc, fdst, size)
raise NotFoundError(f'URL {src} not found') from e except requests.RequestException as e:
raise DownloadError(f'Could not download {src} to {dst}') from e if e.response and e.response.status_code == 404:
raise NotFoundError(f'URL {src} not found') from e
raise DownloadError(f'Could not download {src} to {dst}') from e
shutil.move(tmpdst, dst)
def _download(fsrc, fdst, size): def _download(fsrc, fdst, size):