Skip to content

The only downloader you need. 下载器的集大成者。

feat(bt): 支持关闭在线种子解析以仅下载 .torrent 文件

XiaoYouChRb81a2977ec99
diff --git a/dist/packs/bittorrent_pack.zip b/dist/packs/bittorrent_pack.zip
index 72f8963b..373532da 100644
Binary files a/dist/packs/bittorrent_pack.zip and b/dist/packs/bittorrent_pack.zip differ
diff --git a/features/bittorrent_pack/config.py b/features/bittorrent_pack/config.py
index de92008e..da190c25 100644
--- a/features/bittorrent_pack/config.py
+++ b/features/bittorrent_pack/config.py
@@ -35,6 +35,7 @@ class BitTorrentConfig(PackConfig):
)
enableWebTrackers = ConfigItem("BitTorrent", "EnableWebTrackers", True, BoolValidator())
autoRefreshWebTrackers = ConfigItem("BitTorrent", "AutoRefreshWebTrackers", True, BoolValidator())
+ shouldParseOnlineTorrent = ConfigItem("BitTorrent", "ParseOnlineTorrent", True, BoolValidator())
saveMagnetFile = ConfigItem("BitTorrent", "SaveMagnetTorrentFile", False, BoolValidator())
seedingRatioLimit = RangeConfigItem("BitTorrent", "SeedRatioLimitPercent", 0, RangeValidator(0, 10000))
seedingTimeLimit = RangeConfigItem("BitTorrent", "SeedTimeLimitMinutes", 0, RangeValidator(0, 43200))
@@ -105,6 +106,9 @@ class BitTorrentConfig(PackConfig):
ComboBoxSettingCard(self.storageMode, FluentIcon.SAVE, self.tr("文件分配模式"),
self.tr("稀疏分配更省磁盘写入,预分配更容易提前暴露空间不足"),
texts=[self.tr("稀疏分配"), self.tr("预分配")], parent=btGroup),
+ SwitchSettingCard(FluentIcon.LINK, self.tr("解析在线种子链接"),
+ self.tr("HTTP 链接指向 .torrent 文件时解析为 BT 任务,关闭时仅下载 .torrent 文件"),
+ self.shouldParseOnlineTorrent, btGroup),
SwitchSettingCard(FluentIcon.SAVE, self.tr("保存 Magnet 种子文件"),
self.tr("下载 magnet 链接或在线种子时额外保存 .torrent 文件"), self.saveMagnetFile, btGroup),
SwitchSettingCard(FluentIcon.LIBRARY, self.tr("顺序下载"),
diff --git a/features/bittorrent_pack/manifest.toml b/features/bittorrent_pack/manifest.toml
index 2617576d..31e371e6 100644
--- a/features/bittorrent_pack/manifest.toml
+++ b/features/bittorrent_pack/manifest.toml
@@ -2,5 +2,5 @@
entry = "pack.py"
class = "BitTorrentPack"
dependencies = []
-version = "1.4.1"
+version = "1.4.2"
gdMinVersion = "4.5.0"
diff --git a/features/bittorrent_pack/pack.py b/features/bittorrent_pack/pack.py
index 9854defa..eb573f0b 100644
--- a/features/bittorrent_pack/pack.py
+++ b/features/bittorrent_pack/pack.py
@@ -47,7 +47,10 @@ class TorrentParser(TaskParser):
def match(self, options: TaskOptions) -> bool:
if options.isProbed:
- return options.file.matchType({".torrent"}, {"application/x-bittorrent"})
+ return (
+ bittorrentConfig.shouldParseOnlineTorrent.value
+ and options.file.matchType({".torrent"}, {"application/x-bittorrent"})
+ )
url = toMagnetUrl(options.url.strip())
if urlparse(url).scheme.lower() == "magnet":
return True
diff --git a/tests/bittorrent_pack/test_task_parser.py b/tests/bittorrent_pack/test_task_parser.py
index 746dfec0..0c6d7e18 100644
--- a/tests/bittorrent_pack/test_task_parser.py
+++ b/tests/bittorrent_pack/test_task_parser.py
@@ -5,7 +5,7 @@ import pytest
from aiohttp import web
from app.models.pack import PackServices
-from app.models.task import TaskError, TaskOptions
+from app.models.task import ResourceTaskOptions, TaskError, TaskOptions
from bittorrent_pack.config import bittorrentConfig
from bittorrent_pack.pack import BitTorrentPack
from bittorrent_pack.session import btSession
@@ -36,10 +36,12 @@ def buildTorrent(folder) -> bytes:
return lt.bencode(torrent.generate())
-def buildNamedHandler(content: bytes, name: str):
+def buildNamedHandler(content: bytes, name: str, requests: list[web.Request] | None = None):
rangeHandler = buildRangeHandler(content)
async def handler(request: web.Request) -> web.Response:
+ if requests is not None:
+ requests.append(request)
response = await rangeHandler(request)
response.headers["Content-Disposition"] = f'attachment; filename="{name}"'
return response
@@ -56,6 +58,49 @@ async def test_parse_online_torrent_adds_its_files(features, startServer, tmp_pa
assert task.url == f"{url}?id=1"
+async def test_parse_online_torrent_downloads_file_when_disabled(features, startServer, monkeypatch, tmp_path):
+ monkeypatch.setattr(bittorrentConfig.shouldParseOnlineTorrent, "value", False)
+ requests: list[web.Request] = []
+ url = await startServer(buildNamedHandler(buildTorrent(tmp_path), "movie.torrent", requests))
+
+ task = await features.parse(TaskOptions(url=f"{url}?id=1"))
+
+ assert isinstance(task, HttpTask)
+ assert task.name == "movie.torrent"
+ assert task.url == f"{url}?id=1"
+ assert all("range" in req.headers for req in requests)
+
+
+async def test_parse_resource_torrent_downloads_file_without_request_when_disabled(
+ features, startServer, monkeypatch, tmp_path,
+):
+ monkeypatch.setattr(bittorrentConfig.shouldParseOnlineTorrent, "value", False)
+ torrentBytes = buildTorrent(tmp_path)
+ requests: list[web.Request] = []
+ url = await startServer(buildNamedHandler(torrentBytes, "movie.torrent", requests))
+
+ task = await features.parse(
+ ResourceTaskOptions(url=url, name="movie.torrent", size=len(torrentBytes), canUseRangeRequests=False)
+ )
+
+ assert isinstance(task, HttpTask)
+ assert task.name == "movie.torrent"
+ assert requests == []
+
+
+async def test_parse_local_torrent_still_creates_bt_task_when_online_parse_disabled(
+ features, monkeypatch, tmp_path,
+):
+ monkeypatch.setattr(bittorrentConfig.shouldParseOnlineTorrent, "value", False)
+ torrentFile = tmp_path / "movie.torrent"
+ torrentFile.write_bytes(buildTorrent(tmp_path))
+
+ task = await features.parse(TaskOptions(url=torrentFile.as_uri()))
+
+ assert isinstance(task, BTTask)
+ assert task.name == "movie.mkv"
+
+
async def test_parse_invalid_online_torrent_adds_plain_file(features, startServer):
url = await startServer(buildNamedHandler(b"not a torrent" * 10, "movie.torrent"))
diff --git a/versions.json b/versions.json
index 1a62fd0d..d318db16 100644
--- a/versions.json
+++ b/versions.json
@@ -68,9 +68,9 @@
"gdMinVersion": "4.5.0"
},
"bittorrent_pack": {
- "version": "1.4.1",
+ "version": "1.4.2",
"file": "bittorrent_pack.zip",
- "sha256": "34ddaef956fb5692bf8969282a492a771c0eb0cd8f66928934028ee99a50c7fe",
+ "sha256": "16200c800ce0385cbfbca9ffd7f50bbfb7ea26500dcfb62557e56e0dc69de60c",
"gdMinVersion": "4.5.0"
},
"ed2k_pack": {