The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
refactor(ftp): 优化FTP下载任务的子工作器管理
XiaoYouChR9c178ad99a5b
diff --git a/features/ftp_pack/cards.py b/features/ftp_pack/cards.pyindex 1a06d742..fc20b837 100644--- a/features/ftp_pack/cards.py+++ b/features/ftp_pack/cards.py@@ -491,7 +491,7 @@ class FtpTaskCard(UniversalTaskCard):return receivedBytes, speeddef _openPrimaryTarget(self):- openFile(str(Path(self.task.resolvePath)))+ openFile(self.task.resolvePath)def _openTaskFolder(self):target = Path(self.task.resolvePath)@@ -571,7 +571,7 @@ class FtpTaskCard(UniversalTaskCard):returnfor stage in self.task.stages:- resolvePath = str(getattr(stage, "resolvePath", "") or "").strip()+ resolvePath = stage.resolvePath.strip()if not resolvePath:continuetarget = Path(resolvePath)diff --git a/features/ftp_pack/pack.py b/features/ftp_pack/pack.pyindex 026260e0..d3338885 100644--- a/features/ftp_pack/pack.py+++ b/features/ftp_pack/pack.py@@ -18,11 +18,7 @@ class FtpPack(FeaturePack):return await parse(payload)def createTaskCard(self, task: Task, parent=None):- if isinstance(task, FtpTask):- return FtpTaskCard(task, parent)- return None+ return FtpTaskCard(task, parent)def createResultCard(self, task: Task, parent=None):- if isinstance(task, FtpTask):- return FtpResultCard(task, parent)- return None+ return FtpResultCard(task, parent)diff --git a/features/ftp_pack/task.py b/features/ftp_pack/task.pyindex 0c5f525c..cf7377fb 100644--- a/features/ftp_pack/task.py+++ b/features/ftp_pack/task.py@@ -129,6 +129,7 @@ class FtpTask(Task):item if isinstance(item, FtpRemoteFile) else FtpRemoteFile(**item)for item in self.files]+ self._filesByIndex = {file.index: file for file in self.files}self.title = _sanitizeName(self.title)super().__post_init__()self._recalculateSelection()@@ -147,45 +148,34 @@ class FtpTask(Task):def isDirectorySource(self) -> bool:return self.sourceType == "dir"- @property- def hasUnselectedFiles(self) -> bool:- return self.selectedFileCount < self.totalFileCount-@propertydef resolvePath(self) -> str:return str(Path(self.path) / self.title)@propertydef selectedStages(self) -> list["FtpTaskStage"]:- stages: list[FtpTaskStage] = []- for stage in self.stages:- if not isinstance(stage, FtpTaskStage):- continue- file = self.fileByIndex(stage.fileIndex)- if file is not None and file.selected:- stages.append(stage)- return stages+ return [+ stage+ for stage in self.stages+ if self.fileByIndex(stage.fileIndex).selected+ ]- def fileByIndex(self, index: int) -> FtpRemoteFile | None:- for file in self.files:- if file.index == index:- return file- return None+ def fileByIndex(self, index: int) -> FtpRemoteFile:+ return self._filesByIndex[index]def syncStagePaths(self):rootPath = Path(self.path) / self.title- for stage in self.stages:- if not isinstance(stage, FtpTaskStage):- continue+ if not self.isDirectorySource:+ resolvePath = str(rootPath)+ for stage in self.stages:+ stage.resolvePath = resolvePath+ return+ for stage in self.stages:file = self.fileByIndex(stage.fileIndex)- if file is None:- continue-- if self.isDirectorySource:- stage.resolvePath = str(rootPath / Path(*PurePosixPath(file.relativePath).parts))- else:- stage.resolvePath = str(rootPath)+ stage.resolvePath = str(+ rootPath / Path(*PurePosixPath(file.relativePath).parts)+ )def setTitle(self, title: str):self.title = _sanitizeName(title)@@ -195,19 +185,10 @@ class FtpTask(Task):self.fileSize = sum(file.size for file in self.files if file.selected)def _syncFileProgress(self):- stageByFileIndex = {- stage.fileIndex: stage- for stage in self.stages- if isinstance(stage, FtpTaskStage)- }+ stageByFileIndex = {stage.fileIndex: stage for stage in self.stages}for file in self.files:- stage = stageByFileIndex.get(file.index)- if stage is None:- file.downloadedBytes = 0- file.completed = False- continue-+ stage = stageByFileIndex[file.index]file.downloadedBytes = max(0, int(stage.receivedBytes))file.completed = stage.status == TaskStatus.COMPLETEDif file.completed and file.size > 0:@@ -307,15 +288,9 @@ class FtpTask(Task):self.stages.sort(key=lambda stage: stage.stageIndex)currentStage = Nonetry:- for stage in self.stages:+ for stage in self.selectedStages:if self.status != TaskStatus.RUNNING:break- if not isinstance(stage, FtpTaskStage):- continue-- file = self.fileByIndex(stage.fileIndex)- if file is None or not file.selected:- continueif stage.status == TaskStatus.COMPLETED:continue@@ -345,7 +320,7 @@ class FtpWorker(Worker):def __init__(self, stage: FtpTaskStage):super().__init__(stage)self.stage = stage- self.task: FtpTask = getattr(stage, "_task")+ self.task: FtpTask = stage._taskself.speedHistory: list[int] = []self.accelCheckTime = 0.0self.subworkerTasks: set[asyncio.Task] = set()@@ -367,7 +342,7 @@ class FtpWorker(Worker):client.close()raise- async def _closeTransfer(self, client: aioftp.Client | None, stream):+ def _closeTransfer(self, client: aioftp.Client | None, stream):with suppress(Exception):if stream is not None:stream.close()@@ -471,7 +446,7 @@ class FtpWorker(Worker):remaining -= len(chunk)cfg.globalSpeed += len(chunk)finally:- await self._closeTransfer(client, stream)+ self._closeTransfer(client, stream)async def _transferUnknown(self, subworker: FtpSubworker):client = None@@ -493,7 +468,7 @@ class FtpWorker(Worker):subworker.progress += len(chunk)cfg.globalSpeed += len(chunk)finally:- await self._closeTransfer(client, stream)+ self._closeTransfer(client, stream)async def _transferWholeFile(self, subworker: FtpSubworker):client = None@@ -516,7 +491,7 @@ class FtpWorker(Worker):subworker.progress += len(chunk)cfg.globalSpeed += len(chunk)finally:- await self._closeTransfer(client, stream)+ self._closeTransfer(client, stream)async def handleSubworker(self, subworker: FtpSubworker):if subworker.end == SpecialFileSize.UNKNOWN:@@ -818,9 +793,7 @@ def _sanitizeDisplayUrl(parsed: ParseResult) -> str:)-def _displayTitleForSource(path: PurePosixPath, *, host: str, sourceType: str) -> str:- if sourceType == "file" and path.name:- return _sanitizeName(path.name)+def _displayTitleForSource(path: PurePosixPath, host: str) -> str:if path.name:return _sanitizeName(path.name)return _sanitizeName(host)@@ -915,13 +888,9 @@ async def parse(payload: dict) -> FtpTask:)task = FtpTask(- title=_displayTitleForSource(- sourcePath,- host=connectionInfo.host,- sourceType=sourceType,- ),+ title=_displayTitleForSource(sourcePath, connectionInfo.host),url=_sanitizeDisplayUrl(parsedUrl),- fileSize=sum(file.size for file in files if file.selected),+ fileSize=sum(file.size for file in files),path=path,stages=stages,connectionInfo=connectionInfo,@@ -930,7 +899,6 @@ async def parse(payload: dict) -> FtpTask:proxies=proxies,blockNum=blockNum if isinstance(blockNum, int) else cfg.preBlockNum.value,)- task.syncStagePaths()return taskfinally:await _closeClient(client)