Skip to content

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

fix(task): 统一文件搬移逻辑,修复改路径时 FFmpegStep 崩溃

XiaoYouChR5fba7f012980
diff --git a/app/models/task.py b/app/models/task.py
index 04df616c..b4865bd0 100644
--- a/app/models/task.py
+++ b/app/models/task.py
@@ -160,9 +160,32 @@ class TaskStep:
async def run(self) -> None:
raise NotImplementedError
+ @property
+ def outputPath(self) -> str:
+ return ""
+
def deleteFiles(self):
pass
+ def moveFiles(self, oldFolder: Path, newFolder: Path) -> None:
+ from shutil import move
+ rawPath = self.outputPath
+ if not rawPath:
+ return
+ oldPath = Path(rawPath)
+ if not oldPath.exists():
+ return
+ try:
+ relPath = oldPath.relative_to(oldFolder)
+ except ValueError:
+ return
+ newPath = newFolder / relPath
+ newPath.parent.mkdir(parents=True, exist_ok=True)
+ move(str(oldPath), str(newPath))
+ ghdPath = Path(f"{oldPath}.ghd")
+ if ghdPath.exists():
+ move(str(ghdPath), str(newFolder / f"{relPath}.ghd"))
+
@classmethod
def fromFile(cls, file: TaskFile, task: Task) -> TaskStep:
raise NotImplementedError
@@ -248,29 +271,10 @@ class Task:
step.setOptions(options)
def _move(self, newFolder: Path) -> None:
- from shutil import move
oldFolder = self.outputFolder
newFolder.mkdir(parents=True, exist_ok=True)
for step in self.steps:
- oldPath = Path(step.outputPath)
- if not oldPath.exists():
- continue
- try:
- relPath = oldPath.relative_to(oldFolder)
- except ValueError:
- continue
- newPath = newFolder / relPath
- newPath.parent.mkdir(parents=True, exist_ok=True)
- move(str(oldPath), str(newPath))
- ghdPath = Path(f"{oldPath}.ghd")
- if ghdPath.exists():
- move(str(ghdPath), str(newFolder / f"{relPath}.ghd"))
- storedFile = getattr(step, "outputFile", "")
- if storedFile:
- try:
- step.outputFile = str(newFolder / Path(storedFile).relative_to(oldFolder))
- except ValueError:
- pass
+ step.moveFiles(oldFolder, newFolder)
self.outputFolder = newFolder
def currentSnapshot(self) -> tuple[float, int, int]:
diff --git a/features/bittorrent_pack/task.py b/features/bittorrent_pack/task.py
index 1df19c86..7f076a38 100644
--- a/features/bittorrent_pack/task.py
+++ b/features/bittorrent_pack/task.py
@@ -120,6 +120,12 @@ class BTTask(Task):
if self.magnetTorrentPath is not None:
deletePath(self.magnetTorrentPath)
+ def _move(self, newFolder: Path) -> None:
+ from shutil import move
+ if self.magnetTorrentPath is not None and self.magnetTorrentPath.exists():
+ move(str(self.magnetTorrentPath), str(newFolder / f"{self.name}.torrent"))
+ super()._move(newFolder)
+
def reset(self) -> TaskStatus:
result = super().reset()
self.resumeData = ""
@@ -183,6 +189,10 @@ class BTTask(Task):
@dataclass(kw_only=True)
class BTTaskStep(TaskStep):
+ @property
+ def outputPath(self) -> str:
+ return self.task.outputPath
+
async def run(self) -> None:
from .session import btSession
diff --git a/features/ffmpeg_pack/task.py b/features/ffmpeg_pack/task.py
index 877a5260..cb96a927 100644
--- a/features/ffmpeg_pack/task.py
+++ b/features/ffmpeg_pack/task.py
@@ -37,6 +37,10 @@ class FFmpegStep(TaskStep):
audioExtension: str = ""
shouldDeleteSource: bool = True
+ @property
+ def outputPath(self) -> str:
+ return self.outputFile
+
@property
def outputFile(self) -> str:
return str(self.task.outputFolder / f"{mediaStem(self.task)}.mp4")
diff --git a/features/http_pack/task.py b/features/http_pack/task.py
index ffc88f1e..e9011824 100644
--- a/features/http_pack/task.py
+++ b/features/http_pack/task.py
@@ -73,6 +73,14 @@ class HttpTaskStep(TaskStep):
deletePath(path)
deletePath(Path(f"{path}.ghd"))
+ def moveFiles(self, oldFolder: Path, newFolder: Path) -> None:
+ super().moveFiles(oldFolder, newFolder)
+ if self.outputFile:
+ try:
+ self.outputFile = str(newFolder / Path(self.outputFile).relative_to(oldFolder))
+ except ValueError:
+ pass
+
def setOptions(self, options: dict) -> None:
if "headers" in options:
self.headers = options["headers"]
diff --git a/features/m3u8_pack/task.py b/features/m3u8_pack/task.py
index fe51ee73..380aef2a 100644
--- a/features/m3u8_pack/task.py
+++ b/features/m3u8_pack/task.py
@@ -120,6 +120,26 @@ class M3U8TaskStep(TaskStep):
if self._process is not None and self._process.returncode is None:
self._process.terminate()
+ def moveFiles(self, oldFolder: Path, newFolder: Path) -> None:
+ from shutil import move
+ super().moveFiles(oldFolder, newFolder)
+ rawPath = self.outputPath
+ if rawPath:
+ ghdPath = Path(f"{rawPath}.ghd")
+ if ghdPath.exists():
+ try:
+ relPath = Path(rawPath).relative_to(oldFolder)
+ newGhd = newFolder / f"{relPath}.ghd"
+ newGhd.parent.mkdir(parents=True, exist_ok=True)
+ move(str(ghdPath), str(newGhd))
+ except ValueError:
+ pass
+ oldTemp = Path(self._tempFolder)
+ if oldTemp.is_dir():
+ newTemp = newFolder / ".gd3_m3u8" / self.task.taskId
+ newTemp.parent.mkdir(parents=True, exist_ok=True)
+ move(str(oldTemp), str(newTemp))
+
def deleteFiles(self):
tempFolder = Path(self._tempFolder)
deletePath(tempFolder)
diff --git a/features/yt_dlp_pack/task.py b/features/yt_dlp_pack/task.py
index 5062095d..286965d0 100644
--- a/features/yt_dlp_pack/task.py
+++ b/features/yt_dlp_pack/task.py
@@ -12,12 +12,14 @@ from .config import ytDlpConfig, ytDlpRuntime
DEFAULT_VIDEO_FORMAT = "bv*+ba/b"
PROGRESS_TOKEN = "__GD3_PROGRESS__"
FINAL_FILE_TOKEN = "__GD3_FINAL__"
+BEFORE_DL_TOKEN = "__GD3_BEFORE__"
PROGRESS_TEMPLATE = (
f"download:{PROGRESS_TOKEN}"
"%(progress.downloaded_bytes)s|%(progress.total_bytes)s|"
"%(progress.total_bytes_estimate)s|%(progress.speed)s"
)
FINAL_TEMPLATE = f"after_move:{FINAL_FILE_TOKEN}%(filepath)s"
+BEFORE_DL_TEMPLATE = f"before_dl:{BEFORE_DL_TOKEN}%(filename)s"
ERROR_HINTS = (
("is not available in your country", "该视频在您所在地区不可用,请尝试配置代理"),
@@ -99,6 +101,31 @@ class YtDlpTaskStep(TaskStep):
headers: dict[str, str] = field(default_factory=dict)
lastMessage: str = ""
+ @property
+ def outputPath(self) -> str:
+ return self.outputFile
+
+ def moveFiles(self, oldFolder: Path, newFolder: Path) -> None:
+ from shutil import move
+ if not self.outputFile:
+ return
+ oldPath = Path(self.outputFile)
+ try:
+ relPath = oldPath.relative_to(oldFolder)
+ except ValueError:
+ return
+ newBase = newFolder / relPath
+ newBase.parent.mkdir(parents=True, exist_ok=True)
+ if oldPath.exists():
+ move(str(oldPath), str(newBase))
+ for suffix in (".part", ".ytdl"):
+ p = Path(f"{oldPath}{suffix}")
+ if p.exists():
+ move(str(p), str(f"{newBase}{suffix}"))
+ for frag in oldPath.parent.glob(f"{oldPath.name}.part-Frag*"):
+ move(str(frag), str(newFolder / frag.relative_to(oldFolder)))
+ self.outputFile = str(newBase)
+
@property
def _outputTemplate(self) -> str:
return toPosixPath(self.task.outputFolder / "%(title)s.%(ext)s")
@@ -118,6 +145,7 @@ class YtDlpTaskStep(TaskStep):
"--no-simulate",
"--progress",
"--progress-template", PROGRESS_TEMPLATE,
+ "--print", BEFORE_DL_TEMPLATE,
"--print", FINAL_TEMPLATE,
]
@@ -168,6 +196,9 @@ class YtDlpTaskStep(TaskStep):
text = line.strip()
if not text:
return
+ if text.startswith(BEFORE_DL_TOKEN):
+ self.outputFile = text[len(BEFORE_DL_TOKEN):].strip()
+ return
if text.startswith(FINAL_FILE_TOKEN):
self._finalPath = text[len(FINAL_FILE_TOKEN):].strip()
return