Skip to content

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

feat: 兼容旧任务数据

XiaoYouChRcae5c4727948
diff --git a/app/bases/models.py b/app/bases/models.py
index 1cdfdf0d..4dccb4e8 100644
--- a/app/bases/models.py
+++ b/app/bases/models.py
@@ -39,6 +39,10 @@ def _toSerializable(obj: Any) -> Any:
def _filterDataclassKwargs(cls: type, obj: dict[str, Any]) -> dict[str, Any]:
allowed = {field.name for field in dataclass_fields(cls) if field.init}
+ for klass in cls.__mro__:
+ for name, val in vars(klass).items():
+ if isinstance(val, property):
+ allowed.discard(name)
return {key: value for key, value in obj.items() if key in allowed}
@@ -322,12 +326,36 @@ class Task:
def serialize(self) -> bytes:
obj = _toSerializable(self)
obj.pop("stageType", None)
+ if type(self).__name__ != "Task":
+ obj["type"] = type(self).__name__
if "stages" in obj:
obj["stages"] = [loads(stage.serialize()) for stage in self.stages]
if "files" in obj and self.files is not None:
obj["files"] = [_toSerializable(f) for f in self.files]
return dumps(obj)
+ _registry: ClassVar[Dict[str, Type["Task"]]] = {}
+
+ _TYPE_TO_PACK_ID: ClassVar[Dict[str, str]] = {
+ "HttpTask": "http",
+ "BilibiliTask": "bili",
+ "M3U8Task": "m3u8",
+ "M3U8InstallTask": "m3u8",
+ "FFmpegMergeTask": "ffmpeg",
+ "FFmpegInstallTask": "ffmpeg",
+ "BitTorrentTask": "bt",
+ "BTTask": "bt",
+ "FtpTask": "ftp",
+ }
+
+ _TYPE_ALIASES: ClassVar[Dict[str, str]] = {
+ "BitTorrentTask": "BTTask",
+ }
+
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__(**kwargs)
+ Task._registry[cls.__name__] = cls
+
@classmethod
def deserialize(cls, data: Any) -> "Task":
if isinstance(data, (bytes, bytearray, str)):
@@ -335,7 +363,15 @@ class Task:
else:
obj = data
- obj.pop("type", None)
+ typeName = obj.pop("type", None)
+
+ targetCls = cls
+ if isinstance(typeName, str):
+ resolvedName = Task._TYPE_ALIASES.get(typeName, typeName)
+ targetCls = Task._registry.get(resolvedName, cls)
+
+ if "packId" not in obj and isinstance(typeName, str):
+ obj["packId"] = Task._TYPE_TO_PACK_ID.get(typeName, "http")
if "status" in obj and isinstance(obj["status"], str):
obj["status"] = TaskStatus[obj["status"]]
@@ -350,7 +386,7 @@ class Task:
if rawFiles is not None:
obj["files"] = [TaskFile(**_filterDataclassKwargs(TaskFile, f)) for f in rawFiles]
- return cls(**_filterDataclassKwargs(cls, obj))
+ return targetCls(**_filterDataclassKwargs(targetCls, obj))
def __hash__(self):
return hash(self.taskId)
diff --git a/features/bittorrent_pack/task.py b/features/bittorrent_pack/task.py
index f54b915c..e7255327 100644
--- a/features/bittorrent_pack/task.py
+++ b/features/bittorrent_pack/task.py
@@ -404,6 +404,7 @@ class BTTask(Task):
return False
self.isSeeding = False
+ self._updateSlot()
self.stage.stateText = "已添加新的下载文件"
self.stage.setStatus(TaskStatus.PAUSED)
self.stage.receivedBytes = sum(file.downloadedBytes for file in self.files if file.selected)
@@ -435,14 +436,14 @@ class BTTask(Task):
self.shareRatioPercent = 0
self.seedingTimeSeconds = 0
self.isSeeding = False
+ self._updateSlot()
for file in self.files:
file.downloadedBytes = 0
file.completed = False
return result
- @property
- def usesSlot(self) -> bool:
- return not self.isSeeding
+ def _updateSlot(self):
+ self.usesSlot = not self.isSeeding
async def run(self):
try:
@@ -545,6 +546,7 @@ class BTWorker(Worker):
self.stage.peerCount = int(status.num_peers)
self.stage.seedCount = int(status.num_seeds)
self.task.isSeeding = isSeeding
+ self.task._updateSlot()
self.stage.downloadRate = int(status.download_rate)
self.stage.uploadRate = int(status.upload_rate)
self.task.shareRatioPercent = _shareRatioPercent(status)