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
This commit is contained in:
Estrella Pan
2026-04-19 12:56:29 +02:00
parent d849edf89f
commit 4e9d8b34ab
2 changed files with 34 additions and 3 deletions

View File

@@ -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

View File

@@ -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."""