fix(torrent): support traditional first-download prompts

This commit is contained in:
jxxghp
2026-11-10 07:05:07 +08:00
parent 2ecedc25ce
commit a4ccd86d85
2 changed files with 9 additions and 4 deletions

View File

@@ -36,6 +36,10 @@ from app.schemas.media import resolve_media_identity
from app.schemas.types import MediaSource, MediaType, SystemConfigKey
_SIZE_UNIT = 1024 * 1024
# NexusPHP 首次下载提示会随站点会话语言返回简体或繁体文案。
_FIRST_DOWNLOAD_MARKERS = tuple(
marker.encode("utf-8") for marker in ("下载种子文件", "下載種子檔案")
)
def _is_transient_torrent_status(status: int) -> bool:
@@ -457,7 +461,7 @@ class TorrentHelper:
return False
if self.get_fileinfo_from_torrent_content(content)[1]:
return False
return "下载种子文件".encode("utf-8") not in content
return not any(marker in content for marker in _FIRST_DOWNLOAD_MARKERS)
def _cached_torrent_content(self, cache_backend: Any, cache_path: Path) -> Optional[tuple[
Union[str, bytes], str, list[str],
@@ -532,7 +536,7 @@ class TorrentHelper:
)
if file_list:
return cache_path, req.content, folder_name, file_list, ""
if "下载种子文件".encode("utf-8") in req.content:
if any(marker in req.content for marker in _FIRST_DOWNLOAD_MARKERS):
confirmation = _first_download_form(req.text, url)
if confirmation is None:
logger.warning("触发首次种子下载,但未找到可安全提交的确认表单")

View File

@@ -417,6 +417,7 @@ def test_magnet_body_on_error_does_not_bypass_status_check(boundary):
cache.set.assert_not_called()
@pytest.mark.parametrize("marker", ["下载种子文件", "下載種子檔案"])
@pytest.mark.parametrize("form", [
'<form action="?"><input value="yes" name="confirm"></form>',
"<form method='POST' action='?'><input value='a&amp;b' name='token'></form>",
@@ -424,12 +425,12 @@ def test_magnet_body_on_error_does_not_bypass_status_check(boundary):
'<form action="download.php?id=39" method="post"><input name="confirm" value="yes"></form>',
'<form action="?"><input name="token" value="a&amp;b"><button name="confirm" value="yes">下载</button></form>',
])
def test_first_download_form_variants_are_confirmed(boundary, form):
def test_first_download_form_variants_are_confirmed(boundary, form, marker):
"""
按 HTML 表单语义提交确认,兼容属性顺序、引号、实体和提交按钮。
"""
helper, port, cache, sleep = boundary
page = ("下载种子文件" + form).encode()
page = (marker + form).encode()
port.request.side_effect = [_response(content=page), _response()]
assert helper.download_torrent(URL, cookie="session=test")[1] == CONTENT