always delete incompletely downloaded files
This commit is contained in:
@@ -19,16 +19,14 @@ class BasicDownloader():
|
||||
def download(self, url, pathfmt):
|
||||
"""Download the resource at 'url' and write it to a file-like object"""
|
||||
try:
|
||||
return self.download_impl(url, pathfmt)
|
||||
except:
|
||||
# remove file if download failed
|
||||
try:
|
||||
if self.downloading:
|
||||
os.unlink(pathfmt.realpath)
|
||||
except (AttributeError, FileNotFoundError):
|
||||
pass
|
||||
raise
|
||||
self.download_impl(url, pathfmt)
|
||||
finally:
|
||||
# remove file from incomplete downloads
|
||||
if self.downloading:
|
||||
try:
|
||||
os.remove(pathfmt.realpath)
|
||||
except (OSError, AttributeError):
|
||||
pass
|
||||
|
||||
def download_impl(self, url, file_handle):
|
||||
"""Actual implementaion of the download process"""
|
||||
pass
|
||||
|
||||
@@ -72,14 +72,14 @@ class Downloader(BasicDownloader):
|
||||
# everything ok -- proceed to download
|
||||
self.out.start(pathfmt.path)
|
||||
self.downloading = True
|
||||
with pathfmt.open() as file:
|
||||
try:
|
||||
for data in response.iter_content(None):
|
||||
try:
|
||||
with pathfmt.open() as file:
|
||||
for data in response.iter_content(16384):
|
||||
file.write(data)
|
||||
except rexcepts.RequestException as exception:
|
||||
msg = exception
|
||||
response.close()
|
||||
continue
|
||||
except rexcepts.RequestException as exception:
|
||||
msg = exception
|
||||
response.close()
|
||||
continue
|
||||
self.downloading = False
|
||||
self.out.success(pathfmt.path, tries)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user