Fix incomplete cleanup of temporary files
Fix mistakenly deleting previously saved intermediate files during post-processing cleanup
This commit is contained in:
parent
c61542f852
commit
c4f3468b6c
1 changed files with 45 additions and 7 deletions
52
app/ytdl.py
52
app/ytdl.py
|
|
@ -52,8 +52,8 @@ class Download:
|
||||||
def __init__(self, download_dir, temp_dir, output_template, output_template_chapter, quality, format, ytdl_opts, info):
|
def __init__(self, download_dir, temp_dir, output_template, output_template_chapter, quality, format, ytdl_opts, info):
|
||||||
self.download_dir = download_dir
|
self.download_dir = download_dir
|
||||||
self.temp_dir = temp_dir
|
self.temp_dir = temp_dir
|
||||||
self.output_template = output_template
|
self.output_template = self._add_format_identifier(format, output_template)
|
||||||
self.output_template_chapter = output_template_chapter
|
self.output_template_chapter = self._add_format_identifier(format, output_template_chapter)
|
||||||
self.format = get_format(format, quality)
|
self.format = get_format(format, quality)
|
||||||
self.ytdl_opts = get_opts(format, quality, ytdl_opts)
|
self.ytdl_opts = get_opts(format, quality, ytdl_opts)
|
||||||
if "impersonate" in self.ytdl_opts:
|
if "impersonate" in self.ytdl_opts:
|
||||||
|
|
@ -140,6 +140,8 @@ class Download:
|
||||||
if self.status_queue is not None:
|
if self.status_queue is not None:
|
||||||
self.status_queue.put(None)
|
self.status_queue.put(None)
|
||||||
|
|
||||||
|
self._delete_format_identifier()
|
||||||
|
|
||||||
def running(self):
|
def running(self):
|
||||||
try:
|
try:
|
||||||
return self.proc is not None and self.proc.is_alive()
|
return self.proc is not None and self.proc.is_alive()
|
||||||
|
|
@ -176,6 +178,46 @@ class Download:
|
||||||
log.info(f"Updating status for {self.info.title}: {status}")
|
log.info(f"Updating status for {self.info.title}: {status}")
|
||||||
await self.notifier.updated(self.info)
|
await self.notifier.updated(self.info)
|
||||||
|
|
||||||
|
def _add_format_identifier(self, identifier, template):
|
||||||
|
# Preventing the post-processing of YT-DLP from deleting the intermediate file which was download before.
|
||||||
|
return f'{identifier}_{template}'
|
||||||
|
|
||||||
|
def _delete_format_identifier(self):
|
||||||
|
# Delete the identifier in the file name after the post-processing is complete.
|
||||||
|
if self.canceled or self.info.status != 'finished' or not hasattr(self.info,'filename'):
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
filename = re.sub(r'^\w+_', '', self.info.filename)
|
||||||
|
filepath_idt = os.path.join(self.download_dir, self.info.filename)
|
||||||
|
filepath = os.path.join(self.download_dir, filename)
|
||||||
|
if os.path.exists(filepath):
|
||||||
|
os.remove(filepath)
|
||||||
|
os.rename(filepath_idt, filepath)
|
||||||
|
log.info(f"Renamed file '{filepath_idt}' to '{filepath}'")
|
||||||
|
except PermissionError as e:
|
||||||
|
log.warning(f"Error deleting old file '{filepath}': {e} ")
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
log.warning(f"Error renaming file '{filepath_idt}': {e} ")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.info.filename = filename
|
||||||
|
|
||||||
|
def delete_tmpfile(self):
|
||||||
|
if not self.tmpfilename:
|
||||||
|
return
|
||||||
|
tmpfilename = self.tmpfilename.rsplit('.')[0]
|
||||||
|
def is_tmpfile(filename):
|
||||||
|
return filename.startswith(tmpfilename)
|
||||||
|
|
||||||
|
tmpfiles = filter(is_tmpfile ,os.listdir(self.download_dir))
|
||||||
|
try:
|
||||||
|
for tmpfile in tmpfiles:
|
||||||
|
os.remove(tmpfile)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
class PersistentQueue:
|
class PersistentQueue:
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
pdir = os.path.dirname(path)
|
pdir = os.path.dirname(path)
|
||||||
|
|
@ -279,11 +321,7 @@ class DownloadQueue:
|
||||||
|
|
||||||
def _post_download_cleanup(self, download):
|
def _post_download_cleanup(self, download):
|
||||||
if download.info.status != 'finished':
|
if download.info.status != 'finished':
|
||||||
if download.tmpfilename and os.path.isfile(download.tmpfilename):
|
download.delete_tmpfile()
|
||||||
try:
|
|
||||||
os.remove(download.tmpfilename)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
download.info.status = 'error'
|
download.info.status = 'error'
|
||||||
download.close()
|
download.close()
|
||||||
if self.queue.exists(download.info.id):
|
if self.queue.exists(download.info.id):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue