Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(downloads): 保存渠道探测失败阶段与脱敏原因

longmeidao957ed2d962c2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 540a0305e..07889f5b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ Removed / Fixed / Security。
- **配置**:同源检查统一识别 HTTP、HTTPS 的默认端口,避免地址显式包含 `:80` 或 `:443` 时被误拒绝。
- **馆藏**:处理失败后的线程清理完成前保留停止中状态,避免立即重试撞上仍持有的处理锁;起步失败时显示本次错误,不再退回上一轮结果。
+- **采集**:云下载能力未取得时,在本机诊断记录保留令牌、挂载点或目录探测阶段及失败分类,便于区分配置与网络问题。
## [0.51.0] - 2026-10-28
diff --git a/src/peach/downloads_channels.py b/src/peach/downloads_channels.py
index caa20f937..87883a3fd 100644
--- a/src/peach/downloads_channels.py
+++ b/src/peach/downloads_channels.py
@@ -14,6 +14,7 @@ from .media_sources import SOURCES
from .clouddrive_proxy import media_remote_path
from .push_discovery import normalise_prefix
from .follow_secrets import CredentialError
+from .error_reporting import local_event
def root_key(value):
@@ -68,8 +69,10 @@ class LibraryChannels:
if logged_in and direct_root and direct_root[0] == "pikpak" and config.targets.get("pikpak"):
rows.append(self._row("pikpak", "pikpak", direct_root[1], config.targets["pikpak"], names))
if roots and config.clouddrive_address and token:
+ phase = 'connect'
try:
with self.client(config.clouddrive_address, token, timeout=PROBE_TIMEOUT) as client:
+ phase = 'token_permissions'
permissions = client.token_info().permissions
if not (permissions.allow_add_offline_download and permissions.allow_list_offline_downloads):
problems.append("CloudDrive2 令牌需要提交和查看离线任务权限。")
@@ -78,8 +81,10 @@ class LibraryChannels:
if getattr(permissions, "allow_get_mounts", False):
try:
mounts = client.call("GetMountPoints", Empty(), "读取媒体库挂载点").mountPoints
- except DownloadError:
- pass
+ except DownloadError as error:
+ local_event('download-channel-probe', error, operation='mount_points',
+ source='clouddrive', failure=error.failure)
+ phase = 'library_mapping'
candidates = self._candidates(roots, prefixes, mounts, rows, names, problems)
seen = set()
for key, source, root, target, prefix in candidates:
@@ -92,9 +97,13 @@ class LibraryChannels:
rows.append(self._row(key, source, root, target, names, cloud_root=prefix,
can_cancel=folder.can_manage and bool(
permissions.allow_modify_offline_downloads)))
- except DownloadError:
+ except DownloadError as error:
+ local_event('download-channel-probe', error, operation='folder_capabilities',
+ source=source, failure=error.failure)
problems.append(f"{names.get(root_key(root)) or SOURCES[source].name}的云下载能力未取得。")
- except DownloadError:
+ except DownloadError as error:
+ local_event('download-channel-probe', error, operation=phase,
+ source='clouddrive', failure=error.failure)
problems.append("CloudDrive2 云下载能力未取得,请检查连接与令牌。")
return {"providers": rows, "problems": list(dict.fromkeys(problems))}
diff --git a/tests/test_downloads_channels.py b/tests/test_downloads_channels.py
index 3627ae623..324324848 100644
--- a/tests/test_downloads_channels.py
+++ b/tests/test_downloads_channels.py
@@ -1,7 +1,10 @@
"""媒体库渠道、只读能力探测与缓存的隔离回归。"""
from types import SimpleNamespace
+import json
+from pathlib import Path
+import tempfile
import unittest
-from unittest.mock import Mock
+from unittest.mock import Mock, patch
from peach.downloads import DownloadConfig, DownloadError, DownloadTask, Landing, REMOTE_DONE
from peach.downloads_channels import LibraryChannels
@@ -11,6 +14,35 @@ from peach.follow_secrets import CredentialError
class LibraryChannelTests(unittest.TestCase):
+ def test_unavailable_capabilities_leave_phase_and_failure_evidence_without_exposing_credentials(self):
+ from peach import error_reporting
+ for operation in ('token_permissions', 'folder_capabilities', 'mount_points'):
+ with self.subTest(operation=operation), tempfile.TemporaryDirectory() as directory:
+ self.setUp()
+ error = DownloadError('config', 'Authorization: Bearer test-only-token')
+ if operation == 'token_permissions':
+ self.client.token_info.side_effect = error
+ elif operation == 'folder_capabilities':
+ self.client.folder.side_effect = error
+ else:
+ self.client.token_info.return_value.permissions.allow_get_mounts = True
+ self.client.call.side_effect = error
+ with patch.object(error_reporting, '_log_dir', Path(directory)):
+ result = self.catalog()
+ records = [json.loads(line) for path in Path(directory).glob('errors-*.log')
+ for line in path.read_text(encoding='utf-8').splitlines()]
+ record = next(row for row in records if row.get('operation') == operation)
+ self.assertEqual(record['stage'], 'download-channel-probe')
+ self.assertEqual(record['failure'], 'config')
+ self.assertTrue(record['exception'])
+ self.assertNotIn('test-only-token', json.dumps(records))
+ self.assertNotIn('test-only-token', str(result))
+ if operation == 'mount_points':
+ self.assertEqual(len(result['providers']), 1, '可用的路径映射仍应继续探测')
+ else:
+ self.assertEqual(result['providers'], [])
+ self.assertTrue(result['problems'])
+
def test_a_broken_clouddrive_credential_keeps_a_healthy_pikpak_channel(self):
self.credentials.load.side_effect = CredentialError("test-only-secret-detail")
self.credentials.describe.return_value = {"fields": ["refresh_token"]}