Peach: Local-first video metadata scraper and media manager.
feat(media): 展示逐盘采集流量并统一处理强度控件
longmeidao12cd0d2668ad
diff --git a/.claude/skills/peach-web-ui/SKILL.md b/.claude/skills/peach-web-ui/SKILL.mdindex 85e87e25e..a206e2c96 100644--- a/.claude/skills/peach-web-ui/SKILL.md+++ b/.claude/skills/peach-web-ui/SKILL.md@@ -5,8 +5,8 @@ description: 在新增、修改或复核 Peach 页面、控件、提示、错误# Peach Web UI 复用门槛-最后复核:2026-10-18-证据来源:现有 UI 契约、`docs/TESTING.md`、2026-09-19 本机会话工具记录、jakubkrehel/skills 与 emilkowalski/skills 摘录、`docs/reference-snapshots/open-claude-ui.md`。+最后复核:2026-10-19+证据来源:现有 UI 契约、`docs/TESTING.md`、2026-09-19 本机会话工具记录、jakubkrehel/skills 与 emilkowalski/skills 摘录、`docs/reference-snapshots/open-claude-ui.md`、2026-10-19 用户要求样式修改先预览再测试。## 开工顺序@@ -84,6 +84,7 @@ description: 在新增、修改或复核 Peach 页面、控件、提示、错误## 验收门槛+- 样式修改完成后,运行测试前先启动并保留可访问的预览服务器,向用户提供地址与查看入口;用户明确只要图片时才仅提供截图。提供预览后再运行测试,不把测试通过作为首次预览的前提。- UI 标签、身份、反馈状态和搜索推荐属于语义契约:按实际影响验证数据层、组件或浏览器行为。推荐词上线前对真实 `/api/items` 验证至少一个命中,说明性后缀不得混入搜索词(坏例 `ABW 番号`,好例 `ABW`)。diff --git a/README.en.md b/README.en.mdindex ee6d7c6fd..30049e47d 100644--- a/README.en.md+++ b/README.en.md@@ -47,6 +47,8 @@ Watch history, favorites and settings stay on your computer. Scanning leaves oriScanning, follow updates and new-release metadata collection save their progress and continue unfinished items after the app restarts. See [task recovery](docs/OPERATIONS.md#长时任务断点续跑) for recovery boundaries.+Scanning and collection show a traffic estimate before starting, then usage and estimated totals grouped by each media folder's proxy traffic protection setting. See [configuration](docs/OPERATIONS.md#配置页) for measurement coverage and estimation limits.+The combined "Send version statistics and error diagnostics (recommended)" option is checked by default. Clear it during installation, first setup or in settings to disable both. Saved opt-out choices are preserved. While enabled, it sends the listed technical fields to Sentry; full logs, diagnostic ZIPs, media details and watch history are excluded. See [remote diagnostics](docs/DIAGNOSTICS.md#可选远程诊断).Sharing library and taste details is a separate option in the installer and settings, off by default. Review the complete fields and local preview before explicitly consenting to weekly snapshots of works, tags, ratings, viewing summaries and every preference dimension. Account, device and installation identifiers are excluded. Detailed snapshots can still be linked or re-identified, and the receiving service can see the connection IP. See the [diagnostics reference](docs/DIAGNOSTICS.md#馆藏与口味群体研究).diff --git a/README.md b/README.mdindex c187ed9da..7d5a3d313 100644--- a/README.md+++ b/README.md@@ -47,6 +47,8 @@ https://github.com/user-attachments/assets/a5610874-e611-41bb-be22-7d99f0d64b62扫描、关注更新和新作资料采集会保存进度,应用重启后继续处理未完成的项目。恢复范围见[断点续跑](docs/OPERATIONS.md#长时任务断点续跑)。+扫描与采集启动前可查看流量粗估,运行中显示已用流量和预计总流量,按各盘的代理流量保护设置分别汇总。统计范围与估算限制见[运行与配置](docs/OPERATIONS.md#配置页)。+「发送版本统计与错误诊断(推荐)」默认勾选,可在安装、首次设置或设置页取消。两类技术事件由同一开关控制,已保存的关闭选择会保留。启用后向 Sentry 发送页面列出的技术字段,不自动上传完整日志、诊断 ZIP、媒体信息或观看记录。见[远程诊断说明](docs/DIAGNOSTICS.md#可选远程诊断)。馆藏与口味明细上传在安装器和设置页另设独立选项,默认关闭。设置页先展示完整字段与本机预览,经明确同意后,每周上传作品、标签、评分、观看汇总及全部偏好维度的明细;不含账号、设备或安装标识。明细仍有跨次关联与再识别风险,接收服务可见连接 IP。见[馆藏与口味群体研究](docs/DIAGNOSTICS.md#馆藏与口味群体研究)。diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.mdindex fb3c17c68..e4ebcecbc 100644--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@ -196,6 +196,8 @@ Peach 配置页地址是 `/configuration`。从管理菜单或设置弹窗的「媒体修复在「数据管理」页,订阅源在「关注管理」页的「订阅源」页签(分工见 [ADR-0050](adr/0050-settings-modal-holds-values-pages-hold-work.md))。+「扫描与采集」启动前显示流量粗估;运行中的任务面显示已用流量与预计总流量,可展开查看带网盘图标的各盘明细,连接方式显示「直连」或「代理」,区间使用 `6-12MB` 格式。媒体目录按各自的「代理流量保护」设置分别汇总,开启的目录计入代理流量估算。本地磁盘读取不计入网络流量;资料站下载按来源连接设置另列。挂载读取量受缓存和预读影响,浏览器与外部采集器的流量也无法完整统计,读数作估算参考。新目录尚未登记的文件在扫描后更新预估,暂停和应用重启保留同一任务的累计值。+常用的本机设置:- 「通用 → 开机自启」:开机后启动 Peach、静默启动(开机后只显示托盘图标,不打开网页)、在桌面创建快捷方式。diff --git a/docs/REUSE.md b/docs/REUSE.mdindex f4c820c66..ef8183918 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -106,6 +106,12 @@ Folia(`chthollyphile/folia-major`,AGPL-3.0,revision `9a01e788ed52d25ea01b1脚本路径读取 `settings_file` 的目录配置。`sync_sha1_115.py` 的 Cookie 默认位于 `secrets/115-cookies.txt`,复核 CSV 默认位于 `review/115-sha1重复.csv`;可通过 `--cookie-file`、`--dupes-out` 或对应的 `PEACH_115_COOKIE_FILE`、`PEACH_115_DUPES_OUT` 指定现有文件。目录排序偏好使用可重复的 `--deprioritize-dir`。TLS 两种 shell 共用 `resolve_tls_directory.py`,显式 `-OutDir`/`PEACH_TLS_DIR` 优先;`rehome_unknown_jav.py` 的目标账本根由 `--ledger-unknown-root` 明确指定。+## 馆藏处理流量++馆藏处理的流量分账复用 `media_sources.cost_for` 的最长路径前缀与逐目录保护设置、`ContextVar`、现有任务快照和断点恢复。HTTP 下载复用固定版 HTTPX 0.28.1(BSD-3-Clause)的 [`num_bytes_downloaded`](https://www.python-httpx.org/advanced/clients/#monitoring-download-progress),按压缩传输字节累计,失败的部分响应保留读数;不使用解压后的正文大小替代下载量。FFmpeg 输入读取复用 verbose 级别的 AVIO Statistics,NFO 与配套图片累计实际应用读取量。挂载缓存、预读与系统协议开销无法由这些读数推导,媒体读数仅作流量估算;浏览器、amane 与 curl 指纹传输保留未观测标记,不填零。预估只查询账本,不遍历或下载媒体;资料按每部 0.25–8 MiB、配套图片至多 2 MiB、待探测媒体 1–32 MiB、九帧至多 288 MiB 粗估,文件大小限制媒体项,保护开启的目录不计自动探测。估算是 Peach 的任务策略,不是上游流量保证;读数按任务保存,逐目录明细与代理、全部合计共用一份状态。界面复用 React Aria Slider、现有 `ProcessingEffort` 与 `formModal`;无新增依赖。HTTPX 官方接口已用压缩响应和中断流做隔离实证,未接入机器全局流量监控,以免混入其他应用与任务。++明细开合复用设置页 `Disclosure` 与 `setCollapseOpen`,可由任务提示同步收起;任务正文的网格高度与同一枚箭头沿用 Collapse 的 200ms 节奏。全局减少动态效果设置关闭这些过渡。网盘标识复用 `SourceLabel`,115、PikPak 等使用已登记的站标。+## Windows 常驻托盘诊断报告完成窗口复用 Windows `DialogBoxIndirectParamW` 与 `EndDialog` 的系统消息循环,控件和回调声明指针宽度;导出放在独立单实例线程,托盘消息回调可返回。原生隔离进程验证「打开目录」「打开文件」「关闭」、标题栏关闭、Esc 与 Enter,不操作现有托盘。未采用需要 common-controls v6 激活上下文的 `TaskDialogIndirect`,也未为单个窗口引入 GUI 依赖或 PowerShell 子进程;无新增依赖。diff --git a/frontend/e2e/library-processing-controls.test.ts b/frontend/e2e/library-processing-controls.test.tsindex 063375bd0..8250dca53 100644--- a/frontend/e2e/library-processing-controls.test.ts+++ b/frontend/e2e/library-processing-controls.test.ts@@ -16,10 +16,17 @@ describe('扫描与采集控制', () => {{ id: 'collect', label: '采集外部资料与封面', status: 'current' }],current_asset_name: '', current_action: '', stage: '',stalled: false, waited_seconds: 0,+ traffic: { used_bytes: 0, estimated_min_bytes: 1048576, estimated_max_bytes: 8388608,+ metered_used_bytes: 0, metered_estimated_min_bytes: 0, metered_estimated_max_bytes: 4194304,+ partial: true, unknown_files: true,+ sources: [{ key: 'drive-a', label: '示例网盘 A', root: 'A:\\Media', location: '115', metered: true,+ used_bytes: 0, estimated_min_bytes: 0, estimated_max_bytes: 4194304, unobserved: ['挂载缓存与预读未观测'] },+ { key: 'drive-b', label: '示例网盘 B', root: 'B:\\Media', location: 'pikpak', metered: false,+ used_bytes: 0, estimated_min_bytes: 1048576, estimated_max_bytes: 4194304, unobserved: ['挂载缓存与预读未观测'] }] },eta_kind: 'historical', eta_initial: true, eta_seconds_min: 1200, eta_seconds_max: 2400 };const writes: Record<string, unknown>[] = [];const opened = await visit(browser, '/data-cleanup', viewport, async page => {- await page.route('**/api/library-processing', async route => {+ await page.route('**/api/library-processing{,?*}', async route => {if (route.request().method() === 'POST') {const body = route.request().postDataJSON();writes.push(body);@@ -40,6 +47,7 @@ describe('扫描与采集控制', () => {try {await card.waitFor();await settle(page);+ assert.equal(await card.locator('[data-processing-controls]').count(), 0);await card.getByRole('button', { name: '同步媒体库', exact: true }).click();const dialog = page.getByRole('dialog', { name: '同步媒体库', exact: true });await dialog.waitFor();@@ -48,18 +56,51 @@ describe('扫描与采集控制', () => {const modalLayout = await layout(page);assert.ok(modalLayout.scrollWidth <= modalLayout.viewportWidth, JSON.stringify(modalLayout));assert.deepEqual(modalLayout.offenders, []);- const optionFits = await dialog.locator('label').evaluateAll(labels => labels.every(label => {- const text = label.querySelector('span[aria-hidden="true"]')!.getBoundingClientRect();- const box = label.getBoundingClientRect();- return text.top >= box.top && text.bottom <= box.bottom && text.right <= box.right;- }));- assert.equal(optionFits, true, '处理模式文字应保持单行并落在控件内');+ await dialog.getByText('预计总流量', { exact: true }).waitFor();+ const centering = await dialog.locator('.processing-effort-panel').evaluate(el => {+ const panel = el.getBoundingClientRect();+ const modal = el.closest('dialog')!.getBoundingClientRect();+ return Math.abs((panel.left + panel.right) / 2 - (modal.left + modal.right) / 2);+ });+ assert.ok(centering < 1, `处理强度面板居中偏差 ${centering}px`);+ const footer = await dialog.locator('.geist-modal-footer').evaluate(el => ({ display: getComputedStyle(el).display, padding: getComputedStyle(el).padding }));+ assert.equal(footer.display, 'flex');+ assert.notEqual(footer.padding, '0px');+ const disclosure = dialog.locator('summary');+ assert.equal(await disclosure.getAttribute('aria-expanded'), 'false');+ assert.equal(await disclosure.locator('svg').count(), 1);+ await page.emulateMedia({ reducedMotion: 'no-preference' });+ await dialog.getByText('查看明细', { exact: true }).click();+ assert.equal(await disclosure.getAttribute('aria-expanded'), 'true');+ const sources = dialog.locator('[data-processing-traffic] li');+ assert.match(await sources.nth(0).innerText(), /示例网盘 A[\s\S]*代理/);+ assert.match(await sources.nth(1).innerText(), /示例网盘 B[\s\S]*直连/);+ assert.equal(await sources.locator('svg, img').count(), 2);+ assert.equal((await dialog.innerText()).includes('未观测'), false);+ assert.match(await dialog.innerText(), /1-8MB/);+ const collapse = dialog.locator('.ui-fcollapse');+ const transition = await collapse.evaluate(el => ({ property: getComputedStyle(el).transitionProperty, duration: getComputedStyle(el).transitionDuration }));+ assert.equal(transition.property, 'height');+ assert.notEqual(transition.duration, '0s');+ await page.emulateMedia({ reducedMotion: 'reduce' });+ assert.equal(await collapse.evaluate(el => getComputedStyle(el).transitionProperty), 'none');+ await page.emulateMedia({ reducedMotion: 'no-preference' });+ await disclosure.click();+ await page.waitForFunction(() => !document.querySelector('dialog [data-processing-traffic] details')?.hasAttribute('open'));+ await disclosure.click();+ await page.emulateMedia({ reducedMotion: 'reduce' });+ const startupSlider = dialog.getByRole('slider');+ await startupSlider.press('Home');await dialog.screenshot({ path: path.join(artifacts, `${viewport.name}-startup.png`) });- await dialog.getByRole('radio', { name: '低占用', exact: true }).check();await dialog.getByRole('button', { name: '同步媒体库', exact: true }).click();await card.getByText('预计剩余约 20–40 分钟(粗估)', { exact: true }).waitFor();assert.equal(writes[0]?.speed_mode, 'low');const dock = page.locator('#toasts [data-processing-dock]');+ await dock.locator('[data-processing-traffic]').waitFor();+ state.traffic = { ...state.traffic, used_bytes: 2097152, metered_used_bytes: 1048576,+ estimated_min_bytes: 2097152, metered_estimated_min_bytes: 1048576, unknown_files: false,+ sources: state.traffic.sources.map(source => ({ ...source, used_bytes: 1048576, estimated_min_bytes: 1048576 })) };+ await dock.getByText('约 2MB', { exact: true }).waitFor();const pause = dock.getByRole('button', { name: '暂停', exact: true });assert.equal(await pause.innerText(), '');assert.equal(await pause.locator('svg').count(), 1);@@ -114,7 +155,7 @@ describe('扫描与采集控制', () => {assert.ok(writes.some(body => body.action === 'resume' && body.job_id === 'controlled'));assert.ok(writes.some(body => body.action === 'set_speed' && body.speed_mode === 'standard'));// 同步入口在后台任务期间保持忙态;这里只等待有限的控制请求结束。- await settle(page, 'section[aria-label="扫描与采集"] [data-processing-controls]');+ await settle(page, 'section[aria-label="扫描与采集"] [data-processing-traffic]');const measured = await layout(page);assert.ok(measured.scrollWidth <= measured.viewportWidth, JSON.stringify(measured));assert.deepEqual(measured.offenders, []);@@ -123,7 +164,28 @@ describe('扫描与采集控制', () => {await page.goto(new URL('/', page.url()).href);assert.equal(await page.locator('[data-library-completion]').count(), 0, '运行期间首页由常驻任务卡承接进度');await dock.getByRole('button', { name: '暂停', exact: true }).waitFor();+ await page.emulateMedia({ reducedMotion: 'no-preference' });+ await dock.getByText('查看明细', { exact: true }).click();+ assert.equal(await dock.locator('summary').getAttribute('aria-expanded'), 'true');+ const expandedHeight = (await dock.boundingBox())!.height;+ const noticeMotion = await dock.evaluate(el => ({+ frame: getComputedStyle(el.querySelector('.processing-notice-fold')!).transitionProperty,+ arrow: getComputedStyle(el.querySelector('[data-processing-toggle] > svg')!).transitionProperty,+ arrowDuration: getComputedStyle(el.querySelector('[data-processing-toggle] > svg')!).transitionDuration,+ }));+ assert.ok(noticeMotion.frame.includes('height'));+ assert.equal(noticeMotion.arrow, 'transform');+ assert.notEqual(noticeMotion.arrowDuration, '0s');await dock.getByRole('button', { name: '收起任务详情' }).click();+ assert.equal(await dock.locator('summary').getAttribute('aria-expanded'), 'false');+ await page.waitForFunction(() => !document.querySelector('#toasts [data-processing-dock] details')?.hasAttribute('open'));+ assert.ok((await dock.boundingBox())!.height < expandedHeight);+ await dock.getByRole('button', { name: '展开任务详情' }).click();+ assert.equal(await dock.locator('summary').getAttribute('aria-expanded'), 'false');+ await dock.getByRole('button', { name: '收起任务详情' }).click();+ await page.emulateMedia({ reducedMotion: 'reduce' });+ assert.equal(await dock.locator('.processing-notice-fold').evaluate(el => getComputedStyle(el).transitionProperty), 'none');+ assert.equal(await dock.locator('[data-processing-traffic]').isVisible(), true);const fixed = await dock.boundingBox();await dock.hover();await page.evaluate(() => window.scrollTo(0, document.documentElement.scrollHeight));diff --git a/frontend/src/react/library-processing/library-processing-card.tsx b/frontend/src/react/library-processing/library-processing-card.tsxindex 88517393f..3c79733a4 100644--- a/frontend/src/react/library-processing/library-processing-card.tsx+++ b/frontend/src/react/library-processing/library-processing-card.tsx@@ -28,8 +28,8 @@ import {} from './library-processing';import { useLibraryProcessingJob } from './use-library-processing';import { ProcessingActions } from './processing-actions';-import { ProcessingControls } from './processing-controls';import { ProcessingProgress } from './processing-line';+import { TrafficProgress } from './processing-traffic';/* 中文正文写成常量:JSX 里换行的文字会在接缝处多出一个空格,中文句子里看得见。卡片正文与首屏骨架共用 `SCAN_CARD_TEXT`(`management.ts`)。 */@@ -192,7 +192,7 @@ export function LibraryProcessingCard(props: LibraryProcessingProps) {{processingActive(state) ? <p className="text-body-2-regular text-text-secondary tabular-nums" data-processing-eta="">{remainingTime(state)}</p> : null}- <ProcessingControls state={state} disabled={start.isPending || job.isError} />+ {processingActive(state) ? <TrafficProgress traffic={state.traffic} /> : null}</div></Fieldset><Outcome state={state} problem={problem} settled={settled} onRetry={retry} toast={toast} />diff --git a/frontend/src/react/library-processing/library-processing-notice.tsx b/frontend/src/react/library-processing/library-processing-notice.tsxindex f0e65e6a6..58553b268 100644--- a/frontend/src/react/library-processing/library-processing-notice.tsx+++ b/frontend/src/react/library-processing/library-processing-notice.tsx@@ -12,6 +12,7 @@ import { ProcessingControls } from './processing-controls';import { useLibraryProcessingJob } from './use-library-processing';import { ProcessingLine } from './processing-line';import { CollapseChevron, CollapseRegion } from '../components/collapse-region';+import { TrafficProgress } from './processing-traffic';const COLLAPSE_KEY = 'peach.library-processing.collapsed';function expandedFor(jobId?: string): boolean {@@ -34,15 +35,17 @@ export function ProcessingNoticePanel({ state, problem = '', measure = false }:}) {const [expanded, setExpanded] = useState(() => expandedFor(state?.job_id));const bodyId = useId();+ const [trafficExpanded, setTrafficExpanded] = useState(false);const panel = useRef<HTMLElement>(null);const failed = state?.status === 'failed';const active = processingActive(state);- useEffect(() => { setExpanded(expandedFor(state?.job_id)) }, [state?.job_id]);+ useEffect(() => { setExpanded(expandedFor(state?.job_id)); setTrafficExpanded(false) }, [state?.job_id]);const toggle = () => {try {if (expanded && state?.job_id) sessionStorage.setItem(COLLAPSE_KEY, state.job_id);else sessionStorage.removeItem(COLLAPSE_KEY);} catch { /* 会话存储不可用时当前页面仍可收起 */ }+ if (expanded) setTrafficExpanded(false);setExpanded(!expanded);};useEffect(() => {@@ -67,6 +70,7 @@ export function ProcessingNoticePanel({ state, problem = '', measure = false }:failed ? 'border-current/35 text-text-error-primary': warning ? 'border-current/35 text-status-yellow-text': 'border-border-button-default text-text-primary')}>+ <div className="flex min-w-0 flex-col gap-3"><div className="flex min-w-0 items-center gap-3">{active && state?.total ? <Gauge value={state.checked || 0} max={state.total} />: failed || warning ? <RiErrorWarningLine aria-hidden className="size-5 shrink-0" />@@ -75,10 +79,12 @@ export function ProcessingNoticePanel({ state, problem = '', measure = false }:<p className="text-body-medium">{title}</p>{active ? <p data-processing-eta="" className="text-caption-1-regular text-text-secondary tabular-nums wrap-anywhere">{remainingTime(state!)}</p> : null}</div>- <Button size="small" variant="secondary" iconOnly leadingIcon={CollapseChevron}+ <Button size="small" variant="secondary" iconOnly leadingIcon={CollapseChevron} data-processing-toggle=""aria-label={expanded ? '收起任务详情' : '展开任务详情'} aria-expanded={expanded} aria-controls={bodyId} onClick={toggle} /></div>- <CollapseRegion id={bodyId} open={expanded}><div className="pt-3"><div className={cx('flex min-w-0 flex-col gap-2 border-t pt-3',+ <TrafficProgress traffic={state?.traffic} detailsOpen={trafficExpanded} onDetailsChange={setTrafficExpanded} />+ </div>+ <CollapseRegion id={bodyId} open={expanded} className="processing-notice-fold"><div className="pt-3"><div className={cx('flex min-w-0 flex-col gap-2 border-t pt-3',failed ? 'border-border-error-default/20' : warning ? 'border-current/20' : 'border-separator-border')}>{state?.steps?.length ? <ol aria-label="处理步骤" className="flex min-w-0 flex-col gap-1.5">{state.steps.map(step => <li key={step.id} data-processing-step={step.status}diff --git a/frontend/src/react/library-processing/library-processing.ts b/frontend/src/react/library-processing/library-processing.tsindex a3c024b8e..794bf7bd0 100644--- a/frontend/src/react/library-processing/library-processing.ts+++ b/frontend/src/react/library-processing/library-processing.ts@@ -68,8 +68,26 @@ export interface LibraryProcessingData {stalled?: boolean;waited_seconds?: number;completed_at?: number;+ traffic?: ProcessingTraffic;}+export interface TrafficSource {+ key: string; label: string; root: string; location: string; metered: boolean;+ used_bytes: number; estimated_min_bytes: number; estimated_max_bytes: number;+ unobserved: string[];+}+export interface ProcessingTraffic {+ used_bytes: number; estimated_min_bytes: number; estimated_max_bytes: number;+ metered_used_bytes: number; metered_estimated_min_bytes: number; metered_estimated_max_bytes: number;+ sources: TrafficSource[]; partial: boolean; unknown_files: boolean;+}++export const fetchProcessingEstimate = (command: LibraryProcessingCommand, signal?: AbortSignal) => {+ const params = new URLSearchParams({ estimate: '1', stage: command.stage || 'all' });+ if (command.asset_ids) params.set('asset_ids', JSON.stringify(command.asset_ids));+ return apiGet<{ traffic: ProcessingTraffic }>(`${LIBRARY_PROCESSING_URL}?${params}`, signal);+};+/** 一次写操作的请求体:指定处理阶段与影片,或者按任务号重试失败项。 */export type ProcessingSpeed = 'low' | 'standard' | 'high';export interface LibraryProcessingCommand {diff --git a/frontend/src/react/library-processing/processing-actions.tsx b/frontend/src/react/library-processing/processing-actions.tsxindex cf675bc8f..42d16df8a 100644--- a/frontend/src/react/library-processing/processing-actions.tsx+++ b/frontend/src/react/library-processing/processing-actions.tsx@@ -1,15 +1,41 @@import { useEffect, useRef, useState } from 'react';+import { createPortal } from 'react-dom';import { RiArrowDownSLine, RiDatabase2Line, RiGlobalLine, RiHardDrive2Line } from '@remixicon/react';import { Popover } from 'react-aria-components';import { Button } from '@/components/base/buttons/button';import { MENU_ITEM, MENU_ITEM_INTERACTIVE, MENU_POPOVER_SURFACE } from '@/components/base/dropdown/menu-styles';import { cx } from '@/utils/cx';-import { formModal, iconSwitchHtml } from '@peach/legacy/ui';+import { formModal } from '@peach/legacy/ui';import { MenuDialog } from '../components/menu-dialog';import { busyProps } from '../settings/use-action';-import type { LibraryProcessingCommand, ProcessingSpeed } from './library-processing';+import { errorMessage } from '../../api';+import { fetchProcessingEstimate, type LibraryProcessingCommand, type ProcessingSpeed, type ProcessingTraffic } from './library-processing';+import { ProcessingEffort } from './processing-effort';+import { TrafficProgress } from './processing-traffic';-export const RESOURCE_NOTICE_KEY = 'peach.library-processing.resource-notice.v1';+function ProcessingPrompt({ command, speed, dialog, onSpeed, busy }: {+ command: LibraryProcessingCommand; speed: ProcessingSpeed; dialog: ReturnType<typeof formModal>;+ onSpeed(value: ProcessingSpeed): void; busy: boolean;+}) {+ const [mode, setMode] = useState(speed);+ const [traffic, setTraffic] = useState<ProcessingTraffic>();+ const [failure, setFailure] = useState('');+ useEffect(() => {+ const controller = new AbortController();+ void fetchProcessingEstimate(command, controller.signal).then(result => {+ if (!result.traffic) throw new Error('服务端未提供流量预估');+ if (!controller.signal.aborted) setTraffic(result.traffic);+ }).catch(error => {+ if (!controller.signal.aborted) setFailure(errorMessage(error));+ }).finally(() => { if (!controller.signal.aborted) dialog.confirmButton.disabled = false });+ return () => controller.abort();+ }, [command, dialog]);+ return <div className="flex flex-col gap-4">+ <div className="flex justify-center"><ProcessingEffort value={mode} busy={busy} onChange={value => { setMode(value); onSpeed(value) }} /></div>+ {traffic ? <TrafficProgress traffic={traffic} initial />+ : <p role="status" className="text-body-2-regular text-text-secondary">{failure ? `流量预估未取得:${failure}` : '正在估算流量…'}</p>}+ </div>;+}/** 扫描和已有作品补全共用资料流水线,视频预览由媒体库设置决定。 */export function ProcessingActions({ busy, scan = false, metadataOnly = false, speedMode = 'standard', onRun }: {@@ -18,6 +44,8 @@ export function ProcessingActions({ busy, scan = false, metadataOnly = false, sp}) {const trigger = useRef<HTMLButtonElement>(null);const prompt = useRef<ReturnType<typeof formModal> | null>(null);+ const [promptView, setPromptView] = useState<{ host: HTMLElement; dialog: ReturnType<typeof formModal>; command: LibraryProcessingCommand } | null>(null);+ const selectedSpeed = useRef(speedMode);useEffect(() => () => prompt.current?.close(), []);const [open, setOpen] = useState(false);const primary: { label: string; icon: typeof RiDatabase2Line; command: LibraryProcessingCommand } = { label: scan ? '同步媒体库' : '同步这些影片', icon: RiDatabase2Line,@@ -31,32 +59,34 @@ export function ProcessingActions({ busy, scan = false, metadataOnly = false, spconst run = (command: LibraryProcessingCommand) => {if (busy) return;setOpen(false);- let acknowledged = false;- try { acknowledged = localStorage.getItem(RESOURCE_NOTICE_KEY) === 'seen' } catch { /* 存储不可用时保留提示 */ }- if (!scan || acknowledged || command.stage === 'metadata') {+ if (!scan) {void Promise.resolve(onRun(command)).catch(() => { /* 错误由任务入口留在原位 */ });return;}if (prompt.current) return;+ selectedSpeed.current = speedMode;const previews = command.stage === 'previews';- const title = previews ? '生成视频预览' : command.stage === 'scan' ? '扫描媒体文件' : '同步媒体库';+ const title = previews ? '生成视频预览' : command.stage === 'scan' ? '扫描媒体文件'+ : command.stage === 'metadata' ? '补全影片资料' : command.stage === 'collect' ? '同步已入库影片' : '同步媒体库';const dialog = formModal({title,description: previews? '生成视频预览会读取视频并占用处理器,可能让电脑变慢。需要同时使用电脑时,可选择低占用模式。'- : `处理会读取媒体文件${command.stage === 'scan' ? '' : metadataOnly ? '并下载资料' : '、下载资料并生成视频预览'},可能让电脑变慢。需要同时使用电脑时,可选择低占用模式。`,- body: iconSwitchHtml('processing-speed', '处理模式', [['low', '低占用'], ['standard', '标准'], ['high', '全速']], speedMode, { text: true }),+ : `处理会读取媒体文件${command.stage === 'scan' ? '' : metadataOnly || command.stage === 'metadata' ? '并下载资料' : '、下载资料并生成视频预览'},可能让电脑变慢。需要同时使用电脑时,可选择低占用模式。`,+ body: '<div class="peach-react" data-processing-prompt></div>',confirmLabel: title,+ confirmDisabled: true,onConfirm: async () => {- const selected = dialog.dialog.querySelector<HTMLInputElement>('input[name="processing-speed"]:checked')?.value;- await onRun({ ...command, speed_mode: selected === 'low' ? 'low' : selected === 'high' ? 'high' : 'standard' });- try { localStorage.setItem(RESOURCE_NOTICE_KEY, 'seen') } catch { /* 本次启动照常完成 */ }+ await onRun({ ...command, ...(selectedSpeed.current === speedMode ? {} : { speed_mode: selectedSpeed.current }) });},});prompt.current = dialog;- void dialog.done.then(() => { if (prompt.current === dialog) prompt.current = null });+ setPromptView({ host: dialog.dialog.querySelector<HTMLElement>('[data-processing-prompt]')!, dialog, command });+ void dialog.done.then(() => { if (prompt.current === dialog) { prompt.current = null; setPromptView(null) } });};return <>+ {promptView ? createPortal(<ProcessingPrompt command={promptView.command} dialog={promptView.dialog}+ speed={speedMode} busy={busy} onSpeed={value => { selectedSpeed.current = value }} />, promptView.host) : null}<span data-button-group data-split-button data-variant="primary"><Button leadingIcon={primary.icon} {...busyProps(busy)} onClick={() => run(primary.command)}>{primary.label}</Button><Button ref={trigger} iconOnly leadingIcon={RiArrowDownSLine} aria-label="更多同步方式"diff --git a/frontend/src/react/library-processing/processing-effort.tsx b/frontend/src/react/library-processing/processing-effort.tsxindex cf6beb0b9..ac93b0c3f 100644--- a/frontend/src/react/library-processing/processing-effort.tsx+++ b/frontend/src/react/library-processing/processing-effort.tsx@@ -39,24 +39,13 @@ function EffortFlow({ animate }: { animate: boolean }) {</div>;}-export function ProcessingMode({ value, busy, onChange, animate = true }: {+export function ProcessingEffort({ value, busy, onChange, animate = true }: {value: ProcessingSpeed; busy: boolean; onChange(value: ProcessingSpeed): void; animate?: boolean;}) {- const trigger = useRef<HTMLButtonElement>(null);- const [open, setOpen] = useState(false);const index = Math.max(0, MODES.indexOf(value));const [draft, setDraft] = useState(index);useEffect(() => { if (!busy) setDraft(index) }, [index, busy]);- return <>- <span className="processing-effort-trigger" data-speed={value}>- <Button ref={trigger} size="small" variant="secondary"- aria-label={`处理速度:${LABELS[index]}`}- aria-haspopup="dialog" aria-expanded={open} aria-disabled={busy}- leadingIcon={SPEED_ICONS[index]} trailingIcon={RiArrowDownSLine}- onClick={() => { if (!busy) setOpen(!open) }}>{LABELS[index]}</Button>- </span>- <Popover triggerRef={trigger} isOpen={open} onOpenChange={setOpen} placement="top start" offset={8} className={MENU_POPOVER_SURFACE}>- <MenuDialog aria-label="处理速度" className="processing-effort-panel flex flex-col gap-3 p-2 outline-none">+ return <div className="processing-effort-panel flex flex-col gap-3 p-2"><p className="text-body-medium"><span className="text-text-tertiary">处理强度 </span>{LABELS[draft]}</p><div className="processing-effort-scale text-caption-1-regular text-text-tertiary" aria-hidden="true"><span data-selected={draft === 0 || undefined}><Snail />最慢</span>@@ -76,6 +65,26 @@ export function ProcessingMode({ value, busy, onChange, animate = true }: {</div></Slider><div className="flex justify-between text-caption-1-regular text-text-tertiary"><span>占用少</span><span>占用多</span></div>+ </div>;+}++export function ProcessingMode({ value, busy, onChange, animate = true }: {+ value: ProcessingSpeed; busy: boolean; onChange(value: ProcessingSpeed): void; animate?: boolean;+}) {+ const trigger = useRef<HTMLButtonElement>(null);+ const [open, setOpen] = useState(false);+ const index = Math.max(0, MODES.indexOf(value));+ return <>+ <span className="processing-effort-trigger" data-speed={value}>+ <Button ref={trigger} size="small" variant="secondary"+ aria-label={`处理速度:${LABELS[index]}`}+ aria-haspopup="dialog" aria-expanded={open} aria-disabled={busy}+ leadingIcon={SPEED_ICONS[index]} trailingIcon={RiArrowDownSLine}+ onClick={() => { if (!busy) setOpen(!open) }}>{LABELS[index]}</Button>+ </span>+ <Popover triggerRef={trigger} isOpen={open} onOpenChange={setOpen} placement="top start" offset={8} className={MENU_POPOVER_SURFACE}>+ <MenuDialog aria-label="处理速度" className="outline-none">+ <ProcessingEffort value={value} busy={busy} onChange={onChange} animate={animate} /></MenuDialog></Popover></>;diff --git a/frontend/src/react/library-processing/processing-traffic.tsx b/frontend/src/react/library-processing/processing-traffic.tsxnew file mode 100644index 000000000..c89fb99cc--- /dev/null+++ b/frontend/src/react/library-processing/processing-traffic.tsx@@ -0,0 +1,45 @@+import { fmtSize } from '@peach/legacy/core';+import type { ProcessingTraffic } from './library-processing';+import { useOverlayScrollbar } from '../components/overlay-scrollbar';+import { SourceLabel } from '../components/source-label';+import { Disclosure, SourceMark } from '../settings/section';++const size = (value: number) => fmtSize(value).replace(/\s/g, '');+const range = (low: number, high: number) => {+ if (low === high) return size(high);+ const [start, startUnit] = fmtSize(low).split(' ');+ const [end, endUnit] = fmtSize(high).split(' ');+ return startUnit === endUnit ? `${start}-${end}${endUnit}` : `${size(low)}-${size(high)}`;+};++/** 确认弹窗、任务卡和管理页共用逐目录分账。 */+export function TrafficProgress({ traffic, initial = false, detailsOpen, onDetailsChange }: {+ traffic?: ProcessingTraffic; initial?: boolean; detailsOpen?: boolean; onDetailsChange?(open: boolean): void;+}) {+ const list = useOverlayScrollbar<HTMLUListElement>();+ if (!traffic) return null;+ return <div data-processing-traffic="" className="flex min-w-0 flex-col gap-2 text-caption-1-regular text-text-secondary tabular-nums wrap-anywhere">+ <dl className="inline-grid grid-cols-2 items-baseline gap-x-4 gap-y-1">+ {!initial ? <><dt>已用流量</dt><dd className="text-right text-text-primary">{traffic.partial ? '约 ' : ''}{size(traffic.used_bytes)}</dd></> : null}+ <dt>预计总流量</dt><dd className="text-right text-body-2-medium text-text-primary">{range(traffic.estimated_min_bytes, traffic.estimated_max_bytes)}</dd>+ <dt>其中代理流量</dt><dd className="text-right">{initial ? '' : `已用 ${size(traffic.metered_used_bytes)} / 预计 `}{range(traffic.metered_estimated_min_bytes, traffic.metered_estimated_max_bytes)}</dd>+ </dl>+ <Disclosure summary="查看明细" trailing isOpen={detailsOpen} onOpenChange={onDetailsChange}>+ <div className="relative min-w-0">+ <ul ref={list} className="flex max-h-48 flex-col overflow-y-auto overscroll-contain pr-2">+ {traffic.sources.map(source => <li key={source.key} className="flex min-w-0 flex-col gap-1 border-b border-separator-border py-2 last:border-b-0">+ <div className="flex min-w-0 items-start justify-between gap-2">+ {source.root ? <SourceLabel source={source.location} label={source.label} />+ : <span className="inline-flex min-w-0 items-center gap-1.5"><SourceMark mark="globe" />{source.label}</span>}+ <span className="shrink-0">{source.metered ? '代理' : '直连'}</span>+ </div>+ <div className="flex flex-wrap justify-between gap-x-4 text-text-tertiary">+ {!initial ? <span>已用{source.unobserved.length ? '约' : ''} {size(source.used_bytes)}</span> : null}+ <span>预计 {range(source.estimated_min_bytes, source.estimated_max_bytes)}</span>+ </div>+ </li>)}+ </ul>+ </div>+ </Disclosure>+ </div>;+}diff --git a/frontend/src/react/settings/section.tsx b/frontend/src/react/settings/section.tsxindex 2539e970b..6f56627bc 100644--- a/frontend/src/react/settings/section.tsx+++ b/frontend/src/react/settings/section.tsx@@ -3,7 +3,7 @@* 外框取 BoardUI 设置弹层的 `SettingsSectionLabel` + `SettingsCard`,开关行直接用 `SettingsRow`。* 注册表里没有折叠,它用 BoardUI token 组合,差异登记在 `boardui/ORIGIN.md`。* 提示与进度条另有页面共用的一份,在 `../components/`。 */-import { useId, useRef, useState } from 'react';+import { useId, useLayoutEffect, useRef, useState } from 'react';import type { ComponentType, FormEvent, MouseEvent, ReactNode } from 'react';import { RiArrowRightSLine, RiExternalLinkLine } from '@remixicon/react';import { setCollapseOpen } from '@peach/legacy/ui';@@ -116,14 +116,19 @@ export function Fact({ term, children }: { term: ReactNode; children: ReactNode/** 展开正文。原生 `details` 给键盘与无障碍语义;开合交给共用 Collapse 的 `setCollapseOpen`,* 它给正文外层挂上 `ui-kit/collapse.css` 的 `.ui-fcollapse` 让高度过渡。内边距放在里层,高度才能收到 0。 */export function Disclosure(- { summary, defaultOpen = false, children, trailing = false, onOpenChange, contentClassName }:- { summary: ReactNode; defaultOpen?: boolean; children: ReactNode; trailing?: boolean; onOpenChange?(open: boolean): void; contentClassName?: string },+ { summary, defaultOpen = false, isOpen, children, trailing = false, onOpenChange, contentClassName }:+ { summary: ReactNode; defaultOpen?: boolean; isOpen?: boolean; children: ReactNode; trailing?: boolean; onOpenChange?(open: boolean): void; contentClassName?: string },) {const id = useId();const details = useRef<HTMLDetailsElement>(null);const body = useRef<HTMLDivElement>(null);const primed = useRef(false);- const [open, setOpen] = useState(defaultOpen);+ const [open, setOpen] = useState(isOpen ?? defaultOpen);+ useLayoutEffect(() => {+ if (isOpen === undefined || isOpen === open || !details.current || !body.current) return;+ setCollapseOpen(details.current, body.current, isOpen);+ setOpen(isOpen);+ }, [isOpen, open]);const toggle = (event: MouseEvent) => {event.preventDefault();if (!details.current || !body.current) return;@@ -135,7 +140,7 @@ export function Disclosure(过渡里它会被重新画成展开。这里只在第一次挂上时把初始状态落到 DOM 上。 */const attach = (node: HTMLDetailsElement | null) => {details.current = node;- if (node && defaultOpen && !primed.current) {+ if (node && (isOpen ?? defaultOpen) && !primed.current) {primed.current = true;node.open = true;}diff --git a/frontend/test/react/library-processing.test.tsx b/frontend/test/react/library-processing.test.tsxindex 1fc361ce9..2da268392 100644--- a/frontend/test/react/library-processing.test.tsx+++ b/frontend/test/react/library-processing.test.tsx@@ -6,7 +6,7 @@* `getComputedStyle` 断言;这里只看结构、文字与请求。 */import { act } from 'react';import { notifyManager, QueryClientProvider } from '@tanstack/react-query';-import { afterEach, beforeEach, expect, it, vi } from 'vitest';+import { afterEach, expect, it, vi } from 'vitest';import { queryClient } from '../../src/react/query';import {@@ -14,7 +14,6 @@ import {} from '../../src/react/library-processing/library-processing';import { LibraryProcessingCard } from '../../src/react/library-processing/library-processing-card';import { LibraryProcessingNotice } from '../../src/react/library-processing/library-processing-notice';-import { RESOURCE_NOTICE_KEY } from '../../src/react/library-processing/processing-actions';import type { LibraryProcessingProps } from '../../src/react/bundle';import { buttonNamed as textButtonNamed, click, mount, mountRoot, submit } from './render';@@ -25,7 +24,6 @@ const buttonNamed = (name: string, root: ParentNode = document) =>// 客户端是模块级的单例(所有 React 根共用一个),用例之间不清就互相喂数据。// 报过的任务号记在 `localStorage` 里,也要清,否则下一条用例的通知被当成重复的。-beforeEach(() => localStorage.setItem(RESOURCE_NOTICE_KEY, 'seen'));afterEach(() => {queryClient.clear();localStorage.clear();@@ -43,6 +41,13 @@ type Reply = (method: string, body: unknown) => LibraryProcessingData;function serve(reply: Reply) {const calls: { method: string; body: unknown }[] = [];const fetcher = vi.fn(async (path: string, init?: RequestInit) => {+ if (path.startsWith('/api/library-processing?')) return {+ ok: true, status: 200, json: async () => ({ traffic: {+ used_bytes: 0, estimated_min_bytes: 1024, estimated_max_bytes: 2048,+ metered_used_bytes: 0, metered_estimated_min_bytes: 1024, metered_estimated_max_bytes: 2048,+ sources: [], partial: false, unknown_files: true,+ } }),+ };if (path !== '/api/library-processing') throw new Error(`用例没有给 ${path} 造数据`);const method = init?.method ?? 'GET';const body = init?.body ? JSON.parse(String(init.body)) : undefined;@@ -75,6 +80,16 @@ const tick = (ms: number) => act(async () => { await vi.advanceTimersByTimeAsyncconst menuItem = (label: string) =>[...document.querySelectorAll('[role="dialog"] button')].find((item) => item.textContent?.trim() === label);+const startFrom = async (element: Element | null | undefined) => {+ await click(element);+ await submit(document.querySelector('dialog form'));+};+const chooseLow = () => act(async () => {+ const slider = document.querySelector('dialog input[type="range"]')!;+ slider.dispatchEvent(new KeyboardEvent('keydown', { key: 'Home', bubbles: true }));+ slider.dispatchEvent(new KeyboardEvent('keyup', { key: 'Home', bubbles: true }));+});+it('来源说没有只报一个数,不算问题也不提供失败重试', async () => {await open({status: 'complete',@@ -99,16 +114,16 @@ it('扫描与补全方式共用一颗主键加一个下拉,各自交自己的const served = await open({ status: 'idle' }, () => ({ status: 'complete' }));const host = await mount(card({ toast: vi.fn() }));- await click(buttonNamed('同步媒体库', host));+ await startFrom(buttonNamed('同步媒体库', host));await click(host.querySelector('[data-split-button] [aria-haspopup="dialog"]'));expect(menuItem('同步媒体库')).not.toBeNull();- await click(menuItem('仅扫描文件'));+ await startFrom(menuItem('仅扫描文件'));await click(host.querySelector('[data-split-button] [aria-haspopup="dialog"]'));- await click(menuItem('仅同步已入库影片'));+ await startFrom(menuItem('仅同步已入库影片'));await click(host.querySelector('[data-split-button] [aria-haspopup="dialog"]'));- await click(menuItem('仅生成视频预览'));+ await startFrom(menuItem('仅生成视频预览'));await click(host.querySelector('[data-split-button] [aria-haspopup="dialog"]'));- await click(menuItem('仅补全资料'));+ await startFrom(menuItem('仅补全资料'));expect(served.posts()).toEqual([{}, { stage: 'scan' }, { stage: 'collect' }, { stage: 'previews' }, { stage: 'metadata' }]);expect(host.querySelector('a[href="/sources"]')?.textContent).toContain('来源和凭证');@@ -123,7 +138,7 @@ it('启动只提交一次,进度用 GET 读回来,完成后给出回执与: { status: 'complete', job_id: 'one', scanned: 9, identified: 4, candidates: 2 },);const host = await mount(card({ toast }));- await click(buttonNamed('同步媒体库', host));+ await startFrom(buttonNamed('同步媒体库', host));expect(served.calls.map((call) => call.method)).toEqual(['POST', 'GET']);expect(host.querySelector('a[href="/review"]')).not.toBeNull();@@ -148,7 +163,7 @@ it('补齐女优资料的读数进回执,冲突和未取得各自点名', asyn},);const host = await mount(card({ toast: vi.fn() }));- await click(buttonNamed('同步媒体库', host));+ await startFrom(buttonNamed('同步媒体库', host));expect(served.posts()).toEqual([{}]);expect(host.querySelector('[role="status"]')?.textContent).toContain(@@ -234,7 +249,7 @@ it('有可重试项时页脚那颗发的仍是整批,不夹带上一趟的失}, () => ({ status: 'running', job_id: 'two' }));const host = await mount(card({ toast: vi.fn() }));- await click(buttonNamed('同步媒体库', host));+ await startFrom(buttonNamed('同步媒体库', host));expect(served.posts()).toEqual([{}]);});@@ -388,8 +403,7 @@ it('来源说没有的计数带千分位,等待时长按分、时、天进位'.toBe('采集缺失封面 · 已等待 3 天 0 小时');});-it('首次整库任务先说明资源影响,确认模式后才启动,取消不启动', async () => {- localStorage.removeItem(RESOURCE_NOTICE_KEY);+it('整库任务先显示流量预估,确认模式后才启动,取消不启动', async () => {const served = await open({ status: 'idle', speed_mode: 'standard' }, () => ({ status: 'running', job_id: 'one' }));const host = await mount(card({ toast: vi.fn() }));await click(buttonNamed('同步媒体库', host));@@ -398,10 +412,11 @@ it('首次整库任务先说明资源影响,确认模式后才启动,取消await click(buttonNamed('取消', document.querySelector('dialog')!));expect(served.posts()).toEqual([]);await click(buttonNamed('同步媒体库', host));- await click(document.querySelector('dialog input[value="low"]'));+ expect(document.querySelector('dialog')?.textContent).toContain('预计总流量');+ expect(host.querySelector('[data-processing-controls]')).toBeNull();+ await chooseLow();await submit(document.querySelector('dialog form'));expect(served.posts()).toEqual([{ speed_mode: 'low' }]);- expect(localStorage.getItem(RESOURCE_NOTICE_KEY)).toBe('seen');expect(document.querySelector('dialog')).toBeNull();});@@ -417,7 +432,7 @@ it('暂停与继续使用同一个任务号,暂停不是完成,暂停时仍}return state;});- const host = await mount(card({ toast, onComplete }));+ const host = await mount(notice({ toast, onComplete, mode: 'notice' }));expect(host.textContent).toContain('预计剩余约 20–40 分钟(粗估)');await click(buttonNamed('暂停', host));expect(host.textContent).toContain('正在暂停,当前步骤结束后暂停');@@ -432,13 +447,13 @@ it('暂停与继续使用同一个任务号,暂停不是完成,暂停时仍expect(buttonNamed('暂停', host)).not.toBeNull();});-it('首屏暂停状态在卡片和横幅中保留进度与继续入口', async () => {+it('首屏暂停状态在卡片保留进度,在任务面提供继续入口', async () => {await open({ status: 'paused', job_id: 'one', checked: 2, total: 10 });const host = await mount(card({ toast: vi.fn() }));const banner = await mount(notice({ toast: vi.fn(), mode: 'notice' }));expect(host.textContent).toContain('已暂停');expect(banner.textContent).toContain('已暂停');- expect(buttonNamed('继续', host)).not.toBeNull();+ expect(buttonNamed('继续', host)).toBeNull();expect(buttonNamed('继续', banner)).not.toBeNull();expect(buttonNamed('标准', banner)).not.toBeNull();expect(host.querySelector('[role="progressbar"]')?.getAttribute('aria-valuenow')).toBe('2');@@ -449,7 +464,7 @@ it('模式切换失败保留服务端选中值和原位错误,可重试', asynvi.stubGlobal('fetch', vi.fn(async (_path: string, init?: RequestInit) => init?.method === 'POST'? { ok: false, status: 409, json: async () => ({ error: '任务已变化,请刷新后重试' }) }: { ok: true, status: 200, json: async () => ({ status: 'running', job_id: 'one', speed_mode: 'standard' }) }));- const host = await mount(card({ toast: vi.fn() }));+ const host = await mount(notice({ toast: vi.fn(), mode: 'notice' }));await click(buttonNamed('标准', host));const slider = document.querySelector<HTMLInputElement>('input[type="range"]')!;await act(async () => {@@ -493,17 +508,15 @@ it('扫描总量未知时显示已发现文件数,已知阶段显示完成数'});it('启动失败时提示弹窗保留模式选择和重试入口', async () => {- localStorage.removeItem(RESOURCE_NOTICE_KEY);await open({ status: 'idle' });vi.stubGlobal('fetch', vi.fn(async (_url: string, init?: RequestInit) => init?.method === 'POST'? { ok: false, status: 400, json: async () => ({ error: '媒体目录不可用' }) }: { ok: true, status: 200, json: async () => ({ status: 'idle' }) }));const host = await mount(card({ toast: vi.fn() }));await click(buttonNamed('同步媒体库', host));- await click(document.querySelector('dialog input[value="low"]'));+ await chooseLow();await submit(document.querySelector('dialog form'));expect(document.querySelector('dialog')?.textContent).toContain('媒体目录不可用');- expect(document.querySelector<HTMLInputElement>('dialog input[value="low"]')?.checked).toBe(true);- expect(localStorage.getItem(RESOURCE_NOTICE_KEY)).toBeNull();+ expect(document.querySelector<HTMLInputElement>('dialog input[type="range"]')?.value).toBe('0');expect(document.querySelector('[data-modal-confirm]')?.getAttribute('aria-busy')).toBeNull();});diff --git a/src/peach/browser_transport.py b/src/peach/browser_transport.pyindex 4aa3fd62b..c638ebf1f 100644--- a/src/peach/browser_transport.py+++ b/src/peach/browser_transport.py@@ -418,6 +418,8 @@ class BrowserTransport:atexit.register(self.close)def __call__(self, request: HttpRequest, timeout: float, max_bytes: int) -> HttpResponse:+ from .processing_traffic import unobserved+ unobserved('浏览器采集流量未观测')if request.method.upper() != "GET":raise BrowserUnavailable("浏览器传输只发 GET")with self._lock:diff --git a/src/peach/frame_capture.py b/src/peach/frame_capture.pyindex 442faeb40..1fd3ddab3 100644--- a/src/peach/frame_capture.py+++ b/src/peach/frame_capture.py@@ -47,7 +47,8 @@ def capture_frame(ffmpeg: str, path: str, timestamp: float, destination: Path, *那个时间点,两小时的片子抽最后一帧要几分钟。"""destination.unlink(missing_ok=True)- command = [ffmpeg, "-y", "-v", "error", "-rw_timeout", "8000000"]+ from .processing_traffic import CURRENT as TRAFFIC, media_statistics+ command = [ffmpeg, "-y", "-v", "verbose" if TRAFFIC.get() is not None else "error", "-rw_timeout", "8000000"]checkpoint('frame')command += ffmpeg_options(filter_option='-filter_threads')if color_override:@@ -61,12 +62,14 @@ def capture_frame(ffmpeg: str, path: str, timestamp: float, destination: Path, *completed = subprocess.run(command, capture_output=True, timeout=timeout,creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)except subprocess.TimeoutExpired as error:+ media_statistics(path, error.stderr)if diagnostics is not None:diagnostics.update(reason='timeout', returncode=None,stderr=(error.stderr or b'').decode('utf-8', 'replace'))destination.unlink(missing_ok=True)return False, "ffmpeg timeout"stderr = (completed.stderr or b'').decode('utf-8', 'replace')+ media_statistics(path, stderr)ok = completed.returncode == 0 and _readable_image(destination)if diagnostics is not None:diagnostics.update(reason='success' if ok elsediff --git a/src/peach/http.py b/src/peach/http.pyindex 6751ffe96..303a4d965 100644--- a/src/peach/http.py+++ b/src/peach/http.py@@ -214,7 +214,7 @@ class HttpxTransport:return HttpResponse(response.status_code,dict(response.headers),- _bounded_body(response.iter_bytes(), max_bytes),+ _bounded_body(_download_chunks(response), max_bytes),str(response.url),)target = response.url.join(location)@@ -241,6 +241,19 @@ class HttpxTransport:self.client.close()+def _download_chunks(response):+ from .processing_traffic import record_download+ counted = 0+ try:+ for chunk in response.iter_bytes():+ current = response.num_bytes_downloaded+ record_download(current - counted)+ counted = current+ yield chunk+ finally:+ record_download(response.num_bytes_downloaded - counted)++def _bounded_body(chunks, max_bytes: int) -> bytes:"""最多读 `max_bytes + 1` 字节:多出的那一个字节让调用方能判「超限」而不是「刚好」。"""kept: list[bytes] = []@@ -273,6 +286,8 @@ class CurlCffiTransport:timeout: float,max_bytes: int,) -> HttpResponse:+ from .processing_traffic import unobserved+ unobserved('浏览器指纹传输流量未观测')chunks: list[bytes] = []total = 0try:diff --git a/src/peach/library_nfo.py b/src/peach/library_nfo.pyindex 748703cc6..663828727 100644--- a/src/peach/library_nfo.py+++ b/src/peach/library_nfo.py@@ -90,6 +90,8 @@ def read_nfo(path: Path):"""保留完整原文;拒绝 DTD,并限制 XML 的输入体积。"""with path.open('rb') as handle:raw = handle.read(1024 * 1024 + 1)+ from .processing_traffic import record_media_read+ record_media_read(path, len(raw))# XML 的 UTF-16/32 编码包含零字节;声明检查同样覆盖这些编码。declarations = raw.replace(b'\x00', b'').upper()if len(raw) > 1024 * 1024 or b'<!DOCTYPE' in declarations or b'<!ENTITY' in declarations:diff --git a/src/peach/library_processing.py b/src/peach/library_processing.pyindex 2da5ed37a..6f1610648 100644--- a/src/peach/library_processing.py+++ b/src/peach/library_processing.py@@ -11,6 +11,7 @@ import timeimport uuidimport xml.etree.ElementTree as ETfrom contextlib import closing+from contextvars import copy_contextfrom concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETEDfrom dataclasses import dataclassfrom pathlib import Path@@ -42,6 +43,7 @@ from .media_sources import cost_forfrom .media_processing_policy import metadata_onlyfrom .strm import skip_background_readfrom .record_rehome import reattach_in+from .processing_traffic import ProcessingTrafficfrom .processing_control import (task_control, bind_control, checkpoint_values,initial_estimate, ProcessingEstimate, interrupted_state,deadline_after, resume_fields)@@ -887,7 +889,7 @@ def _read_local_posters(jobs, state, *, update, issue):workers = {'low': 1, 'standard': 2, 'high': 4}[state['speed_mode']]while submitted < len(jobs) and len(futures) < workers:row, source, code, target = jobs[submitted]- futures[executor.submit(read_jpeg, source, isolated=row.get('location') != 'local')] = (+ futures[executor.submit(copy_context().run, read_jpeg, source, isolated=row.get('location') != 'local')] = (row, code, target, time.time())submitted += 1completed, _ = wait(futures, return_when=FIRST_COMPLETED)@@ -2230,14 +2232,17 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='coscope_ids = chosen_ids if targeted else Nonecontrol = task_control(config, state_file)identity = job_id or uuid.uuid4().hex+ traffic = ProcessingTraffic(config, db_path, stage, scope_ids,+ resume_state=resume_state)def control_report(**values):resume_fields(state, values)state.update(values)estimate.update(state)+ state['traffic'] = traffic.snapshot(finished=state.get('status') in ('complete', 'failed'))flush_state(force=True)report(dict(state))from .update_tasks import activity- with (activity(Path(db_path), 'library-processing'),+ with (activity(Path(db_path), 'library-processing'), traffic.bind(),FileLock(str(state_path(config)) + '.lock', timeout=0),bind_control(control, identity, publish=control_report, active=active, db_path=Path(db_path),mode=speed_mode)):@@ -2283,11 +2288,19 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='cocheckpoint_values(values)state.update(values)estimate.update(state)+ state['traffic'] = traffic.snapshot(finished=state.get('status') in ('complete', 'failed'))state['last_progress_at'] = time.time()state['progress_seq'] += 1flush_state(force='status' in values)report(dict(state))+ def traffic_report():+ state['traffic'] = traffic.snapshot()+ flush_state()+ report(dict(state))++ traffic.publish = traffic_report+def issue(asset, message, *, action='', retryable=False, paused=False, diagnostics=None):"""`asset` 是这一项的馆藏行,来源离线一类与具体项目无关的问题给 `None`。@@ -2325,6 +2338,8 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='coscan_checkpoints=state['scan_checkpoints'],save_checkpoint=lambda: flush_state(force=True), initial_probed=state['probed'])state.update(scanned=sources.scanned, scanned_bytes=sources.scanned_bytes, probed=sources.probed)+ traffic.unknown_files = False+ traffic.refresh_plan()finish_step('sources')if stage == SCAN_STAGE:# `checked`/`total` 在探时长时借给进度条用过;只扫描这一段没有采集,读数归零。@@ -2334,6 +2349,7 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='coreturn staterows, genre_decisions = _collection_rows(db_path, locations=locations, online_roots=sources.online_roots, asset_ids=scope_ids)+ traffic.refresh_plan(rows)pending = _processing_workload(config, rows, stage, database, checkpoints.completed['completed_previews'])update(estimate_pending=dict(pending))if stage == PREVIEWS_STAGE:@@ -2467,6 +2483,7 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='coexcept Exception:state.update(status='failed', error='处理被中断。核对媒体目录后重试。', completed_at=time.time())state.update(interrupted_state(control))+ state['traffic'] = traffic.snapshot(finished=state.get('status') == 'failed')_save(path, state)raisefinally:diff --git a/src/peach/local_poster.py b/src/peach/local_poster.pyindex 80b231dd8..484c2794c 100644--- a/src/peach/local_poster.py+++ b/src/peach/local_poster.py@@ -19,14 +19,20 @@ def read_jpeg(source: Path, *, isolated=True) -> tuple[bytes, tuple[int, int]]:if not isolated:return _decode(source)from .images import measure_image_size+ from .processing_traffic import CURRENT, record_media_readcommand = ([sys.executable, 'local-image'] if getattr(sys, 'frozen', False)else [sys.executable, '-m', 'peach.local_poster'])with tempfile.TemporaryDirectory(prefix='peach-local-poster-') as temporary:output = Path(temporary).resolve() / 'poster.jpg'result = subprocess.run([*command, str(source), str(output)], stdin=subprocess.DEVNULL,- stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=TIMEOUT_SECONDS,+ stdout=subprocess.PIPE if CURRENT.get() is not None else subprocess.DEVNULL,+ stderr=subprocess.DEVNULL, timeout=TIMEOUT_SECONDS,creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0)+ if CURRENT.get() is not None:+ for line in (getattr(result, 'stdout', None) or b'').splitlines():+ if line.isdigit():+ record_media_read(source, int(line))if result.returncode:raise ValueError('配套图片无法读取')data = output.read_bytes()@@ -36,11 +42,17 @@ def read_jpeg(source: Path, *, isolated=True) -> tuple[bytes, tuple[int, int]]:return data, size-def _decode(source):+def _decode(source, *, report=None):try:if source.stat().st_size > MAX_BYTES:raise ValueError('配套图片体积超出范围')- with Image.open(source) as image:+ with source.open('rb') as handle:+ raw = handle.read(MAX_BYTES + 1)+ from .processing_traffic import record_media_read+ (report or (lambda size: record_media_read(source, size)))(len(raw))+ if len(raw) > MAX_BYTES:+ raise ValueError('配套图片体积超出范围')+ with Image.open(io.BytesIO(raw)) as image:image.load()output = io.BytesIO()image.convert('RGB').save(output, format='JPEG', quality=95)@@ -55,7 +67,7 @@ def main(argv=None):parser.add_argument('output', type=Path)args = parser.parse_args(argv)try:- data, _ = _decode(args.source)+ data, _ = _decode(args.source, report=lambda size: print(size, flush=True))args.output.write_bytes(data)return 0except (OSError, ValueError, Image.DecompressionBombError):diff --git a/src/peach/media_probe.py b/src/peach/media_probe.pyindex 18f3719cb..1bbdfe1db 100644--- a/src/peach/media_probe.py+++ b/src/peach/media_probe.py@@ -19,6 +19,7 @@ import subprocessfrom collections.abc import Callable, Sequencefrom concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETEDfrom contextlib import closing+from contextvars import copy_contextfrom pathlib import PureWindowsPathfrom .jobs import ACTIVE_ASSET_SQL@@ -55,11 +56,12 @@ def context_fields(width: int, height: int, duration: float) -> tuple[str | Nonedef probe_file(ffprobe: str, path: str, timeout: float = TIMEOUT_SECONDS) -> tuple:+ from .processing_traffic import CURRENT as TRAFFIC, media_statisticsif skip_background_read(path):raise ValueError('STRM 和 ISO 不执行视频探测')result = subprocess.run([- ffprobe, "-v", "error", "-rw_timeout", "8000000",+ ffprobe, "-v", "verbose" if TRAFFIC.get() is not None else "error", "-rw_timeout", "8000000","-select_streams", "v:0", "-show_entries","format=duration:stream=width,height,codec_name,avg_frame_rate","-of", "json", path,@@ -68,6 +70,7 @@ def probe_file(ffprobe: str, path: str, timeout: float = TIMEOUT_SECONDS) -> tuptimeout=timeout,creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0,)+ media_statistics(path, result.stderr)if result.returncode:raise subprocess.CalledProcessError(result.returncode, 'ffprobe', stderr=result.stderr)payload = json.loads(result.stdout or b"{}")@@ -97,6 +100,9 @@ def measure(ffprobe: str, asset_id: int, path: str, timeout: float = TIMEOUT_SECduration, width, height, codec, fps, audio = probe_file(ffprobe, resolve_case_insensitive(path), timeout)except Exception as error: # 超时、文件读不到、输出不是 JSON:对这一行都是「没拿到」+ if isinstance(error, subprocess.TimeoutExpired):+ from .processing_traffic import media_statistics+ media_statistics(path, error.stderr)from .error_reporting import subprocess_failuresubprocess_failure('media-probe', tool=ffprobe, error=error, asset_id=asset_id,returncode=getattr(error, 'returncode', None), timeout_seconds=timeout)@@ -172,7 +178,7 @@ def _measure_batches(ffprobe, rows):workers = {'low': 1, 'standard': 2, 'high': WORKERS}[control.mode] if control else WORKERSwhile start < len(rows) and len(futures) < workers:row = rows[start]- futures.add(executor.submit(measure, ffprobe, row[0], row[1]))+ futures.add(executor.submit(copy_context().run, measure, ffprobe, row[0], row[1]))start += 1completed, futures = wait(futures, return_when=FIRST_COMPLETED)for future in completed:diff --git a/src/peach/metadata_amane.py b/src/peach/metadata_amane.pyindex 8b6c2a88d..a3e2d4ce5 100644--- a/src/peach/metadata_amane.py+++ b/src/peach/metadata_amane.py@@ -278,6 +278,8 @@ class AmaneBridge:direct_sites: Sequence[str] = (),timeout: float | None = None) -> dict:"""问几站,返回桥的整份报告(`sites` 键下每站一条记录)。"""+ from .processing_traffic import unobserved+ unobserved('外部采集器流量未观测')number = validate_provider_code(code)names = [str(site).strip() for site in sites]unknown = [site for site in names if site not in SITES]diff --git a/src/peach/processing_traffic.py b/src/peach/processing_traffic.pynew file mode 100644index 000000000..e5d2baee2--- /dev/null+++ b/src/peach/processing_traffic.py@@ -0,0 +1,209 @@+"""馆藏任务的逐目录流量读数、代理流量保护分账与粗估。"""+from __future__ import annotations++import math+import re+import sqlite3+import threading+import time+from contextlib import closing, contextmanager+from contextvars import ContextVar+from pathlib import PureWindowsPath++from .media_sources import CLOUD_LOCATIONS, cost_for++CURRENT = ContextVar('peach_processing_traffic', default=None)+ROUTE = ContextVar('peach_processing_traffic_route', default='metadata')+MIB = 1024 ** 2+STATISTICS = re.compile(r'Statistics:\s*(\d+) bytes read')+++def _key(path):+ return str(PureWindowsPath(path)).rstrip('\\').casefold()+++def record_download(size):+ meter = CURRENT.get()+ if meter is not None:+ meter.add(ROUTE.get(), size)+++def unobserved(reason):+ meter = CURRENT.get()+ if meter is not None:+ meter.mark(ROUTE.get(), reason)+++def record_media_read(path, size):+ meter = CURRENT.get()+ if meter is not None:+ meter.media_read(str(path), size)+++def media_statistics(path, stderr):+ """FFmpeg 的输入读取量;挂载缓存和预读由挂载工具管理。"""+ meter = CURRENT.get()+ if meter is None:+ return+ text = stderr.decode('utf-8', 'replace') if isinstance(stderr, bytes) else str(stderr or '')+ sizes = STATISTICS.findall(text)+ if sizes:+ record_media_read(path, sum(map(int, sizes)))+ else:+ meter.media_read(str(path), 0, missing=True)+++@contextmanager+def download_route(metered):+ token = ROUTE.set('metadata-proxy' if metered else 'metadata-direct')+ try:+ yield+ finally:+ ROUTE.reset(token)+++class ProcessingTraffic:+ def __init__(self, config, db_path, stage, asset_ids=None, *, resume_state=None):+ self.config, self.db_path, self.stage, self.asset_ids = config, db_path, stage, asset_ids+ self.lock = threading.RLock()+ self.publish = lambda: None+ self.published_at = 0+ self.owner = threading.get_ident()+ self.rows = {}+ for location, roots in config.locations.items():+ for root in roots:+ self.rows[f'{location}:{_key(root)}'] = dict(+ label=config.library_names.get(root) or root, root=root, location=location,+ metered=cost_for(location, root, config) == 'metered',+ network=location in CLOUD_LOCATIONS or cost_for(location, root, config) == 'metered',+ used_bytes=0, remaining_min=0, remaining_max=0, unobserved=[])+ for key, label, metered in [('metadata', '影片资料', True),+ ('metadata-proxy', '影片资料', True),+ ('metadata-direct', '影片资料', False)]:+ self.rows[key] = dict(label=label, root='', location='metadata', metered=metered,+ network=True, used_bytes=0, remaining_min=0, remaining_max=0, unobserved=[])+ previous = (resume_state or {}).get('traffic') or {}+ for old in previous.get('sources', []):+ if old.get('key') in self.rows:+ self.rows[old['key']].update(used_bytes=old.get('used_bytes', 0),+ unobserved=list(old.get('unobserved', [])))+ elif old.get('key') and old.get('used_bytes'):+ self.rows[old['key']] = {key: old[key] for key in+ ('label', 'root', 'location', 'metered', 'network', 'used_bytes', 'unobserved')}+ self.rows[old['key']].update(remaining_min=0, remaining_max=0)+ self.unknown_files = stage in ('all', 'scan') and asset_ids is None+ self.refresh_plan()++ @contextmanager+ def bind(self):+ token = CURRENT.set(self)+ try:+ yield self+ finally:+ CURRENT.reset(token)++ def _media_key(self, path, location=None):+ candidates = []+ for key, row in self.rows.items():+ if not row['root'] or location and row['location'] != location:+ continue+ roots = self.config.locations.get(row['location'], ())+ if row['root'] not in roots:+ continue+ index = roots.index(row['root'])+ mounts = self.config.mounts.get(row['location'], ())+ for prefix in [row['root'], *([mounts[index]] if index < len(mounts) else [])]:+ declared, actual = _key(prefix), _key(path)+ if actual == declared or actual.startswith(declared + '\\'):+ candidates.append((len(declared), key))+ return max(candidates)[1] if candidates else None++ def refresh_plan(self, rows=None):+ """账本统计不访问媒体目录;保护开启的目录不预估自动探测。"""+ query = "SELECT location,path,size,duration,snapshot_path FROM asset WHERE medium='video' AND disposal IS NULL"+ params = []+ if self.asset_ids is not None:+ query += ' AND id IN (' + ','.join('?' for _ in self.asset_ids) + ')'+ params += self.asset_ids+ if rows is None:+ with closing(sqlite3.connect(self.db_path, timeout=30)) as connection:+ assets = connection.execute(query, params).fetchall()+ else:+ assets = [(row['location'], row['path'], row['size'], row['duration'], row['snapshot_path']) for row in rows]+ with self.lock:+ for row in self.rows.values():+ row.update(remaining_min=0, remaining_max=0)+ for location, path, size, duration, preview in assets:+ key = self._media_key(path, location)+ if key is None or self.stage == 'previews' and location != 'local':+ continue+ row = self.rows[key]+ size = max(0, size or 0)+ from .strm import skip_background_read+ readable = not skip_background_read(path)+ if self.stage != 'previews' and self.stage != 'scan':+ # 资料和配套图片的工作量只用于粗估,不当作实测下载。+ self.rows['metadata']['remaining_min'] += MIB // 4+ self.rows['metadata']['remaining_max'] += 8 * MIB+ if row['network']:+ row['remaining_max'] += 2 * MIB+ if self.stage != 'metadata' and duration is None and not row['metered'] and row['network'] and readable:+ row['remaining_min'] += min(size, MIB)+ row['remaining_max'] += min(size, 32 * MIB)+ if self.stage in ('all', 'collect', 'previews') and location == 'local' and row['network'] and not preview and readable:+ from .media_processing_policy import metadata_only+ if not metadata_only(self.config.directory('state')):+ row['remaining_max'] += min(size, 9 * 32 * MIB)+ if row['network']:+ self._reason(row, '挂载缓存与预读未观测')++ @staticmethod+ def _reason(row, reason):+ if reason not in row['unobserved']:+ row['unobserved'].append(reason)++ def mark(self, key, reason):+ with self.lock:+ self._reason(self.rows[key], reason)++ def add(self, key, size):+ if not isinstance(size, (int, float)) or not math.isfinite(size) or size <= 0:+ return+ with self.lock:+ row = self.rows[key]+ if not row['network']:+ return+ row['used_bytes'] += int(size)+ remaining = self.rows['metadata'] if key.startswith('metadata-') else row+ for field in ('remaining_min', 'remaining_max'):+ remaining[field] = max(0, remaining[field] - int(size))+ if threading.get_ident() != self.owner or time.monotonic() - self.published_at < 0.5:+ return+ self.published_at = time.monotonic()+ self.publish()++ def media_read(self, path, size, *, missing=False):+ key = self._media_key(path)+ if key is not None:+ if self.rows[key]['network']:+ self.mark(key, '挂载缓存与预读未观测')+ if missing:+ self.mark(key, '媒体读取量未取得')+ self.add(key, size)++ def snapshot(self, *, finished=False):+ with self.lock:+ sources = []+ for key, row in self.rows.items():+ if not row['root'] and not row['used_bytes'] and not row['remaining_max'] and not row['unobserved']:+ continue+ source = {**row, 'unobserved': list(row['unobserved'])}+ sources.append(dict(key=key, **source,+ estimated_min_bytes=row['used_bytes'] + (0 if finished else row['remaining_min']),+ estimated_max_bytes=row['used_bytes'] + (0 if finished else row['remaining_max'])))+ result = dict(sources=sources, unknown_files=self.unknown_files and not finished,+ partial=any(row['unobserved'] for row in sources))+ for field in ('used_bytes', 'estimated_min_bytes', 'estimated_max_bytes'):+ result[field] = sum(row[field] for row in sources)+ result['metered_' + field] = sum(row[field] for row in sources if row['metered'])+ return resultdiff --git a/src/peach/scraping_access.py b/src/peach/scraping_access.pyindex d0d04aba1..c07b28b07 100644--- a/src/peach/scraping_access.py+++ b/src/peach/scraping_access.py@@ -412,7 +412,15 @@ class SourceTransport:for _ in range(6):from .processing_control import checkpointcheckpoint('network')- response = self._request(request, timeout, max_bytes)+ from .processing_traffic import CURRENT, download_route+ if CURRENT.get() is None:+ response = self._request(request, timeout, max_bytes)+ else:+ source = source_for(request.url)+ direct = bool(source and values_for(self.root, source).get('network') == 'direct')+ options = {'trust_env': False} if direct else peach_proxy.client_options(self.root)+ with download_route(bool(options.get('proxy') or options.get('trust_env', True))):+ response = self._request(request, timeout, max_bytes)if response.status not in {301, 302, 303, 307, 308} or not response.headers.get("location"):return responsetarget = urljoin(request.url, response.headers["location"])diff --git a/src/peach/web_library_processing.py b/src/peach/web_library_processing.pyindex 9c75533c9..f84c40d3a 100644--- a/src/peach/web_library_processing.py+++ b/src/peach/web_library_processing.py@@ -16,6 +16,14 @@ def _current(contract):def q_library_processing(contract, _args):+ if _args.get('estimate') == '1':+ config = settings_file.active()+ body = {'stage': _args.get('stage') or ALL_STAGES}+ if 'asset_ids' in _args:+ body['asset_ids'] = json.loads(_args['asset_ids'])+ _, asset_ids, stage = _processing_request(contract, config, {}, body)+ from .processing_traffic import ProcessingTraffic+ return {'traffic': ProcessingTraffic(config, contract.db_path, stage, asset_ids).snapshot()}state = decorate(_current(contract))control = control_for(settings_file.active())control.refresh(force=True)diff --git a/tests/test_processing_traffic.py b/tests/test_processing_traffic.pynew file mode 100644index 000000000..e524ab9b0--- /dev/null+++ b/tests/test_processing_traffic.py@@ -0,0 +1,166 @@+"""馆藏任务按媒体目录的流量保护选项分账。"""+import gzip+import sqlite3+import subprocess+import tempfile+import unittest+from contextlib import closing+from dataclasses import replace+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import patch++import httpx++from peach.http import HttpRequest, HttpxTransport+from peach.processing_traffic import ProcessingTraffic, download_route, media_statistics, record_download, record_media_read+from peach.settings_file import PeachConfig+from support.ledger import fresh_ledger+++class ProcessingTrafficTests(unittest.TestCase):+ def setUp(self):+ temporary = tempfile.TemporaryDirectory()+ self.addCleanup(temporary.cleanup)+ self.root = Path(temporary.name).resolve()+ self.config = PeachConfig(self.root, self.root / 'config.toml', present=True,+ locations={'mounted': ('A:\\Cloud', 'B:\\Cloud'), 'local': ('R:\\Media',)},+ media_costs={'A:\\Cloud': 'metered', 'B:\\Cloud': 'free'})+ self.db = fresh_ledger(self.root)++ def test_media_reads_follow_each_root_and_totals_conserve_bytes(self):+ traffic = ProcessingTraffic(self.config, self.db, 'metadata')+ with traffic.bind():+ record_media_read('A:\\Cloud\\a.nfo', 100)+ record_media_read('B:\\Cloud\\b.jpg', 200)+ record_media_read('R:\\Media\\c.jpg', 300)+ record_download(50)+ result = traffic.snapshot()+ drives = {row['root']: row for row in result['sources'] if row.get('root')}+ self.assertEqual(drives['A:\\Cloud']['used_bytes'], 100)+ self.assertTrue(drives['A:\\Cloud']['metered'])+ self.assertEqual(drives['B:\\Cloud']['used_bytes'], 200)+ self.assertFalse(drives['B:\\Cloud']['metered'])+ self.assertEqual(drives['R:\\Media']['used_bytes'], 0)+ self.assertEqual(result['used_bytes'], 350)+ self.assertEqual(result['used_bytes'], sum(row['used_bytes'] for row in result['sources']))+ self.assertEqual(result['metered_used_bytes'], sum(row['used_bytes'] for row in result['sources'] if row['metered']))++ def test_http_counts_compressed_download_and_failed_partial_stream(self):+ packed = gzip.compress(b'a' * 10000)+ class Broken(httpx.SyncByteStream):+ def __iter__(self):+ yield b'abcd'+ raise httpx.ReadError('断开')+ replies = iter([httpx.Response(200, headers={'content-encoding': 'gzip'}, stream=httpx.ByteStream(packed)),+ httpx.Response(200, stream=Broken())])+ client = httpx.Client(transport=httpx.MockTransport(lambda request: next(replies)))+ self.addCleanup(client.close)+ transport = HttpxTransport(client)+ traffic = ProcessingTraffic(self.config, self.db, 'metadata')+ with traffic.bind():+ self.assertEqual(len(transport(HttpRequest('GET', 'https://example.com/a', {}), 1, 20000).body), 10000)+ with self.assertRaises(httpx.ReadError):+ transport(HttpRequest('GET', 'https://example.com/b', {}), 1, 20000)+ self.assertEqual(traffic.snapshot()['used_bytes'], len(packed) + 4)++ def test_resume_keeps_counts_and_fresh_task_starts_at_zero(self):+ traffic = ProcessingTraffic(self.config, self.db, 'collect')+ with traffic.bind():+ record_media_read('A:\\Cloud\\a.nfo', 100)+ restored = ProcessingTraffic(self.config, self.db, 'collect', resume_state={'traffic': traffic.snapshot()})+ with restored.bind():+ record_media_read('A:\\Cloud\\a.nfo', 25)+ self.assertEqual(restored.snapshot()['used_bytes'], 125)+ self.assertEqual(ProcessingTraffic(self.config, self.db, 'collect').snapshot()['used_bytes'], 0)++ def test_same_provider_roots_have_independent_proxy_estimates_and_selection(self):+ with closing(sqlite3.connect(self.db)) as connection, connection:+ for path in ('A:\\Cloud\\a.mp4', 'B:\\Cloud\\b.mp4'):+ connection.execute('INSERT INTO asset(location,path,name,medium,size) VALUES(?,?,?,?,?)',+ ('mounted', path, 'video.mp4', 'video', 1024**3))+ scan = ProcessingTraffic(self.config, self.db, 'scan').snapshot()+ drives = {row['root']: row for row in scan['sources']}+ self.assertEqual(drives['A:\\Cloud']['estimated_max_bytes'], 0)+ self.assertGreater(drives['B:\\Cloud']['estimated_max_bytes'], 0)+ self.assertEqual(scan['metered_estimated_max_bytes'], 0)+ self.assertEqual(scan['estimated_max_bytes'], sum(row['estimated_max_bytes'] for row in scan['sources']))+ selected = ProcessingTraffic(self.config, self.db, 'scan', [1]).snapshot()+ self.assertFalse(selected['unknown_files'])+ self.assertEqual(selected['estimated_max_bytes'], 0)++ def test_http_route_and_media_statistics_accumulate_without_cross_task_leak(self):+ traffic = ProcessingTraffic(self.config, self.db, 'collect')+ with traffic.bind():+ with download_route(False):+ record_download(50)+ with download_route(True):+ record_download(30)+ media_statistics('A:\\Cloud\\a.mp4', b'Statistics: 100 bytes read, 1 seeks\n')+ media_statistics('B:\\Cloud\\b.mp4', b'ffmpeg timeout')+ record_download(500)+ result = traffic.snapshot()+ self.assertEqual(result['used_bytes'], 180)+ self.assertEqual(result['metered_used_bytes'], 130)+ self.assertTrue(result['partial'])++ def test_read_only_estimate_uses_requested_stage_without_starting_a_job(self):+ from peach.web_library_processing import q_library_processing+ database_root = self.config.directory('database')+ database_root.mkdir(parents=True)+ db = fresh_ledger(database_root)+ contract = SimpleNamespace(db_path=db)+ before = set(self.root.rglob('*'))+ with patch('peach.web_library_processing.settings_file.active', return_value=self.config):+ response = q_library_processing(contract, {'estimate': '1', 'stage': 'scan'})+ with self.assertRaises(ValueError):+ q_library_processing(contract, {'estimate': '1', 'stage': 'invalid'})+ self.assertTrue(response['traffic']['unknown_files'])+ self.assertEqual(response['traffic']['used_bytes'], 0)+ self.assertEqual(set(self.root.rglob('*')), before)++ def test_mount_alias_and_longest_root_own_the_read(self):+ config = replace(self.config, locations={'mounted': ('A:\\Cloud', 'A:\\Cloud\\Direct')},+ mounts={'mounted': ('/mnt/cloud', '/mnt/direct')},+ media_costs={'A:\\Cloud': 'metered', 'A:\\Cloud\\Direct': 'free'})+ traffic = ProcessingTraffic(config, self.db, 'metadata')+ with traffic.bind():+ record_media_read('a:\\cloud\\DIRECT\\a.nfo', 100)+ record_media_read('/mnt/cloud/b.nfo', 50)+ record_media_read('/mnt/direct/c.nfo', 25)+ result = traffic.snapshot()+ self.assertEqual(result['used_bytes'], 175)+ self.assertEqual(result['metered_used_bytes'], 50)++ def test_plan_uses_scanned_workload_without_an_asset_id_parameter_list(self):+ traffic = ProcessingTraffic(self.config, self.db, 'scan')+ rows = [dict(location='mounted', path='B:\\Cloud\\a.mp4', size=1024**3,+ duration=None, snapshot_path=None)]+ with patch('peach.processing_traffic.sqlite3.connect', side_effect=AssertionError('重复查询')):+ traffic.refresh_plan(rows)+ self.assertGreater(traffic.snapshot()['estimated_max_bytes'], 0)+ self.assertEqual(traffic.snapshot()['metered_estimated_max_bytes'], 0)++ def test_real_ffmpeg_probe_and_frame_report_input_reads(self):+ from peach.ffmpeg import FFmpegResolver+ from peach.frame_capture import capture_frame+ from peach.media_probe import probe_file+ from support.conditions import missing_prerequisite+ resolver = FFmpegResolver(self.root)+ ffmpeg, ffprobe = resolver.ffmpeg(), resolver.ffprobe()+ if ffmpeg is None or ffprobe is None:+ missing_prerequisite('媒体流量实证需要 FFmpeg 与 ffprobe')+ source = self.root / '示例.mp4'+ subprocess.run([str(ffmpeg.path), '-y', '-v', 'error', '-f', 'lavfi', '-i',+ 'color=c=black:s=320x180:r=5:d=1', '-c:v', 'mpeg4', str(source)],+ capture_output=True, check=True, timeout=20)+ config = replace(self.config, locations={'mounted': ('A:\\Cloud',)},+ mounts={'mounted': (str(self.root),)})+ traffic = ProcessingTraffic(config, self.db, 'collect')+ with traffic.bind():+ self.assertGreater(probe_file(str(ffprobe.path), str(source))[0], 0)+ probed = traffic.snapshot()['used_bytes']+ self.assertGreater(probed, 0)+ success, _ = capture_frame(str(ffmpeg.path), str(source), 0.1, self.root / 'frame.jpg')+ self.assertTrue(success)+ self.assertGreater(traffic.snapshot()['used_bytes'], probed)