Fix subtitle saving after completed transfers

This commit is contained in:
jxxghp
2026-09-24 07:35:39 +08:00
parent 9dba379e49
commit ffa14e9541
2 changed files with 253 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ from app.application.directory import (
build_media_download_path,
validate_download_save_path,
)
from app.application.history import TransferHistorySnapshot
from app.application.torrent.download import TorrentHelper
from app.chain.download.contract import _DownloadOwnerBase
from app.chain.download.ports import (
@@ -36,6 +37,7 @@ from app.schemas.file import FileURI
from app.schemas.system import TransferDirectoryConf as _SchemaTransferDirectoryConf
from app.schemas.types import (
MediaSource,
MediaType,
)
if TYPE_CHECKING:
@@ -132,6 +134,23 @@ class DownloadSubtitleOwner(_DownloadOwnerBase):
in get_chain_runtime_config_snapshot().subtitle_extensions
)
def _get_completed_transfer_histories(
self,
download_hash: Optional[str],
) -> Optional[List[TransferHistorySnapshot]]:
"""读取同一下载任务已成功整理的源文件与目标文件对应关系。"""
if not download_hash:
return []
try:
return [
history
for history in self.transfer_history_repository.list_by_hash(download_hash)
if history.status and history.src and history.dest
]
except Exception as err:
logger.debug(f"查询下载任务整理历史失败:{str(err)}")
return None
@classmethod
def _get_subtitle_working_dir(
cls,
@@ -527,6 +546,51 @@ class DownloadSubtitleOwner(_DownloadOwnerBase):
except Exception as err:
logger.error(f"删除临时文件失败:{str(err)}")
def _enqueue_recreated_site_subtitles(
self,
*,
storage_chain: StorageChain,
working_dir_item: _SchemaFileItem,
context: Context,
download_hash: Optional[str],
downloader: Optional[str],
) -> None:
"""把恢复目录中的站点字幕逐个送入标准整理队列。"""
try:
subtitle_items = [
item
for item in storage_chain.list_files(working_dir_item, recursion=False) or []
if item.type == "file"
and self._is_subtitle_file(item.name or Path(item.path).name)
]
except Exception as err:
logger.error(f"读取恢复下载目录中的字幕文件失败:{err}")
return
if not subtitle_items:
return
from app.chain.transfer.facade import TransferChain
mediainfo = context.media_info
media_type = getattr(mediainfo, "type", None)
if media_type in (None, MediaType.UNKNOWN):
media_type = getattr(context.meta_info, "type", None)
transfer_chain = TransferChain()
for subtitle_item in subtitle_items:
try:
success, message = transfer_chain.do_transfer(
fileitem=subtitle_item,
mediainfo=mediainfo,
mtype=media_type,
downloader=downloader,
download_hash=download_hash,
background=True,
)
if not success:
logger.warn(f"站点字幕加入整理队列失败:{subtitle_item.path} - {message}")
except Exception as err:
logger.error(f"站点字幕加入整理队列异常:{subtitle_item.path} - {err}", exc_info=True)
def download_site_subtitles(
self,
context: Context,
@@ -562,8 +626,9 @@ class DownloadSubtitleOwner(_DownloadOwnerBase):
)
# 文件保存目录,如果是单文件种子,则folder_name是空,此时文件保存目录就是下载目录
storage_chain = StorageChain()
# 等待目录存在
# 等待原下载目录出现;已成功整理而源目录被移动清理时,只恢复空下载目录供字幕落盘。
working_dir_item = None
video_already_transferred = False
# split download_dir into storage and path
fileURI = FileURI.from_uri(download_dir.as_posix())
storage = fileURI.storage or "local"
@@ -580,14 +645,39 @@ class DownloadSubtitleOwner(_DownloadOwnerBase):
if content_dir:
storage = content_storage or storage
download_dir = content_dir
for _ in range(30):
found = storage_chain.get_file_item(storage, download_dir / folder_name)
target_dir = download_dir / folder_name
transfer_histories: Optional[List[TransferHistorySnapshot]] = None
for attempt in range(30):
found = storage_chain.get_file_item(storage, target_dir)
if found:
working_dir_item = found
transfer_histories = self._get_completed_transfer_histories(download_hash)
video_already_transferred = bool(transfer_histories)
break
if attempt == 0:
transfer_histories = self._get_completed_transfer_histories(download_hash)
if transfer_histories:
try:
working_dir_item = storage_chain.get_folder(storage, target_dir)
except Exception as err:
logger.error(f"重建已整理任务下载目录失败:{target_dir} - {err}")
video_already_transferred = bool(transfer_histories)
if working_dir_item:
logger.info(f"下载源目录已被整理清理,重建下载目录以保存字幕:{target_dir}")
break
time.sleep(1)
if not working_dir_item:
logger.error(f"下载目录不存在,无法保存字幕:{download_dir / folder_name}")
transfer_histories = self._get_completed_transfer_histories(download_hash)
if transfer_histories:
try:
working_dir_item = storage_chain.get_folder(storage, target_dir)
except Exception as err:
logger.error(f"重建已整理任务下载目录失败:{target_dir} - {err}")
video_already_transferred = bool(transfer_histories)
if working_dir_item:
logger.info(f"下载源目录已被整理清理,重建下载目录以保存字幕:{target_dir}")
if not working_dir_item:
logger.error(f"下载目录不存在,无法保存字幕:{target_dir}")
return
# 解析字幕下载链接
sublink_list = self._site_subtitle_links(context)
@@ -623,4 +713,15 @@ class DownloadSubtitleOwner(_DownloadOwnerBase):
)
finally:
_close_download_response(ret)
if not video_already_transferred:
transfer_histories = self._get_completed_transfer_histories(download_hash)
video_already_transferred = bool(transfer_histories)
if video_already_transferred:
self._enqueue_recreated_site_subtitles(
storage_chain=storage_chain,
working_dir_item=working_dir_item,
context=context,
download_hash=download_hash,
downloader=downloader,
)
logger.info(f"{torrent.page_url} 页面字幕下载完成")

View File

@@ -332,6 +332,9 @@ def test_download_site_subtitles_does_not_create_missing_save_folder(monkeypatch
user_agent="MoviePilotTest",
)
chain.list_torrents = MagicMock(return_value=[])
chain.transfer_history_repository = SimpleNamespace(
list_by_hash=MagicMock(return_value=[]),
)
chain._site_subtitle_links = MagicMock()
context = Context(
torrent_info=TorrentInfo(page_url="https://example.com/torrent/1"),
@@ -348,6 +351,151 @@ def test_download_site_subtitles_does_not_create_missing_save_folder(monkeypatch
chain._site_subtitle_links.assert_not_called()
def test_download_site_subtitles_recreates_source_directory_and_queues_subtitle(monkeypatch):
"""视频整理后源目录被移动清理时,应恢复目录并把新字幕交给整理队列。"""
accessed_paths = []
created_dirs = []
transfer_calls = []
operation_order = []
target_dir = Path("/download/影视下载/下载中/Demo.Show")
subtitle_item = FileItem(
storage="local",
type="file",
path=(target_dir / "Demo.Show.S01E01.CHS.srt").as_posix(),
name="Demo.Show.S01E01.CHS.srt",
extension="srt",
)
transfer_histories = [SimpleNamespace(
status=True,
src="/download/影视下载/下载中/Demo.Show/Demo.Show.S01E01.1080p.WEB-DL.mkv",
dest="/media/电视剧/Example/Season 01/Example - S01E01.mkv",
)]
class _FakeTorrentHelper:
"""提供固定的多文件种子目录名。"""
def get_fileinfo_from_torrent_content(self, _content):
"""返回多文件种子目录名。"""
return "Demo.Show", []
class _FakeStorageChain:
"""模拟源目录被整理清理并能重新创建。"""
def get_file_item(self, storage, path):
"""记录源目录检查并报告其不存在。"""
accessed_paths.append((storage, path))
return None
def get_folder(self, storage, path):
"""仅重建原下载目录。"""
created_dirs.append((storage, path))
return FileItem(
storage=storage,
type="dir",
path=path.as_posix(),
name=path.name,
)
def list_files(self, _folder, recursion=False):
"""提供字幕文件,并确认保存操作先于队列提交。"""
assert recursion is False
assert "saved" in operation_order
operation_order.append("list")
return [subtitle_item]
class _FakeTransferChain:
"""记录标准整理队列提交。"""
def do_transfer(self, **kwargs):
"""保存自动整理请求参数。"""
transfer_calls.append(kwargs)
operation_order.append("queued")
return True, "已添加到整理队列"
monkeypatch.setattr(download_subtitle, "TorrentHelper", _FakeTorrentHelper)
monkeypatch.setattr(download_subtitle, "StorageChain", _FakeStorageChain)
monkeypatch.setattr(download_subtitle.time, "sleep", lambda _seconds: None)
from app.chain.transfer import facade as transfer_facade
monkeypatch.setattr(transfer_facade, "TransferChain", _FakeTransferChain)
chain = DownloadChain.__new__(DownloadChain)
chain.runtime_config = SimpleNamespace(
download_subtitle=True,
proxy=None,
temporary_path=Path("/tmp/moviepilot-test"),
subtitle_extensions=tuple(settings.RMT_SUBEXT),
media_extensions=tuple(settings.RMT_MEDIAEXT),
user_agent="MoviePilotTest",
)
chain.list_torrents = MagicMock(return_value=[DownloaderTorrent(
hash="hash123",
downloader="qb",
content_path="/download/影视下载/下载中/Demo.Show",
)])
chain.transfer_history_repository = SimpleNamespace(
list_by_hash=MagicMock(return_value=transfer_histories),
)
chain._site_subtitle_links = MagicMock(return_value=["https://example.com/subtitle.srt"])
class _FakeResponse:
"""为字幕下载回调提供可关闭的成功响应。"""
status_code = 200
content = b"subtitle-content"
def close(self):
"""模拟释放网络响应。"""
class _FakeHttp:
"""返回固定字幕响应,避免真实网络请求。"""
def get(self, *_args, **_kwargs):
"""返回成功响应。"""
return _FakeResponse()
monkeypatch.setattr(
download_subtitle,
"_download_ports_snapshot",
lambda: (_FakeHttp(), object()),
)
def _save_response(**kwargs):
"""记录字幕已保存到重新创建的原下载目录。"""
assert kwargs["working_dir_item"].path == target_dir.as_posix()
operation_order.append("saved")
chain._save_site_subtitle_response = MagicMock(side_effect=_save_response)
mediainfo = SimpleNamespace(type=MediaType.TV)
context = Context(
media_info=mediainfo,
torrent_info=TorrentInfo(page_url="https://example.com/torrent/1"),
)
chain.download_site_subtitles(
context=context,
download_dir=Path("/download/影视下载/下载中"),
torrent_content=b"torrent-content",
download_hash="hash123",
downloader="qb",
)
assert accessed_paths == [("local", target_dir)]
assert created_dirs == [("local", target_dir)]
chain.transfer_history_repository.list_by_hash.assert_called_once_with("hash123")
chain._site_subtitle_links.assert_called_once_with(context)
chain._save_site_subtitle_response.assert_called_once()
assert operation_order == ["saved", "list", "queued"]
assert len(transfer_calls) == 1
assert transfer_calls[0] == {
"fileitem": subtitle_item,
"mediainfo": mediainfo,
"mtype": MediaType.TV,
"downloader": "qb",
"download_hash": "hash123",
"background": True,
}
def test_download_single_supplements_category_before_download_event(monkeypatch):
"""下载事件和目录选择前应已有 TMDB 分类,同时保留原识别源身份。"""
captured = {}