diff --git a/gallery_dl/downloader/common.py b/gallery_dl/downloader/common.py index 35895cf4..8eed513a 100644 --- a/gallery_dl/downloader/common.py +++ b/gallery_dl/downloader/common.py @@ -15,16 +15,18 @@ class BasicDownloader(): max_tries = 5 - def download(self, url, path): - """Download the resource at 'url' and write it to a file given by 'path'""" - with open(path, "wb") as file: + def download(self, url, fileobj): + """Download the resource at 'url' and write it to a file-like object""" + try: + return self.download_impl(url, fileobj) + except: + # remove file if download failed try: - return self.download_impl(url, file) - except: - #remove file if download failed - file.close() - os.unlink(path) - raise + fileobj.close() + os.unlink(fileobj.name) + except AttributeError: + pass + raise def download_impl(self, url, file_handle): """Actual implementaion of the download process""" diff --git a/gallery_dl/jobs.py b/gallery_dl/jobs.py index b567e3b9..bcbc2da7 100644 --- a/gallery_dl/jobs.py +++ b/gallery_dl/jobs.py @@ -93,7 +93,8 @@ class DownloadJob(Job): return dlinstance = self.get_downloader(url) self.printer.start(path) - tries = dlinstance.download(url, path) + with open(path, "wb") as file: + tries = dlinstance.download(url, file) self.printer.success(path, tries) def set_directory(self, msg):