From 4e9d8b34ab2562599c1fdf376863865bfa234b15 Mon Sep 17 00:00:00 2001 From: Estrella Pan Date: Sun, 19 Apr 2026 12:56:29 +0200 Subject: [PATCH] fix(downloader): parse Windows qB save_path on Linux hosts (#1016) When AB runs in a Linux container and qBittorrent runs on a Windows host, qB returns save_path strings with backslash separators. PurePosixPath treats those as a single unsplittable segment, so _path_to_bangumi never matched "Season N" and every non-S1 bangumi collapsed to Season 1. PureWindowsPath accepts both "\\" and "/" as separators, so using it for the parsing side handles both layouts. Path generation (_gen_save_path / _join_path) still uses the platform-conditional Path alias, so AB keeps emitting native paths for the host it runs on. Diagnosis and fix credited to the reporter in #1016. Closes #1016 --- backend/src/module/downloader/path.py | 9 ++++++--- backend/src/test/test_path_parser.py | 28 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/backend/src/module/downloader/path.py b/backend/src/module/downloader/path.py index 6e6caf74..af5232db 100644 --- a/backend/src/module/downloader/path.py +++ b/backend/src/module/downloader/path.py @@ -1,6 +1,7 @@ import logging import re from os import PathLike +from pathlib import PureWindowsPath from module.conf import PLATFORM, settings from module.models import Bangumi, BangumiUpdate @@ -36,9 +37,11 @@ class TorrentPath: @staticmethod def _path_to_bangumi(save_path: PathLike[str] | str, torrent_name: str = ""): - # Split save path and download path - save_parts = Path(save_path).parts - download_parts = Path(settings.downloader.path).parts + # Use PureWindowsPath regardless of the host AB runs on: it accepts + # both "\" and "/" separators, so a qBittorrent-on-Windows save_path + # reaching a Linux AB still splits into segments correctly (#1016). + save_parts = PureWindowsPath(save_path).parts + download_parts = PureWindowsPath(settings.downloader.path).parts # Get bangumi name and season bangumi_name = "" season = 1 diff --git a/backend/src/test/test_path_parser.py b/backend/src/test/test_path_parser.py index 9221b69f..afb841a4 100644 --- a/backend/src/test/test_path_parser.py +++ b/backend/src/test/test_path_parser.py @@ -13,6 +13,34 @@ def test_path_to_bangumi(): assert season == 2 +def test_path_to_bangumi_windows_style_save_path(): + """Regression for #1016: when qBittorrent runs on Windows and AB runs on + Linux, qB returns backslash paths. PurePosixPath treats the whole string + as one segment, leaving season stuck at 1.""" + from module.downloader.path import TorrentPath + + with patch("module.downloader.path.settings") as mock_settings: + mock_settings.downloader.path = r"D:\video\Bangumis" + path = r"D:\video\Bangumis\小书痴的下克上\Season 4" + bangumi_name, season = TorrentPath._path_to_bangumi(path) + + assert bangumi_name == "小书痴的下克上" + assert season == 4 + + +def test_path_to_bangumi_posix_path_on_linux_ab(): + """Regression guard: POSIX paths still parse correctly after the fix.""" + from module.downloader.path import TorrentPath + + with patch("module.downloader.path.settings") as mock_settings: + mock_settings.downloader.path = "/downloads/Bangumi" + path = "/downloads/Bangumi/葬送的芙莉莲/Season 2" + bangumi_name, season = TorrentPath._path_to_bangumi(path) + + assert bangumi_name == "葬送的芙莉莲" + assert season == 2 + + class TestGenSavePath: """Tests for TorrentPath._gen_save_path with season_offset."""