fix: clear() deletes chapter/subtitle files + persist subtitle_files

Bug fixes from full codebase review:
- clear() with DELETE_FILE_ON_TRASHCAN now also deletes chapter_files
  and subtitle_files, not just the primary filename
- _PERSISTED_DOWNLOAD_FIELDS now includes 'subtitle_files' so subtitle
  file info survives app restart

Tests:
- Added test_subtitle_lang_list_with_region_variant_input (zh-Hans)
- Added test_subtitle_lang_list_dedup_region_variant (pt-BR)
This commit is contained in:
TonyBlu 2026-05-25 21:51:37 +08:00
parent 6d890021a6
commit 33e6ae3388
2 changed files with 35 additions and 5 deletions

View file

@ -134,6 +134,23 @@ class DlFormatsTests(unittest.TestCase):
self.assertEqual(_normalize_subtitle_language(""), "en")
self.assertEqual(_normalize_subtitle_language(" "), "en")
def test_subtitle_lang_list_with_region_variant_input(self):
"""zh-Hans should expand to include zh base and zh-CN, zh-TW, etc."""
from app.dl_formats import _subtitle_lang_list
result = _subtitle_lang_list("zh-Hans", include_orig=True)
self.assertEqual(result[0], "zh-Hans") # original input first
self.assertIn("zh", result) # base language fallback
self.assertIn("zh-CN", result) # regional variant
self.assertIn("zh-orig", result) # orig suffix for auto-captions
def test_subtitle_lang_list_dedup_region_variant(self):
"""pt-BR should not duplicate pt-BR in the variant expansion."""
from app.dl_formats import _subtitle_lang_list
result = _subtitle_lang_list("pt-BR", include_orig=True)
self.assertEqual(result.count("pt-BR"), 1) # no duplicate
self.assertIn("pt", result) # base language fallback
self.assertIn("pt-PT", result) # other regional variant
if __name__ == "__main__":
unittest.main()

View file

@ -328,6 +328,7 @@ _PERSISTED_DOWNLOAD_FIELDS = (
"filename",
"size",
"chapter_files",
"subtitle_files",
)
@ -1494,11 +1495,23 @@ class DownloadQueue:
continue
if self.config.DELETE_FILE_ON_TRASHCAN:
dl = self.done.get(id)
try:
dldirectory, _ = self.__calc_download_path(dl.info.download_type, dl.info.folder)
os.remove(os.path.join(dldirectory, dl.info.filename))
except Exception as e:
log.warning(f'deleting file for download {id} failed with error message {e!r}')
dldirectory, _ = self.__calc_download_path(dl.info.download_type, dl.info.folder)
# Delete the primary downloaded file
files_to_delete = [dl.info.filename]
# Also delete chapter files and subtitle files
for cf in getattr(dl.info, 'chapter_files', []) or []:
if isinstance(cf, dict) and cf.get('filename'):
files_to_delete.append(cf['filename'])
for sf in getattr(dl.info, 'subtitle_files', []) or []:
if isinstance(sf, dict) and sf.get('filename'):
files_to_delete.append(sf['filename'])
for filename in files_to_delete:
if not filename:
continue
try:
os.remove(os.path.join(dldirectory, filename))
except Exception as e:
log.warning(f'deleting file {filename} for download {id} failed with error message {e!r}')
self.done.delete(id)
await self.notifier.cleared(id)
return {'status': 'ok'}