Skip to content

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

feat(task): 添加任务标题去重功能

XiaoYouChR89b835c391d5
diff --git a/app/bases/models.py b/app/bases/models.py
index a81a12bf..cb77797c 100644
--- a/app/bases/models.py
+++ b/app/bases/models.py
@@ -120,6 +120,7 @@ class Task:
def setTitle(self, title: str):
self.title = title
+ self.syncStagePaths()
def syncStagePaths(self):
raise NotImplementedError
diff --git a/app/supports/utils.py b/app/supports/utils.py
index 67d367d0..b27d818a 100644
--- a/app/supports/utils.py
+++ b/app/supports/utils.py
@@ -11,6 +11,7 @@ from PySide6.QtGui import QDesktopServices
from loguru import logger
from qfluentwidgets import MessageBox
+from app.bases.models import Task
from app.supports.config import cfg
def openFolder(path):
@@ -149,6 +150,29 @@ def getReadableTime(seconds: int) -> str:
remainingSeconds = seconds % 3600
return f"{int(seconds // 3600)}h{int(remainingSeconds // 60)}m{remainingSeconds % 60}s"
+
+def ensureUniqueTaskTarget(
+ task: Task,
+) -> bool:
+ target = Path(task.resolvePath.strip())
+ if not target.name:
+ return False
+
+ if not target.exists() and not Path(f"{target}.ghd").exists():
+ return False
+
+ suffixes = "".join(target.suffixes) # .tar.gz
+ stem = target.name[:-len(suffixes)] if suffixes else target.name # stem 不会去除所有的后缀
+
+ index = 1
+ while True:
+ renamed = target.with_name(f"{stem}({index}){suffixes}")
+ if not renamed.exists() and not Path(f"{renamed}.ghd").exists():
+ task.setTitle(renamed.name)
+ return True
+ index += 1
+
+
def retry(
retries: int = 3, delay: float = 0.1, handleFunction: Callable = lambda e: None
):
diff --git a/app/view/windows/main_window.py b/app/view/windows/main_window.py
index 80ca0470..61b843e7 100644
--- a/app/view/windows/main_window.py
+++ b/app/view/windows/main_window.py
@@ -18,7 +18,7 @@ from app.supports.config import cfg, DEFAULT_HEADERS, AUTHOR_URL, VERSION, FEEDB
from app.supports.recorder import taskRecorder
from app.supports.signal_bus import signalBus
from app.supports.update import fetchLatestRelease, hasNewerRelease, releaseVersion, selectCurrentPlatformAsset
-from app.supports.utils import getProxies, bringWindowToTop, showMessageBox, isLessThanWin10
+from app.supports.utils import getProxies, bringWindowToTop, showMessageBox, isLessThanWin10, ensureUniqueTaskTarget
from app.view.components.add_task_dialog import AddTaskDialog
from app.view.components.labels import IconBodyLabel
from app.view.components.release_info_dialog import ReleaseInfoDialog
@@ -319,6 +319,10 @@ class MainWindow(MSFluentWindow):
def addTask(self, task) -> bool:
try:
+ originalTitle = task.title
+ if ensureUniqueTaskTarget(task):
+ logger.info("检测到重名文件,已自动重命名 {} -> {}", originalTitle, task.title)
+
card = featureService.createTaskCard(task, self)
taskRecorder.add(task, False)
self.taskPage.addCard(card)
@@ -326,7 +330,7 @@ class MainWindow(MSFluentWindow):
taskRecorder.flush()
return True
except Exception as e:
- logger.opt(exception=e).error("无法创建任务卡片 {}", getattr(task, "title", "Unknown"))
+ logger.opt(exception=e).error("无法创建任务卡片 {}", task.title)
return False
def closeEvent(self, event):
diff --git a/features/bili_pack/task.py b/features/bili_pack/task.py
index de3aeaff..9f30a00a 100644
--- a/features/bili_pack/task.py
+++ b/features/bili_pack/task.py
@@ -52,9 +52,6 @@ class BilibiliTask(Task):
audioPath = finalPath.with_name(f"{finalPath.stem}.audio.m4s")
return str(videoPath), str(audioPath)
- def setTitle(self, title: str):
- self.title = title
-
def syncStagePaths(self):
self.videoPath, self.audioPath = self._buildStagePaths()
for stage in self.stages:
diff --git a/features/http_pack/task.py b/features/http_pack/task.py
index fe76e550..01224268 100644
--- a/features/http_pack/task.py
+++ b/features/http_pack/task.py
@@ -37,10 +37,6 @@ class HttpTask(Task):
proxies: dict = field(default_factory=getProxies)
blockNum: int = field(default_factory=lambda: httpConfig.preBlockNum.value)
- def setTitle(self, title: str):
- self.title = title
- self.syncStagePaths()
-
def syncStagePaths(self):
resolvePath = str(self.path / self.title)
for stage in self.stages: