Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(player): SMB 作品的同目录字幕经共享列目录发现

longmeidao0baa03b5b0b2
diff --git a/src/peach/subtitles.py b/src/peach/subtitles.py
index 8a8bacd0b..d71099f86 100644
--- a/src/peach/subtitles.py
+++ b/src/peach/subtitles.py
@@ -259,12 +259,20 @@ def file_key(name: str) -> str:
def discover_sidecars(video: Path, video_suffixes: set[str]) -> list[dict]:
"""读取正片所在目录,返回配给当前视频的字幕,不写账本。"""
+ from .smb_media import SmbResource
+
+ def wanted(name: str) -> bool:
+ return Path(name).suffix.lower() in video_suffixes or bool(subtitle_format(name))
+
try:
- with os.scandir(video.parent) as entries:
- names = [entry.name for entry in entries
- if (Path(entry.name).suffix.lower() in video_suffixes
- or subtitle_format(entry.name))
- and entry.is_file(follow_symlinks=False)]
+ if isinstance(video, SmbResource):
+ # SMB 共享上的作品不是操作系统路径,经共享列目录;传输层错误已映射为 OSError。
+ names = [entry.name for entry in video.parent.scandir()
+ if wanted(entry.name) and not entry.is_dir and not entry.is_symlink]
+ else:
+ with os.scandir(video.parent) as entries:
+ names = [entry.name for entry in entries
+ if wanted(entry.name) and entry.is_file(follow_symlinks=False)]
except OSError:
return []
videos = [name for name in names if Path(name).suffix.lower() in video_suffixes]
diff --git a/tests/test_subtitles.py b/tests/test_subtitles.py
index 555b5320b..fb9620807 100644
--- a/tests/test_subtitles.py
+++ b/tests/test_subtitles.py
@@ -216,6 +216,32 @@ class ScanTests(unittest.TestCase):
self.assertEqual(len(self._rows()), 3)
+class SmbDiscoveryTests(unittest.TestCase):
+ """SMB 共享上的作品不是操作系统路径,同目录字幕经 SMB 列目录发现。"""
+
+ def _video(self):
+ from peach.smb_media import SmbResource
+ from peach.smb_transport import SmbConnection
+ return SmbResource("nas", r"R:\Media", "smb://files.invalid/media", ("ABP-758", "ABP-758.mp4"),
+ SmbConnection("files.invalid", "media"))
+
+ def test_sidecars_next_to_an_smb_video_are_listed_through_the_share(self):
+ from peach.smb_media import SmbResource
+ from peach.smb_transport import SmbEntry
+ listing = [SmbEntry(9, 1, 1, False, "ABP-758.mp4"), SmbEntry(3, 1, 1, False, "ABP-758.chs.srt"),
+ SmbEntry(3, 1, 1, False, "OTHER-1.srt"), SmbEntry(0, 1, 1, True, "ABP-758.srt"),
+ SmbEntry(3, 1, 1, False, "ABP-758.ass", True)]
+ with mock.patch.object(SmbResource, "scandir", return_value=listing):
+ found = subtitles.discover_sidecars(self._video(), scan.VIDEO)
+ self.assertEqual([(row["name"], row["path"], row["language"]) for row in found],
+ [("ABP-758.chs.srt", r"R:\Media\ABP-758\ABP-758.chs.srt", "zh-Hans")])
+
+ def test_an_unreachable_share_yields_no_sidecars(self):
+ from peach.smb_media import SmbResource
+ with mock.patch.object(SmbResource, "scandir", side_effect=OSError(113, "SMB 连接不可用")):
+ self.assertEqual(subtitles.discover_sidecars(self._video(), scan.VIDEO), [])
+
+
class DecodeTests(unittest.TestCase):
def test_a_bom_names_the_encoding(self):
self.assertEqual(subtitles.decode("台词".encode("utf-8-sig")), "台词")