Delete empty files after download errors

It's debatable whether we should keep non-empty files, but surely empty
ones are useless.

TODO: Add changelog entry
TODO: Pick to master?
This commit is contained in:
Florian Bruhin 2022-05-03 18:46:22 +02:00
parent 4812c8e30e
commit 59922dfe4e
2 changed files with 14 additions and 0 deletions

View File

@ -131,11 +131,22 @@ class DownloadItem(downloads.AbstractDownloadItem):
self._reply.deleteLater()
self._reply = None
if self.fileobj is not None:
pos = self.fileobj.tell()
log.downloads.debug(f"File position at error: {pos}")
try:
self.fileobj.close()
except OSError:
log.downloads.exception("Error while closing file object")
if pos == 0:
# Emtpy remaining file
filename = self._get_open_filename()
log.downloads.debug(f"Removing empty file at {filename}")
try:
os.remove(filename)
except OSError:
log.downloads.exception("Error while removing empty file")
def _init_reply(self, reply):
"""Set a new reply and connect its signals.

View File

@ -578,11 +578,13 @@ Feature: Downloading things from a website.
When I set downloads.location.prompt to false
And I run :download http://localhost:(port)/redirect/12 --dest redirection
Then the error "Download error: Too many redirects" should be shown
And the downloaded file redirection should not exist
Scenario: Downloading with redirect to itself
When I set downloads.location.prompt to false
And I run :download http://localhost:(port)/redirect-self
Then the error "Download error: Too many redirects" should be shown
And the downloaded file redirect-self should not exist
Scenario: Downloading with absolute redirect
When I set downloads.location.prompt to false
@ -603,6 +605,7 @@ Feature: Downloading things from a website.
# First error is due to the load-insecurely value above
Then the error "Certificate error: The certificate is self-signed, and untrusted" should be shown
And the error "Download error: Insecure redirect" should be shown
And the downloaded file download.bin should not exist
## Other