The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
refactor(app): 将 addDownloadTask 函数从 signal_bus.py 移动到 methods.py, 并优化其调用逻辑
XiaoYouChR391afd567e83
diff --git a/app/common/download_task.py b/app/common/download_task.pyindex e134bf03..ae917f63 100644--- a/app/common/download_task.py+++ b/app/common/download_task.py@@ -27,7 +27,8 @@ class DownloadWorker:class DownloadTask(QThread):- """TaskManager"""+ """ Task Manager+ self.fileSize == -1 表示自动获取; == 0 表示不能并行下载; else 表示正常"""taskInited = Signal(bool) # 线程初始化成功, 并传递是否支持并行下载的信息# processChange = Signal(str) # 目前进度 且因为C++ int最大值仅支持到2^31 PyQt又没有Qint类 故只能使用str代替@@ -120,47 +121,42 @@ class DownloadTask(QThread):def __initTask(self):"""获取链接信息并初始化线程"""- if self.fileSize == -1 or not self.fileName:- try:+ try:+ if self.fileSize == -1 or not self.fileName:self.url, self.fileName, self.fileSize = getLinkInfo(self.url, self.headers, self.fileName)- if self.fileSize:- self.ableToParallelDownload = True- else:- self.ableToParallelDownload = False # 处理无法并行下载的情况-- # 获取文件路径- if not self.filePath and Path(self.filePath).is_dir() == False:- self.filePath = Path.cwd()-- else:- self.filePath = Path(self.filePath)- if not self.filePath.exists():- self.filePath.mkdir()-- # 检验文件合法性并自动重命名- if sys.platform == "win32":- self.fileName = ''.join([i for i in self.fileName if i not in r'\/:*?"<>|']) # 去除Windows系统不允许的字符- if len(self.fileName) > 255:- self.fileName = self.fileName[:255]-- Path(f"{self.filePath}/{self.fileName}").touch()-- # 任务初始化完成- if self.ableToParallelDownload:- self.taskInited.emit(True)- else:- self.taskInited.emit(False)- self.preBlockNum = 1-- except Exception as e: # 重试也没用- self.gotWrong.emit(repr(e))- else:if self.fileSize:self.ableToParallelDownload = Trueelse:self.ableToParallelDownload = False # 处理无法并行下载的情况+ # 获取文件路径+ if not self.filePath and Path(self.filePath).is_dir() == False:+ self.filePath = Path.cwd()++ else:+ self.filePath = Path(self.filePath)+ if not self.filePath.exists():+ self.filePath.mkdir()++ # 检验文件合法性并自动重命名+ if sys.platform == "win32":+ self.fileName = ''.join([i for i in self.fileName if i not in r'\/:*?"<>|']) # 去除Windows系统不允许的字符+ if len(self.fileName) > 255:+ self.fileName = self.fileName[:255]++ Path(f"{self.filePath}/{self.fileName}").touch()++ # 任务初始化完成+ if self.ableToParallelDownload:+ self.taskInited.emit(True)+ else:+ self.taskInited.emit(False)+ self.preBlockNum = 1++ except Exception as e: # 重试也没用+ self.gotWrong.emit(repr(e))+def __loadWorkers(self):if not self.ableToParallelDownload:# 如果无法并行下载,创建一个单线程的 workerdiff --git a/app/common/methods.py b/app/common/methods.pyindex e6a5e331..151f87a4 100644--- a/app/common/methods.py+++ b/app/common/methods.py@@ -15,8 +15,9 @@ from PySide6.QtGui import QDesktopServicesfrom PySide6.QtWidgets import QApplicationfrom loguru import logger-from app.common.config import cfg+from app.common.config import cfg, Headersfrom app.common.plugin_base import PluginBase+from app.common.signal_bus import signalBusplugins = []@@ -274,4 +275,19 @@ def bringWindowToTop(window):window.showNormal()# 激活窗口,使其显示在最前面window.activateWindow()- window.raise_()\ No newline at end of file+ window.raise_()+++def addDownloadTask(url: str, fileName: str = None, filePath: str = None,+ headers: dict = None, status:str = "working", preBlockNum: int= None, notCreateHistoryFile: bool = False, fileSize: int = -1):+ """ Global function to add download task """+ if not filePath:+ filePath = cfg.downloadFolder.value++ if not preBlockNum:+ preBlockNum = cfg.preBlockNum.value++ if not headers:+ headers = Headers++ signalBus.addTaskSignal.emit(url, fileName, filePath, headers, status, preBlockNum, notCreateHistoryFile, str(fileSize))diff --git a/app/common/signal_bus.py b/app/common/signal_bus.pyindex 14c71fd6..96fb6652 100644--- a/app/common/signal_bus.py+++ b/app/common/signal_bus.py@@ -1,8 +1,6 @@# coding: utf-8from PySide6.QtCore import QObject, Signal-from app.common.config import cfg, Headers-class SignalBus(QObject):""" Signal bus """@@ -10,17 +8,3 @@ class SignalBus(QObject):allTaskFinished = Signal()signalBus = SignalBus()--def addDownloadTask(url: str, fileName: str = None, filePath: str = None,- headers: dict = None, status:str = "working", preBlockNum: int= None, notCreateHistoryFile: bool = False, fileSize: str = "-1"):- """ Global function to add download task """- if not filePath:- filePath = cfg.downloadFolder.value-- if not preBlockNum:- preBlockNum = cfg.preBlockNum.value-- if not headers:- headers = Headers-- signalBus.addTaskSignal.emit(url, fileName, filePath, headers, status, preBlockNum, notCreateHistoryFile, fileSize)\ No newline at end of filediff --git a/app/components/add_task_dialog.py b/app/components/add_task_dialog.pyindex 26865d78..ac32af3c 100644--- a/app/components/add_task_dialog.py+++ b/app/components/add_task_dialog.py@@ -13,8 +13,7 @@ from app.components.custom_mask_dialog_base import MaskDialogBasefrom .Ui_AddTaskOptionDialog import Ui_AddTaskOptionDialogfrom .custom_dialogs import EditHeadersDialogfrom ..common.config import cfg, Headers-from ..common.methods import getReadableSize, getLinkInfo-from ..common.signal_bus import addDownloadTask+from ..common.methods import getReadableSize, getLinkInfo, addDownloadTaskurlRe = re.compile(r"^" +"(https?://)" +@@ -125,7 +124,7 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):try:path.mkdir(parents=True, exist_ok=True)except Exception as e:- MessageBox("错误", str(e), self)+ MessageBox("错误", repr(e), self)else:if not os.access(path, os.W_OK):MessageBox("错误", "似乎是没有权限向此目录写入文件", self)@@ -145,7 +144,7 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):try:path.mkdir(parents=True, exist_ok=True)except Exception as e:- MessageBox("错误", str(e), self)+ MessageBox("错误", repr(e), self)else:if not os.access(path, os.W_OK):MessageBox("错误", "似乎是没有权限向此目录写入文件", self)diff --git a/app/components/update_dialog.py b/app/components/update_dialog.pyindex f2141975..d6ec1272 100644--- a/app/components/update_dialog.py+++ b/app/components/update_dialog.py@@ -6,8 +6,7 @@ from loguru import loggerfrom qfluentwidgets import InfoBar, InfoBarPosition, FluentStyleSheetfrom app.common.config import VERSION-from app.common.methods import getProxy, getLocalTimeFromGithubApiTime, getReadableSize-from app.common.signal_bus import addDownloadTask+from app.common.methods import getProxy, getLocalTimeFromGithubApiTime, getReadableSize, addDownloadTaskfrom app.components.Ui_UpdateDialog import Ui_UpdateDialogfrom app.components.custom_mask_dialog_base import MaskDialogBasediff --git a/app/view/pop_up_window.py b/app/view/pop_up_window.pyindex 117616f7..487632be 100644--- a/app/view/pop_up_window.py+++ b/app/view/pop_up_window.py@@ -186,7 +186,7 @@ class PopUpWindow(QWidget, Ui_PopUpWindow):@classmethoddef showPopUpWindow(cls, fileResolvePath:str, mainWindow=None):- print("PopUpWindow Created")+ # print("PopUpWindow Created")return cls(fileResolvePath, mainWindow)@@ -209,7 +209,7 @@ class PopUpWindowManager(QObject):PopUpWindowManager._initialized = Truedef add(self, popUpWindow: PopUpWindow):- print("PopUpWindow Added")+ # print("PopUpWindow Added")if popUpWindow not in self.popUpWindows:self.popUpWindows.append(popUpWindow)# 按照 PopUpWindow 的数量移动 PopUpWindow 的 y 坐标@@ -218,7 +218,7 @@ class PopUpWindowManager(QObject):popUp.move(popUp.x(), getCurrentScreenGeometry().height() - ((popUp.height() + 13) * (len(self.popUpWindows) - i)))def remove(self, popUpWindow: PopUpWindow):- print("PopUpWindow Destroyed")+ # print("PopUpWindow Destroyed")if popUpWindow in self.popUpWindows:self.popUpWindows.remove(popUpWindow)# 按照 PopUpWindow 的数量移动 PopUpWindow 的 y 坐标diff --git a/plugins/jy_os_page.py b/plugins/jy_os_page.pyindex b0ace623..09d9e675 100644--- a/plugins/jy_os_page.py+++ b/plugins/jy_os_page.py@@ -15,9 +15,9 @@ from qfluentwidgets import SmoothScrollArea, TitleLabel, SettingCardGroup, Optiofrom qfluentwidgets.components.dialog_box.mask_dialog_base import MaskDialogBasefrom app.common.config import Headers-from app.common.methods import getProxy+from app.common.methods import getProxy, addDownloadTaskfrom app.common.plugin_base import PluginBase-from app.common.signal_bus import signalBus, addDownloadTask+from app.common.signal_bus import signalBusclass JyOSPagePlugin(PluginBase):@@ -241,7 +241,7 @@ class DownloadOptionDialog(MaskDialogBase):try:path.mkdir(parents=True, exist_ok=True)except Exception as e:- MessageBox("错误", str(e), self)+ MessageBox("错误", repr(e), self)else:if not os.access(path, os.W_OK):MessageBox("错误", "似乎是没有权限向此目录写入文件", self)