Skip to content

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

feat(task): 在Task模型中添加resolvePath属性用于获取完整文件路径

XiaoYouChRec2b275a0ec6
diff --git a/app/bases/models.py b/app/bases/models.py
index 97e0e55e..4d9e299a 100644
--- a/app/bases/models.py
+++ b/app/bases/models.py
@@ -113,6 +113,10 @@ class Task:
createdAt: int = field(default_factory=lambda: int(time_ns()))
path: Path = field(default_factory=lambda: Path(cfg.downloadFolder.value))
+ @property
+ def resolvePath(self) -> str:
+ return str(self.path / self.title)
+
def __post_init__(self):
for stage in self.stages:
stage.bindTask(self)
diff --git a/app/services/core_service.py b/app/services/core_service.py
index bf01c032..53a5b982 100644
--- a/app/services/core_service.py
+++ b/app/services/core_service.py
@@ -33,12 +33,32 @@ class CoreService(QThread):
self.desktopNotifier = DesktopNotifier(app_name="Ghost Downloader", app_icon=Icon(path=getNotifierIcon()))
def sendNotification(self, task: Task):
+ resolvePath = task.resolvePath
+ if not resolvePath:
+ logger.error(f"task {task.taskId} has no resolvePath for notification")
+ return
+
+ directoryPath = str(Path(resolvePath).parent)
iconTempPath = Path(QStandardPaths.writableLocation(QStandardPaths.StandardLocation.TempLocation)) / "finished_file_icon.png"
- QFileIconProvider().icon(QFileInfo(task.stages[0].resolvePath)).pixmap(48, 48).scaled(128, 128,
- aspectMode=Qt.AspectRatioMode.KeepAspectRatio,
- mode=Qt.TransformationMode.SmoothTransformation).save(str(iconTempPath), "PNG")
- buttons = [Button(self.tr('打开文件'), lambda: openFile(task.stages[0].resolvePath)), Button(self.tr('打开目录'), lambda: openFile(task.path))]
- self.loop.create_task(self.desktopNotifier.send(self.tr("下载完成"), task.title, buttons=buttons, on_clicked=lambda: openFile(task.stages[0].resolvePath), icon=Icon(path=iconTempPath)))
+ QFileIconProvider().icon(QFileInfo(resolvePath)).pixmap(48, 48).scaled(
+ 128,
+ 128,
+ aspectMode=Qt.AspectRatioMode.KeepAspectRatio,
+ mode=Qt.TransformationMode.SmoothTransformation,
+ ).save(str(iconTempPath), "PNG")
+ buttons = [
+ Button(self.tr('打开文件'), lambda: openFile(resolvePath)),
+ Button(self.tr('打开目录'), lambda: openFile(directoryPath)),
+ ]
+ self.loop.create_task(
+ self.desktopNotifier.send(
+ self.tr("下载完成"),
+ task.title,
+ buttons=buttons,
+ on_clicked=lambda: openFile(resolvePath),
+ icon=Icon(path=iconTempPath),
+ )
+ )
def runCoroutine(self, coroutine: Coroutine, callback: Callable[[dict, str | None], Coroutine | None] | None = None):
diff --git a/features/http_pack/cards.py b/features/http_pack/cards.py
index 48529e18..fabbe17c 100644
--- a/features/http_pack/cards.py
+++ b/features/http_pack/cards.py
@@ -25,7 +25,7 @@ class HttpTaskCard(TaskCard):
self.cardStatus = self.task.status
self.hBoxLayout = QHBoxLayout(self)
- self.iconLabel = ImageLabel(QFileIconProvider().icon(QFileInfo(self.task.stages[0].resolvePath)).pixmap(48, 48), self)
+ self.iconLabel = ImageLabel(QFileIconProvider().icon(QFileInfo(self.task.resolvePath)).pixmap(48, 48), self)
# TODO macOS
self.iconLabel.setFixedSize(48, 48)
@@ -49,8 +49,8 @@ class HttpTaskCard(TaskCard):
def connectSignalToSlot(self):
self.toggleRunningStatusButton.clicked.connect(self.toggleRunningStatus)
- self.openFileButton.clicked.connect(lambda: (openFile(self.task.path / self.task.title)))
- self.openFolderButton.clicked.connect(lambda: (openFile(self.task.path)))
+ self.openFileButton.clicked.connect(lambda: openFile(self.task.resolvePath))
+ self.openFolderButton.clicked.connect(lambda: openFile(self.task.path))
self.cancelButton.clicked.connect(self._onDeleteButtonClicked)
def toggleRunningStatus(self):
@@ -128,7 +128,8 @@ class HttpTaskCard(TaskCard):
return
candidates: set[Path] = set()
- candidates.add(Path(self.task.path) / self.task.title)
+ if self.task.resolvePath:
+ candidates.add(Path(self.task.resolvePath))
for stage in self.task.stages:
resolvePath = getattr(stage, "resolvePath", None)
if resolvePath:
@@ -238,7 +239,7 @@ class HttpResultCard(ResultCard):
return super().eventFilter(obj, event)
def resetFileIcon(self):
- icon = QFileIconProvider().icon(QFileInfo(self.task.title))
+ icon = QFileIconProvider().icon(QFileInfo(self.task.resolvePath))
self.iconLabel.setImage(icon.pixmap(16, 16))
self.iconLabel.setFixedSize(16, 16)
@@ -266,6 +267,7 @@ class HttpResultCard(ResultCard):
self.task.title = filename
self.filenameLabel.setText(filename)
self.filenameEdit.setText(filename)
+ self.resetFileIcon()
def getTask(self) -> HttpTask:
return self.task
diff --git a/features/http_pack/pack.py b/features/http_pack/pack.py
index 3fdad35f..d944b042 100644
--- a/features/http_pack/pack.py
+++ b/features/http_pack/pack.py
@@ -135,6 +135,8 @@ async def parse(payload: dict) -> HttpTask:
# 获取文件名
fileName = _extractFileName(response.url, head) # 这里取重定向之前的 URL 目的是更好的获取
+ resolvePath = str(Path(cfg.downloadFolder.value) / fileName)
+
task = HttpTask(
title=fileName,
url=url,
@@ -149,7 +151,7 @@ async def parse(payload: dict) -> HttpTask:
fileSize=fileSize,
headers=headers,
proxies=proxies,
- resolvePath=str(Path(cfg.downloadFolder.value) / fileName),
+ resolvePath=resolvePath,
blockNum=blockNum,
)
task.addStage(stage)