修复 RAR 字幕包下载识别

This commit is contained in:
jxxghp
2026-06-10 08:46:10 +08:00
parent 82694d2d8b
commit cba52c57e6
6 changed files with 224 additions and 20 deletions

View File

@@ -85,6 +85,20 @@ class _FakeSubtitleResponse:
"""
content = b"subtitle-content"
headers = {}
class _FakeSubtitleResponseWithHeader:
"""
模拟带下载文件名响应头的字幕 API 响应。
"""
content = b"archive-content"
headers = {
"content-disposition": (
'attachment; filename="Hypnosis_AKA_Saimin_(1999)_480i_JAPANESE_NTSC_DVD_REMUX_MPEG-2_DD_2.0-MeeSta.rar"'
)
}
def test_download_single_submits_download_added_to_background(monkeypatch):
@@ -176,6 +190,79 @@ def test_save_subtitle_response_creates_missing_temp_directory(monkeypatch, tmp_
assert storage_chain.uploaded_files
def test_save_subtitle_response_accepts_rar_filename_from_header(monkeypatch, tmp_path):
"""
PHP 下载链接应按响应头文件名识别 RAR 字幕压缩包,而不是按 URL 后缀误拒绝。
"""
storage_chain = _FakeSubtitleStorageChain()
temp_path = tmp_path / "temp"
extracted_dir = temp_path / "Hypnosis_AKA_Saimin_(1999)_480i_JAPANESE_NTSC_DVD_REMUX_MPEG-2_DD_2.0-MeeSta"
extracted_subtitle = extracted_dir / "Hypnosis_AKA_Saimin_(1999).srt"
def fake_unpack_archive(archive_file, extract_dir, archive_format=None):
assert archive_format == "rar"
assert archive_file.suffix == ".rar"
extract_dir.mkdir(parents=True, exist_ok=True)
extracted_subtitle.write_text("subtitle", encoding="utf-8")
monkeypatch.setattr(
download_module,
"settings",
SimpleNamespace(TEMP_PATH=temp_path, RMT_SUBEXT=settings.RMT_SUBEXT),
)
monkeypatch.setattr(download_module, "StorageChain", lambda: storage_chain)
monkeypatch.setattr(download_module.SystemUtils, "unpack_archive", fake_unpack_archive)
chain = DownloadChain.__new__(DownloadChain)
subtitle = SubtitleInfo(
title="Hypnosis",
enclosure="https://audiences.me/downloadsubs.php?torrentid=666519&subid=2195",
)
saved_files = chain._save_subtitle_response(
subtitle=subtitle,
response=_FakeSubtitleResponseWithHeader(),
target_dir=Path("/downloads"),
)
assert saved_files == ["/downloads/Hypnosis_AKA_Saimin_(1999).srt"]
assert storage_chain.uploaded_files == [extracted_subtitle]
def test_save_subtitle_response_rejects_unsupported_filename_from_header(monkeypatch, tmp_path):
"""
响应头文件名不是字幕或支持的压缩包时,应继续拒绝保存。
"""
storage_chain = _FakeSubtitleStorageChain()
temp_path = tmp_path / "temp"
response = SimpleNamespace(
content=b"<html>error</html>",
headers={"content-disposition": 'attachment; filename="error.html"'},
)
monkeypatch.setattr(
download_module,
"settings",
SimpleNamespace(TEMP_PATH=temp_path, RMT_SUBEXT=settings.RMT_SUBEXT),
)
monkeypatch.setattr(download_module, "StorageChain", lambda: storage_chain)
chain = DownloadChain.__new__(DownloadChain)
subtitle = SubtitleInfo(
title="Hypnosis",
enclosure="https://audiences.me/downloadsubs.php?torrentid=666519&subid=2195",
)
saved_files = chain._save_subtitle_response(
subtitle=subtitle,
response=response,
target_dir=Path("/downloads"),
)
assert saved_files == []
assert storage_chain.uploaded_files == []
class _FakeBatchTorrentHelper:
"""
为批量下载测试提供稳定排序和种子文件集数解析。