Peach: Local-first video metadata scraper and media manager.
feat(web): 完善整理模板构建和主要站点标识
longmeidao2eb39b505ecc
diff --git a/docs/REUSE.md b/docs/REUSE.mdindex 5aadaa256..f4c820c66 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -511,6 +511,7 @@ A 盘垃圾隔离使用短名称 `peach-purge-<32 位 UUID>.peach-quarantine`,- 名字里的括号都走 `split_composite_aliases.py`:自动那拨只认罗马字复合人名,`--from-review` 那拨按人工判定清掉不承载名字的尾巴,旧写法留作别名;读音、厂牌消歧和角色出处不拆。`<私有验证记录>` 中的 28 条 creator 注音、575 条 tag 角色出处和 10 条 series 厂牌或载体消歧均保留,不属于待执行批次。- 全库创作者归属复核走 `scripts/audit_creator_attributions.py` 与 `metadata_creator_attributions`:复用 `code_creators` 的文件番号核对、DMM `matching_cids`、`western_release_identity`、厂牌规范名和别名、`studio_sites.is_platform`、字段归属、SQLite 备份及 `review_csv`。发行站作品号同时核对目录名与媒体文件名;来源目录见 [Legs Japan](https://www.legsjapan.com/en/)、[Fellatio Japan](https://www.fellatiojapan.com/en/),平台身份见 [myfans 官方说明](https://creators.myfans.jp/)。预览覆盖全部来源关系与无作品实体;执行带冻结计划、备份和回滚记录,在事务内重算。只撤下有文件或发行来源证据的 `legacy:asset` 关系,保留用户判断、外部身份、规范实体和个人记录。月份、画质、份数与合集注记只回归已存在的完整账号;结构目录按整名切分判定,词汇取 `catalog_rules` 标签、`genre_taxonomy` 来源词与 `resources/naming/structural_directory_words.txt`,不登记具体馆藏的目录名;`creator_collection_base` 同时用于审计与摄取。公开厂牌断言可补空厂牌投影及关系,缺少规范厂牌时记录新实体完整快照;`upsert_asset_entity(update_entity_metadata=False)` 保留既有资料。结构目录、已知转载站及可信厂牌不进入自动 creator 摄取。外键检查逐项比较既有悬空;回滚核对字段版本、关系和新实体引用,后续改动拒绝整批恢复。- 实体链接可安装:`entity_link` 表、`q_entity` 的 `links` 契约、资料页 favicon 与管理页链接管理成套;死链区分「搬走了」和「没了」,`rediscover_entity_links.py` 从站点索引页上溯找新锚,确证没了的由 `link_status.settle_gone` 处置(已隐退女优留成不可点的失效标记,其余删除)。+- 链接管理的主要站点复用 `SiteMark` 与 `/link-mark?id=`;`/api/links` 的 `top_host_link_ids` 为每个主要站点提供一条现有实体链接的 ID,取图范围由账本记录确定,计数使用链接外的中性 `Chip`。整理规则复用 BoardUI `Input`、描边模板按钮与轻量参数按钮;参数插入当前输入框的光标或选区,模板同时填入文件名与目录。`/api/organize/example` 以固定示例字段调用正式模板解析器,随自定义输入显示路径与语法问题,不读取账本或生成计划 CSV。编辑后须重新预览实际整理计划。番号参数使用 `{code}`,解析器接受 `{number}` 别名以读取已保存的规则。无新增依赖或真实 ledger 写入。- 厂牌社媒核查:`find_studio_socials.py` 用 Beautiful Soup 4.15.0 解析锚点,传输与字符集用 `peach.http`,账号键用 `social_links.handle`,不加浏览器运行时;Peach 负责同站年龄门、已有账号差集与证据表。整页正则会混进帖子与脚本里的地址,不用于账号提取。- 事务所是实体:57 家各有 `/agencies/<名字>` 页,成员、官网、标签与作品都按 `entity_membership` 算,女优页点得进去,搜名字出这家人的片;原文留在 `metadata.agency`。- 外链圆标与厂牌标识取站点自己声明的资产,宽扁字标不参加小圆标竞选;`/logo` 的 `variant` 分 `icon`、`logo` 与最清晰的 `large`,大图版式和资料页取 `large`,紧凑版式取 `icon`。头像与标识共用 `nativeImageFit`:按像素密度折算的源尺寸不足框四成时等比居中、不放大、同图模糊补底,框短边小于 64 px 不补底;加载、回落和版式切换都重新度量。diff --git a/docs/adr/0039-media-stays-put-until-organize-is-asked-for.md b/docs/adr/0039-media-stays-put-until-organize-is-asked-for.mdindex 30f7d4b97..a46d36bee 100644--- a/docs/adr/0039-media-stays-put-until-organize-is-asked-for.md+++ b/docs/adr/0039-media-stays-put-until-organize-is-asked-for.md@@ -26,7 +26,7 @@ README 的「数据边界」一直承诺媒体保留在原目录:Peach 只读**二、模板挂在来源根上。** 用户对每个来源(`local` / `115` / `pikpak`)配置一份文件名模板和一份目录模板,存在 `profile.settings_json` 的 `organizeTemplates` 里,跟着账本走。占位符-`{number}` `{title}` `{studio}` `{year}` `{actors}` `{series}` `{cd}` `{mosaic}` `{definition}`+`{code}` `{title}` `{studio}` `{year}` `{actors}` `{series}` `{cd}` `{mosaic}` `{definition}`只从 approved 真相字段取值,候选一律不参与,因为按未复核的猜测改文件名,等于把猜测写进了文件系统,而那一层没有 `source` 和 `confidence` 可言。`[...]` 分组里的占位符为空时整段省略。diff --git a/frontend/e2e/organize-builder.test.ts b/frontend/e2e/organize-builder.test.tsnew file mode 100644index 000000000..34854719a--- /dev/null+++ b/frontend/e2e/organize-builder.test.ts@@ -0,0 +1,87 @@+import assert from 'node:assert/strict';+import { after, before, describe, it } from 'node:test';+import type { Browser } from 'playwright-core';+import { launch, settle, VIEWPORTS, visit } from './harness.ts';++describe('整理规则构建', () => {+ let browser: Browser;+ before(async () => { browser = await launch() });+ after(async () => { await browser.close() });+ for (const viewport of VIEWPORTS) it(`${viewport.name}:参数插入、模板区分与站点图标`, async () => {+ const opened = await visit(browser, '/data-cleanup', viewport, async (page) => {+ await page.route('**/api/organize', route => route.fulfill({ json: {+ status: 'idle', locations: [{ location: 'local', roots: [] }], last_batch: '',+ templates: { local: { file: '{code}', dir: '' } },+ presets: [{ label: '按厂牌建文件夹', file: '{code}[ {title}]', dir: '{studio}', example: '厂牌/ABC-123 标题.mp4' }],+ placeholders: Object.entries({ code: '番号', title: '标题', studio: '厂牌', series: '系列', year: '发行年', actors: '出演者', cd: '分片号', mosaic: '有码无码', definition: '画质' })+ .map(([key, label]) => ({ key, label })),+ } }));+ await page.route('**/api/links', route => route.fulfill({ json: {+ total: 3, entities: 1, by_kind: {}, top_hosts: [['example.com', 3]], top_host_link_ids: { 'example.com': 7 },+ } }));+ await page.route('**/link-mark?*', route => route.fulfill({ contentType: 'image/png',+ body: Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9ZQmcAAAAASUVORK5CYII=', 'base64') }));+ });+ try {+ const { page } = opened;+ const organize = page.locator('#organize');+ const file = organize.getByRole('textbox', { name: '文件名模板', exact: true });+ const dir = organize.getByRole('textbox', { name: '目录模板', exact: true });+ await file.waitFor();+ await settle(page);+ assert.equal(await organize.getByRole('group', { name: '插入参数', exact: true }).getByRole('button').count(), 9);+ await file.fill('前缀-后缀');+ await file.evaluate((input: HTMLInputElement) => { input.focus(); input.setSelectionRange(3, 3) });+ await organize.getByRole('button', { name: '番号 {code}', exact: true }).click();+ assert.equal(await file.inputValue(), '前缀-{code}后缀');+ assert.equal(await file.evaluate(input => document.activeElement === input), true);+ await dir.fill('厂牌/作品');+ await dir.evaluate((input: HTMLInputElement) => { input.focus(); input.setSelectionRange(3, 5) });+ await organize.getByRole('button', { name: '标题 {title}', exact: true }).click();+ assert.equal(await dir.inputValue(), '厂牌/{title}');+ await dir.evaluate((input: HTMLInputElement) => { input.focus(); input.setSelectionRange(3, 10) });+ await organize.getByRole('button', { name: '标题 {title}', exact: true }).click();+ assert.equal(await dir.inputValue(), '厂牌/{title}');+ assert.equal(await dir.evaluate(input => document.activeElement === input), true);+ assert.equal(await dir.evaluate((input: HTMLInputElement) => input.selectionStart), 10);+ await file.fill('自定义_{code}[_{definition}]');+ await dir.fill('{year}/{studio}');+ await page.waitForFunction(() => document.querySelector('[data-organize-example]')?.textContent?.includes('2024/厂牌/自定义_ABC-123_1080p.mp4'));+ const faces = await page.evaluate(() => {+ const face = (label: string) => {+ const button = document.querySelector(`#organize [aria-label="${label}"] button`)!;+ const style = getComputedStyle(button);+ return [style.backgroundColor, style.borderTopWidth, style.boxShadow];+ };+ return { preset: face('套用模板'), parameter: face('插入参数'), overflow: document.documentElement.scrollWidth > innerWidth };+ });+ assert.notDeepEqual(faces.preset, faces.parameter, '模板与参数使用不同按钮样式');+ assert.equal(faces.overflow, false);+ await organize.getByRole('button', { name: '按厂牌建文件夹', exact: true }).click();+ assert.equal(await file.inputValue(), '{code}[ {title}]');+ assert.equal(await dir.inputValue(), '{studio}');+ await page.waitForFunction(() => document.querySelector('[data-organize-example]')?.textContent?.includes('厂牌/ABC-123 标题.mp4'));+ assert.ok((await organize.textContent())?.includes('路径示例:厂牌/ABC-123 标题.mp4'));+ const icon = page.locator('#link-manager a[href="https://example.com"] img');+ await icon.scrollIntoViewIfNeeded();+ await page.waitForFunction(() => {+ const image = document.querySelector<HTMLImageElement>('#link-manager a img');+ return image?.complete && image.naturalWidth > 0;+ });+ assert.equal(await icon.getAttribute('src'), '/link-mark?id=7');+ const link = page.locator('#link-manager a[href="https://example.com"]');+ assert.equal((await link.textContent())?.trim(), 'example.com');+ assert.equal(await link.locator('svg').count(), 1);+ await link.hover();+ const badge = page.locator('[data-top-host] > span');+ assert.equal(await badge.textContent(), '3');+ const badgeFace = await badge.evaluate(node => {+ const style = getComputedStyle(node);+ return { background: style.backgroundColor, decoration: style.textDecorationLine };+ });+ assert.notEqual(badgeFace.background, 'rgba(0, 0, 0, 0)');+ assert.equal(badgeFace.decoration, 'none');+ assert.deepEqual(opened.problems, []);+ } finally { await opened.close() }+ });+});diff --git a/frontend/src/react/components/site-mark.tsx b/frontend/src/react/components/site-mark.tsxindex 07095c2f5..88a8651dd 100644--- a/frontend/src/react/components/site-mark.tsx+++ b/frontend/src/react/components/site-mark.tsx@@ -3,11 +3,12 @@ import { RiBuildingLine, RiGlobalLine } from '@remixicon/react';import { siteMarkUrl } from '@peach/legacy/core';/** 采集来源的站标由本站缓存或随应用提供的静态资源承载。 */-export function SiteMark({ source }: { source: string }) {- const [failed, setFailed] = useState(false);+export function SiteMark({ source, linkId }: { source: string; linkId?: never } | { linkId: number; source?: never }) {+ const [failed, setFailed] = useState('');+ const mark = String(linkId ?? source);if (source === 'makers') return <RiBuildingLine aria-hidden className="size-4 shrink-0" />;- if (failed) return <RiGlobalLine aria-hidden className="size-4 shrink-0" />;- const bundled = ['javten', 'fc2ppvdb', 'avwikidb', 'minnano-av', 'github'].includes(source);- return <img src={bundled ? `/site-icon/${source}.png` : siteMarkUrl({ source })} alt="" width={16} height={16}- loading="lazy" onError={() => setFailed(true)} className="size-4 shrink-0 rounded-none object-contain" />;+ if (failed === mark) return <RiGlobalLine aria-hidden className="size-4 shrink-0" />;+ const bundled = source && ['javten', 'fc2ppvdb', 'avwikidb', 'minnano-av', 'github'].includes(source);+ return <img src={linkId !== undefined ? `/link-mark?id=${linkId}` : bundled ? `/site-icon/${source}.png` : siteMarkUrl({ source: source! })} alt="" width={16} height={16}+ loading="lazy" onError={() => setFailed(mark)} className="size-4 shrink-0 rounded-none object-contain" />;}diff --git a/frontend/src/react/data-cleanup/link-manager.tsx b/frontend/src/react/data-cleanup/link-manager.tsxindex 2bc4a7000..819591086 100644--- a/frontend/src/react/data-cleanup/link-manager.tsx+++ b/frontend/src/react/data-cleanup/link-manager.tsx@@ -13,6 +13,7 @@ import { useQuery } from '@tanstack/react-query';import { confirmModal } from '@peach/legacy/ui';import { Button } from '@/components/base/buttons/button';+import { Chip } from '@/components/base/badges/chip';import { errorMessage } from '../../api';import { ErrorExcerpt } from '../activity/error-excerpt';@@ -22,6 +23,7 @@ import {Fieldset, FieldsetTitle, PanelFooter, RESULT_PANEL, RESULT_SECTION,} from '../components/fieldset';import { Note } from '../components/note';+import { SiteMark } from '../components/site-mark';import { TaskProgress } from '../components/task-progress';import { spriteGlyph } from '../components/sprite-glyph';import { queryClient } from '../query';@@ -125,7 +127,7 @@ export function LinkManager() {const selectedGone = gone.filter((item) => gonePicked.includes(item.id)).map((item) => item.id);const selectedRetryable = retryable.filter((item) => picked.includes(item.id)).map((item) => item.id);const info = stats.data;- const hosts = (info?.top_hosts ?? []).slice(0, 3).map(([host, n]) => `${host} ${count(n)}`).join(' · ');+ const hosts = (info?.top_hosts ?? []).slice(0, 3);const run = (retry?: readonly number[]) => {if (running || (retry && !retry.length)) return;@@ -222,10 +224,19 @@ export function LinkManager() {<Stat key={kind} label={LINK_KINDS[kind] || kind} value={count(value)} />))}</div>- {hosts+ {hosts.length? <div className="mt-4 flex min-w-0 flex-col gap-0.5"><span className="text-caption-1-regular text-text-secondary">主要站点</span>- <b className="text-body-2-medium break-words text-text-primary">{hosts}</b>+ <div className="flex min-w-0 flex-wrap gap-x-5 gap-y-2">+ {hosts.map(([host, n]) => <span key={host} data-top-host="" className="inline-flex min-w-0 max-w-full items-center gap-1.5">+ <a href={`https://${host}`} target="_blank" rel="noopener noreferrer"+ className="inline-flex min-w-0 items-center gap-1.5 text-body-2-medium text-text-primary">+ <SiteMark linkId={info.top_host_link_ids[host]!} /><span className="min-w-0 wrap-anywhere">{host}</span>+ <RiExternalLinkLine aria-hidden className="size-3.5 shrink-0" />+ </a>+ <Chip variant="caption" color="neutral"><span className="tabular-nums">{count(n)}</span></Chip>+ </span>)}+ </div></div>: null}</>diff --git a/frontend/src/react/data-cleanup/links.ts b/frontend/src/react/data-cleanup/links.tsindex d9878a01d..01eadd852 100644--- a/frontend/src/react/data-cleanup/links.ts+++ b/frontend/src/react/data-cleanup/links.ts@@ -32,6 +32,7 @@ export interface LinkStats {entities: number;by_kind: Record<string, number>;top_hosts: [string, number][];+ top_host_link_ids: Record<string, number>;}/** 一条被判出来的链接。`note` 是站点回的那句结果。 */diff --git a/frontend/src/react/data-cleanup/organize-card.tsx b/frontend/src/react/data-cleanup/organize-card.tsxindex 53e0ec172..6034ae4bf 100644--- a/frontend/src/react/data-cleanup/organize-card.tsx+++ b/frontend/src/react/data-cleanup/organize-card.tsx@@ -3,7 +3,7 @@* 预览是一次同步请求,结果只活在这张卡上;执行与回滚是同一趟后台任务,快照摊在* `/api/organize` 的顶层,所以 `useBackgroundJob` 用 `withJob` 只换任务那几格,来源、模板* 与上一批原样留着。执行与回滚动的是真实文件名与数据库路径,都要先过确认弹层。 */-import { useRef, useState } from 'react';+import { useLayoutEffect, useRef, useState } from 'react';import { useMutation, useQuery } from '@tanstack/react-query';import { confirmModal, MEDIA_SOURCE_ICONS } from '@peach/legacy/ui';import { LOC } from '@peach/legacy/core';@@ -23,7 +23,7 @@ import { SourceMark } from '../settings/section';import { FolderSearchIcon } from '../settings/folder-rows';import { busyProps } from '../settings/use-action';import {- baseName, DIRECTORY_TEMPLATE_ERROR, fetchOrganize, ORGANIZE_KEY, organizeOutcome, planSummary,+ baseName, DIRECTORY_TEMPLATE_ERROR, fetchOrganize, fetchOrganizeExample, ORGANIZE_KEY, organizeOutcome, planSummary,pickOrganizeFolder, previewOrganize, rememberTemplates, startOrganize, withOrganizeJob,type OrganizeCommand, type OrganizeData, type OrganizeJob, type OrganizePlan,} from './organize';@@ -76,6 +76,22 @@ export function OrganizeCard(const fileInput = useRef<HTMLInputElement>(null);const dirInput = useRef<HTMLInputElement>(null);const pathInput = useRef<HTMLInputElement>(null);+ const example = useQuery({+ queryKey: ['organize-example', fields.file, fields.dir],+ queryFn: ({ signal }) => fetchOrganizeExample(fields.file, fields.dir, signal),+ enabled: Boolean(data),+ });+ const [target, setTarget] = useState<'file' | 'dir'>('file');+ const focused = useRef(false);+ const insertion = useRef<{ field: 'file' | 'dir'; caret: number } | null>(null);+ useLayoutEffect(() => {+ const next = insertion.current;+ if (!next) return;+ insertion.current = null;+ const input = (next.field === 'file' ? fileInput : dirInput).current;+ input?.focus();+ input?.setSelectionRange(next.caret, next.caret);+ });const request = () => ({ location, path: path.trim(), file_template: fields.file.trim(), dir_template: fields.dir.trim() });const preview = useMutation({@@ -119,7 +135,18 @@ export function OrganizeCard(const plan = previewMatches ? preview.data : undefined;const changes = Number(plan?.counts?.change || 0);const name = LOC[location] || location;- const hint = (data?.placeholders ?? []).map((item) => `{${item.key}} ${item.label}`).join(' · ');+ const changeTemplates = (patch: Partial<{ file: string; dir: string }>) => {+ edit(patch); setInvalid(null); preview.reset();+ };+ const insertParameter = (key: string) => {+ const input = (target === 'file' ? fileInput : dirInput).current;+ const value = fields[target];+ const start = focused.current ? input?.selectionStart ?? value.length : value.length;+ const end = focused.current ? input?.selectionEnd ?? start : start;+ const token = `{${key}}`;+ insertion.current = { field: target, caret: start + token.length };+ changeTemplates({ [target]: value.slice(0, start) + token + value.slice(end) });+ };const runPreview = () => {if (preview.isPending || running || !location) return;@@ -196,7 +223,7 @@ export function OrganizeCard(onSelectionChange={(key) => {if (key === null) return;setChosen(String(key)); setScope({ location: String(key), path: '' });- setInvalid(null); preview.reset(); picker.reset();+ setInvalid(null); preview.reset(); picker.reset(); focused.current = false;}}>{locations.map((row) => (<SelectItem key={row.location} id={row.location} textValue={LOC[row.location] || row.location}>@@ -216,25 +243,48 @@ export function OrganizeCard(</div><p className="text-caption-1-regular text-text-secondary">指定路径时包含子目录,目录模板以该路径为起点。模板均留空时只清理文件名。</p>{picker.isError ? <Note tone="error" title="目录选择失败">{errorMessage(picker.error)};也可直接输入路径。</Note> : null}- <Input label="文件名模板" placeholder="{number}[ {title}]" value={fields.file} ref={fileInput}+ <Input label="文件名模板" placeholder="{code}[ {title}]" value={fields.file} ref={fileInput}spellCheck="false" isInvalid={invalid === 'file'}- onChange={(value) => { edit({ file: value }); setInvalid(null) }} />+ onFocus={() => { setTarget('file'); focused.current = true }}+ onChange={(value) => changeTemplates({ file: value })} /><Input label="目录模板" placeholder="留空表示原地改名" value={fields.dir} ref={dirInput}spellCheck="false" isInvalid={invalid === 'dir'}- onChange={(value) => { edit({ dir: value }); setInvalid(null) }} />+ onFocus={() => { setTarget('dir'); focused.current = true }}+ onChange={(value) => changeTemplates({ dir: value })} />{data?.presets?.length- ? <div className="flex flex-wrap gap-2">+ ? <div className="flex flex-col gap-2" role="group" aria-label="套用模板">+ <p className="text-caption-1-medium text-text-secondary">套用模板</p>+ <div className="flex flex-wrap gap-2">{data.presets.map((preset) => (- <Button key={preset.label} variant="secondary" title={preset.label}+ <Button key={preset.label} variant="secondary" size="small" title={preset.example || preset.label}className="max-w-full [&>span]:min-w-0 [&>span]:shrink"- onClick={() => { edit({ file: preset.file, dir: preset.dir }); setInvalid(null) }}>+ onClick={() => { changeTemplates({ file: preset.file, dir: preset.dir }); focused.current = false }}><span className="truncate">{preset.label}</span></Button>))}+ </div></div>: null}+ <p aria-live="polite" aria-busy={example.isFetching} data-organize-example=""+ className="text-caption-1-regular text-text-secondary">+ 路径示例:<code className="wrap-anywhere">{example.data?.example || example.data?.error+ || (example.isError ? errorMessage(example.error) : '正在预览…')}</code>+ </p>+ {data?.placeholders?.length ? <div className="flex flex-col gap-2" role="group" aria-label="插入参数">+ <p className="text-caption-1-regular text-text-secondary" aria-live="polite">+ <strong className="text-caption-1-semibold text-text-primary">插入参数</strong>(到{target === 'file' ? '文件名模板' : '目录模板'})+ </p>+ <div className="flex flex-wrap gap-1">+ {data.placeholders.map((item) => <Button key={item.key} variant="ghost" size="small"+ aria-label={`${item.label} {${item.key}}`} onClick={() => insertParameter(item.key)}>+ <span className="inline-flex flex-wrap items-center gap-x-1.5">+ <code>{`{${item.key}}`}</code><span className="text-text-secondary">{item.label}</span>+ </span>+ </Button>)}+ </div>+ </div> : null}<p className="text-caption-1-regular leading-4.5 text-text-secondary">- {`${hint ? `${hint};` : ''}方括号里的内容在字段为空时整段省略。`}+ 点击参数插入光标处,选中文字可替换。方括号里的内容在字段为空时整段省略。</p></div><div aria-live="polite" className="mt-3 flex flex-col gap-2 empty:hidden">{state}</div>diff --git a/frontend/src/react/data-cleanup/organize.ts b/frontend/src/react/data-cleanup/organize.tsindex 1ac165993..b1bb2aad7 100644--- a/frontend/src/react/data-cleanup/organize.ts+++ b/frontend/src/react/data-cleanup/organize.ts@@ -16,6 +16,11 @@ export const ORGANIZE_APPLY_URL = '/api/organize/apply';export const ORGANIZE_ROLLBACK_URL = '/api/organize/rollback';export const ORGANIZE_KEY = ['organize'] as const;+export const ORGANIZE_EXAMPLE_URL = '/api/organize/example';++export interface OrganizeExample { ok: boolean; example?: string; error?: string; field?: 'file' | 'dir' }+export const fetchOrganizeExample = (file: string, dir: string, signal?: AbortSignal) =>+ apiGet<OrganizeExample>(`${ORGANIZE_EXAMPLE_URL}?${new URLSearchParams({ file_template: file, dir_template: dir })}`, signal);export interface OrganizeTemplate { file: string; dir: string }@@ -36,7 +41,7 @@ export interface OrganizeJob {export interface OrganizeData extends OrganizeJob {locations: { location: string; roots: string[] }[];templates: Record<string, OrganizeTemplate>;- presets: ({ label: string } & OrganizeTemplate)[];+ presets: ({ label: string; example?: string } & OrganizeTemplate)[];placeholders: { key: string; label: string }[];last_batch: string;}diff --git a/frontend/test/react/data-cleanup.test.tsx b/frontend/test/react/data-cleanup.test.tsxindex 98bbdae1b..28164d790 100644--- a/frontend/test/react/data-cleanup.test.tsx+++ b/frontend/test/react/data-cleanup.test.tsx@@ -45,6 +45,10 @@ function serve(routes: Record<string, Route>) {const method = init?.method ?? 'GET';const body = init?.body ? JSON.parse(String(init.body)) : undefined;calls.push({ path, method, body });+ if (path.startsWith('/api/organize/example?')) {+ const params = new URL(path, 'http://localhost').searchParams;+ return { ok: true, status: 200, json: async () => ({ ok: true, example: `${params.get('dir_template')}/${params.get('file_template')}.mp4` }) };+ }if (!(path in routes)) {unknown.push(path);return { ok: false, status: 404, json: async () => ({}) };@@ -88,9 +92,9 @@ const withoutRoots: MediaSourcesData = {const organizeIdle: OrganizeData = {status: 'idle', locations: [{ location: 'local', roots: ['R:\\media'] }],- templates: { local: { file: '{number}', dir: '' } },- presets: [{ label: '番号 + 标题', file: '{number}[ {title}]', dir: '{studio}' }],- placeholders: [{ key: 'number', label: '番号' }, { key: 'title', label: '标题' }],+ templates: { local: { file: '{code}', dir: '' } },+ presets: [{ label: '番号 + 标题', file: '{code}[ {title}]', dir: '{studio}' }],+ placeholders: [{ key: 'code', label: '番号' }, { key: 'title', label: '标题' }],last_batch: '',};@@ -106,7 +110,7 @@ const pageRoutes = (sources: MediaSourcesData): Record<string, Route> => ({'/api/media-repair': IDLE_REPAIR,'/api/libraries': { libraries: [] },'/api/organize': organizeIdle,- '/api/links': { total: 0, entities: 0, by_kind: {}, top_hosts: [] },+ '/api/links': { total: 0, entities: 0, by_kind: {}, top_hosts: [], top_host_link_ids: {} },'/api/links/check': { status: 'idle', check_id: '', checked: 0, total: 0, gone: [], unclear: [], scope: '' },'/api/links/prune': { status: 'idle' },'/api/orphan-records': { total: 0, items: [] },@@ -343,6 +347,22 @@ describe('资源同步', () => {});describe('链接管理', () => {+ it('主要站点分别链接到主页并通过本站读取 favicon,图标失败时保留链接', async () => {+ const { host } = await openPage(withRoots, {+ '/api/links': { total: 8, entities: 3, by_kind: {}, top_hosts: [['example.com', 5], ['example.org', 3]], top_host_link_ids: { 'example.com': 8, 'example.org': 9 } },+ });+ const link = host.querySelector<HTMLAnchorElement>('#link-manager a[href="https://example.com"]')!;+ expect(link.textContent).toBe('example.com');+ expect(link.parentElement?.textContent).toBe('example.com5');+ expect(link.querySelector('svg')).not.toBeNull();+ expect(link.rel).toContain('noopener');+ const image = link.querySelector('img')!;+ expect(image.getAttribute('src')).toBe('/link-mark?id=8');+ await act(async () => { image.dispatchEvent(new Event('error')) });+ expect(link.querySelector('img')).toBeNull();+ expect(link.querySelector('svg')).not.toBeNull();+ expect(link.href).toBe('https://example.com/');+ });const row = (id: number, patch: Partial<LinkCheckState['gone'][number]> = {}) => ({id, entity: `女优 ${id}`, link_kind: 'official', label: '官网', url: `https://example.com/talent/${id}`, note: '404', ...patch,});@@ -353,6 +373,7 @@ describe('链接管理', () => {const stats = {total: 719, entities: 210, by_kind: { official: 224, social: 373, catalog: 100, source_reference: 22 },top_hosts: [['x.com', 300], ['instagram.com', 70], ['linktr.ee', 3], ['tiktok.com', 1]],+ top_host_link_ids: { 'x.com': 1, 'instagram.com': 2, 'linktr.ee': 3, 'tiktok.com': 4 },};it('每一类链接各占一格,类型用界面上的名字,主要站点只列前三', async () => {@@ -363,7 +384,8 @@ describe('链接管理', () => {expect(cells).toEqual(['链接总数719分布在 210 个女优、厂牌与系列', '官网/事务所224', '社交账号373', '作品资料站100', '资料出处22',]);- expect(host.textContent).toContain('x.com 300 · instagram.com 70 · linktr.ee 3');+ expect([...host.querySelectorAll('#link-manager a')].map((link) => link.textContent))+ .toEqual(['x.com', 'instagram.com', 'linktr.ee']);expect(host.textContent).not.toContain('tiktok.com');});@@ -527,6 +549,50 @@ describe('孤儿记录', () => {});describe('整理', () => {+ it('每个参数都能插入模板,默认追加到文件名并把光标留在参数后', async () => {+ serve({ '/api/organize': organizeIdle });+ queryClient.setQueryData(['organize'], organizeIdle);+ const host = await wrap(<OrganizeCard toast={vi.fn()} failure={vi.fn()} />);+ const file = host.querySelector<HTMLInputElement>('input[placeholder="{code}[ {title}]"]')!;+ for (const { key, label } of organizeIdle.placeholders) {+ const before = file.value;+ await click(host.querySelector(`button[aria-label="${label} {${key}}"]`));+ expect(file.value).toBe(`${before}{${key}}`);+ expect(document.activeElement).toBe(file);+ expect(file.selectionStart).toBe(file.value.length);+ }+ });++ it('参数替换目录模板的选区,连续插入沿用光标,不改变文件名', async () => {+ serve({ '/api/organize': organizeIdle });+ queryClient.setQueryData(['organize'], organizeIdle);+ const host = await wrap(<OrganizeCard toast={vi.fn()} failure={vi.fn()} />);+ const dir = host.querySelector<HTMLInputElement>('input[placeholder="留空表示原地改名"]')!;+ await type(dir, '厂牌/作品');+ await act(async () => { dir.focus(); dir.setSelectionRange(3, 5) });+ await click(host.querySelector('button[aria-label="番号 {code}"]'));+ expect(dir.value).toBe('厂牌/{code}');+ await click(host.querySelector('button[aria-label="标题 {title}"]'));+ expect(dir.value).toBe('厂牌/{code}{title}');+ expect(document.activeElement).toBe(dir);+ expect(host.querySelector<HTMLInputElement>('input[placeholder="{code}[ {title}]"]')!.value).toBe('{code}');+ });++ it('套用模板替换两个输入框,编辑参数后需要重新预览', async () => {+ const server = organizeServer();+ queryClient.setQueryData(['organize'], organizeIdle);+ const host = await wrap(<OrganizeCard toast={vi.fn()} failure={vi.fn()} />);+ await click(buttonNamed('预览', host));+ await settle();+ expect(buttonNamed('执行整理', host)).not.toBeNull();+ await click(buttonNamed('番号 + 标题', host));+ expect(buttonNamed('执行整理', host)).toBeNull();+ expect(host.querySelector<HTMLInputElement>('input[placeholder="{code}[ {title}]"]')!.value).toBe('{code}[ {title}]');+ expect(host.querySelector<HTMLInputElement>('input[placeholder="留空表示原地改名"]')!.value).toBe('{studio}');+ await click(host.querySelector('button[aria-label="标题 {title}"]'));+ expect(host.querySelector<HTMLInputElement>('input[placeholder="{code}[ {title}]"]')!.value).toBe('{code}[ {title}]{title}');+ expect(server.posts('/api/organize/apply')).toEqual([]);+ });const plan = {counts: { change: 2, unchanged: 5 }, reasons: { 缺番号: 1 }, truncated: false,rows: [@@ -549,13 +615,13 @@ describe('整理', () => {const server = organizeServer();queryClient.setQueryData(['organize'], organizeIdle);const host = await wrap(<OrganizeCard toast={vi.fn()} failure={vi.fn()} />);- const file = host.querySelector<HTMLInputElement>('input[placeholder="{number}[ {title}]"]');- expect(file?.value).toBe('{number}');- await type(file, ' {number} {title} ');+ const file = host.querySelector<HTMLInputElement>('input[placeholder="{code}[ {title}]"]');+ expect(file?.value).toBe('{code}');+ await type(file, ' {code} {title} ');await click(buttonNamed('预览', host));await settle();expect(server.posts('/api/organize/preview')).toEqual([- { location: 'local', path: '', file_template: '{number} {title}', dir_template: '' },+ { location: 'local', path: '', file_template: '{code} {title}', dir_template: '' },]);expect(server.posts('/api/settings')).toEqual([]);expect(host.textContent).toContain('2 个文件会改名或移动,5 个已经就是目标名字;跳过 缺番号 1。');@@ -575,10 +641,10 @@ describe('整理', () => {expect(seen.map((options) => options.title)).toEqual(['整理文件']);expect(seen[0]!.body).toContain('「本地」上的文件名与目录');expect(server.posts('/api/organize/apply')).toEqual([- { location: 'local', path: '', file_template: '{number}', dir_template: '', confirm: true },+ { location: 'local', path: '', file_template: '{code}', dir_template: '', confirm: true },]);expect(server.posts('/api/settings')).toEqual([- { organizeTemplates: { local: { file: '{number}', dir: '' } } },+ { organizeTemplates: { local: { file: '{code}', dir: '' } } },]);});diff --git a/src/peach/organize.py b/src/peach/organize.pyindex 3906eb232..4e742492b 100644--- a/src/peach/organize.py+++ b/src/peach/organize.py@@ -32,11 +32,17 @@ from .fsutil import atomic_write_text#: 计划 CSV 的列。`reason` 只在 `action=skip` 时有值。PLAN_FIELDS = ["asset_id", "location", "current_path", "target_path", "action", "reason"]-#: 界面上给的几组模板。第一组是最小可用的一套,后两组演示目录分层。+#: 文件名与目录组合,以及使用示例字段时的完整路径。PRESETS: tuple[dict[str, str], ...] = (- {"label": "番号 标题", "file": "{number}[ {title}]", "dir": ""},- {"label": "厂牌分目录", "file": "{number}[ {title}]", "dir": "{studio}"},- {"label": "厂牌 / 番号目录", "file": "{number}[ {title}][ CD{cd}]", "dir": "{studio}/{number}"},+ {"label": "仅番号", "file": "{code}", "dir": "", "example": "ABC-123.mp4"},+ {"label": "番号和标题", "file": "{code}[ {title}]", "dir": "", "example": "ABC-123 标题.mp4"},+ {"label": "番号和画质", "file": "{code}[ {definition}]", "dir": "", "example": "ABC-123 1080p.mp4"},+ {"label": "番号和出演者", "file": "{code}[ {actors}]", "dir": "", "example": "ABC-123 甲、乙.mp4"},+ {"label": "按厂牌建文件夹", "file": "{code}[ {title}]", "dir": "{studio}", "example": "厂牌/ABC-123 标题.mp4"},+ {"label": "按系列建文件夹", "file": "{code}[ {title}]", "dir": "{series}", "example": "系列/ABC-123 标题.mp4"},+ {"label": "按出演者建文件夹", "file": "{code}[ {title}]", "dir": "{actors}", "example": "甲、乙/ABC-123 标题.mp4"},+ {"label": "按发行年建文件夹", "file": "{code}[ {title}]", "dir": "{year}", "example": "2024/ABC-123 标题.mp4"},+ {"label": "厂牌下按番号建文件夹", "file": "{code}[ {title}][ CD{cd}]", "dir": "{studio}/{code}", "example": "厂牌/ABC-123/ABC-123 标题 CD2.mp4"},)#: 跳过原因的固定写法。界面按这几个键分组计数,所以它们是契约的一部分。@@ -128,7 +134,7 @@ def placeholder_values(row, performers: Sequence[str] = ()) -> dict[str, str]:release_date = str(row["release_date"] or "")marker = part_marker(name)return {- "number": normalise_code_key(code) if is_jav_code(code) else code.strip(),+ "code": normalise_code_key(code) if is_jav_code(code) else code.strip(),"title": title.strip(),"studio": str(row["studio"] or "").strip(),"series": str(row["series"] or "").strip(),diff --git a/src/peach/organize_templates.py b/src/peach/organize_templates.pyindex 5b222eea9..8180d2631 100644--- a/src/peach/organize_templates.py+++ b/src/peach/organize_templates.py@@ -1,7 +1,7 @@"""整理用的路径模板:占位符取值、可选分组、文件名清洗与逃逸防护(ADR-0039)。模板只有三种记号:字面量、`{占位符}`、`[可选分组]`。分组里的占位符只要有一个取不到值,-整段就不出现——`{number}[ {title}][ CD{cd}]` 在没有标题时给 `ABC-123`,而不是+整段就不出现——`{code}[ {title}][ CD{cd}]` 在没有标题时给 `ABC-123`,而不是`ABC-123 CD2` 这种带着两个空格的名字。占位符一律从 approved 真相字段取值(见 `organize.placeholder_values`)。候选不参与:@@ -24,7 +24,7 @@ from dataclasses import dataclass#: 模板认识的占位符,以及它在界面上的说明。顺序就是界面上列出来的顺序。PLACEHOLDERS: dict[str, str] = {- "number": "番号",+ "code": "番号","title": "标题","studio": "厂牌","series": "系列",@@ -35,6 +35,11 @@ PLACEHOLDERS: dict[str, str] = {"definition": "画质",}+EXAMPLE_VALUES = {+ "code": "ABC-123", "title": "标题", "studio": "厂牌", "series": "系列",+ "year": "2024", "actors": "甲、乙", "cd": "2", "mosaic": "无码", "definition": "1080p",+}+#: 单个路径组件的长度上限。Windows 的组件上限是 255 个字符,但长名字在资源管理器、#: 压缩包和网盘客户端里到处挨截断,所以留一大截余量;超出的从中间截断而不是末尾——#: 末尾往往是分片号和画质这些用来区分的部分。@@ -144,6 +149,9 @@ def _parse(template: str) -> tuple[_Token, ...]:if end < 0:raise TemplateError("占位符的 `{` 没有配对的 `}`")name = template[index + 1:end].strip()+ # 已保存的模板可以使用番号别名。+ if name == "number":+ name = "code"if name not in PLACEHOLDERS:raise TemplateError(f"不认识的占位符 `{{{name}}}`;可用的是 "@@ -267,3 +275,15 @@ def render(template: str, values: dict[str, str], *,else [sanitise_component(text)]components = [part for part in components if part]return "/".join(components), frozenset(missing)+++def example_path(file_template: str, dir_template: str) -> dict:+ """以固定示例字段渲染当前输入的路径。"""+ parts = {}+ for field, text, directory in (("file", file_template, False), ("dir", dir_template, True)):+ try:+ parts[field], _missing = render(text, EXAMPLE_VALUES, directory=directory)+ except TemplateError as error:+ return {"ok": False, "error": str(error), "field": field}+ filename = (parts["file"] if file_template.strip() else "ABC-123 标题") + ".mp4"+ return {"ok": True, "example": "/".join(filter(None, (parts["dir"], filename)))}diff --git a/src/peach/web_links.py b/src/peach/web_links.pyindex dc3bfaa51..739101b1d 100644--- a/src/peach/web_links.py+++ b/src/peach/web_links.py@@ -100,19 +100,23 @@ def w_links(contract: LinkContract, args=None):by_kind: dict[str, int] = {}by_entity_kind: dict[str, int] = {}hosts: dict[str, int] = {}+ host_link_ids: dict[str, int] = {}for row in rows:by_kind[row["link_kind"]] = by_kind.get(row["link_kind"], 0) + 1by_entity_kind[row["entity_kind"]] = by_entity_kind.get(row["entity_kind"], 0) + 1host = row["hostname"] or urlsplit(row["url"]).hostname or ""if host:hosts[host] = hosts.get(host, 0) + 1+ host_link_ids.setdefault(host, row["id"])+ top_hosts = sorted(hosts.items(), key=lambda item: (-item[1], item[0]))[:12]return {"ok": True,"total": len(rows),"entities": len({row["entity_id"] for row in rows}),"by_kind": by_kind,"by_entity_kind": by_entity_kind,- "top_hosts": sorted(hosts.items(), key=lambda item: (-item[1], item[0]))[:12],+ "top_hosts": top_hosts,+ "top_host_link_ids": {host: host_link_ids[host] for host, _ in top_hosts},}diff --git a/src/peach/web_organize.py b/src/peach/web_organize.pyindex 48d492051..7eacdf11b 100644--- a/src/peach/web_organize.py+++ b/src/peach/web_organize.py@@ -15,7 +15,7 @@ from pathlib import Pathfrom . import organizefrom .migrations import sqlite_backupfrom .media_sources import SCANNABLE_LOCATIONS-from .organize_templates import PLACEHOLDERS, TemplateError, validate_template+from .organize_templates import PLACEHOLDERS, TemplateError, example_path, validate_templatefrom .platform import location_rootsfrom .scripting import verify_after_writefrom .web_settings import organize_templates@@ -57,6 +57,12 @@ def q_organize(contract, _args=None) -> dict:}+def q_organize_example(_contract, args=None) -> dict:+ """以示例字段预览模板,保持账本与文件系统只读。"""+ args = args or {}+ return example_path(str(args.get("file_template") or ""), str(args.get("dir_template") or ""))++def w_organize_preview(contract, body) -> dict:"""出计划:写一份 CSV,回给界面会变的行(上限 200)与跳过统计。"""location, file_template, dir_template = _templates(contract, body)diff --git a/src/peach/web_router.py b/src/peach/web_router.pyindex e9b237e4b..a69e2ca63 100644--- a/src/peach/web_router.py+++ b/src/peach/web_router.py@@ -86,7 +86,7 @@ from .web_links import w_links, w_links_check, w_links_prunefrom .web_library_processing import q_library_processing, q_library_processing_issues, w_library_processingfrom .web_media_repair import q_media_repair, w_media_repairfrom .web_organize import (- q_organize, w_organize_apply, w_organize_preview, w_organize_rollback,+ q_organize, q_organize_example, w_organize_apply, w_organize_preview, w_organize_rollback,)from .web_orphan_records import q_orphan_records, w_orphan_attachfrom .web_playlists import q_playlist, q_playlists, w_playlist@@ -354,6 +354,7 @@ GET_HANDLERS = {"/api/media-repair": q_media_repair,"/api/orphan-records": q_orphan_records,"/api/organize": q_organize,+ "/api/organize/example": q_organize_example,"/api/items": q_items,"/api/item": _get_item,"/api/parts": q_parts,diff --git a/tests/test_organize.py b/tests/test_organize.pyindex f17388700..ee144697f 100644--- a/tests/test_organize.py+++ b/tests/test_organize.py@@ -19,7 +19,7 @@ ROOT_DECLARATION = "R:\\media"DECLARED = {"local": (ROOT_DECLARATION,)}VALUES = {- "number": "ABC-123", "title": "标题", "studio": "厂牌", "series": "系列",+ "code": "ABC-123", "title": "标题", "studio": "厂牌", "series": "系列","year": "2024", "actors": "甲、乙", "cd": "2", "mosaic": "无码","definition": "1080p",}@@ -28,6 +28,8 @@ VALUES = {class TemplateRenderTests(unittest.TestCase):CASES = [# (模板, 取值覆盖, 期望)+ ("{code}", {}, "ABC-123"),+ ("{code}[ {title}]", {}, "ABC-123 标题"),("{number}", {}, "ABC-123"),("{number} {title}", {}, "ABC-123 标题"),("{number}[ {title}]", {"title": ""}, "ABC-123"),@@ -66,6 +68,14 @@ class TemplateRenderTests(unittest.TestCase):text, _missing = render("{studio}/{number}", VALUES, directory=True)self.assertEqual(text, "厂牌/ABC-123")+ def test_presets_render_the_displayed_example_path(self):+ for preset in organize.PRESETS:+ with self.subTest(preset=preset["label"]):+ filename, missing = render(preset["file"], VALUES)+ directory, directory_missing = render(preset["dir"], VALUES, directory=True)+ self.assertFalse(missing | directory_missing)+ self.assertEqual("/".join(filter(None, (directory, filename + ".mp4"))), preset["example"])+class SanitiseTests(unittest.TestCase):def test_filename_symbols_and_invalid_unicode_are_removed(self):@@ -540,7 +550,19 @@ class EndpointTests(unittest.TestCase):payload = q_organize(self.contract)self.assertEqual(payload["status"], "idle")self.assertTrue(payload["presets"])- self.assertIn("number", [item["key"] for item in payload["placeholders"]])+ self.assertIn("code", [item["key"] for item in payload["placeholders"]])+ self.assertNotIn("number", [item["key"] for item in payload["placeholders"]])++ def test_custom_template_example_uses_the_registered_read_route(self):+ from peach.web_router import dispatch_api_get++ output = dispatch_api_get(None, "/api/organize/example", {+ "file_template": "自定义_{code}[_{definition}]", "dir_template": "{year}/{studio}"})+ self.assertEqual(output, {"ok": True, "example": "2024/厂牌/自定义_ABC-123_1080p.mp4"})+ invalid = dispatch_api_get(None, "/api/organize/example", {"file_template": "{code"})+ self.assertFalse(invalid["ok"])+ self.assertEqual(invalid["field"], "file")+ self.assertIn("没有配对", invalid["error"])if __name__ == "__main__":diff --git a/tests/test_rm_web.py b/tests/test_rm_web.pyindex 11579b241..a3f718910 100644--- a/tests/test_rm_web.py+++ b/tests/test_rm_web.py@@ -1179,7 +1179,7 @@ class WebDataTests(unittest.TestCase):"/api/taste/refresh","/api/links/prune", "/api/resource-sync/apply","/api/follow/tags", "/api/follow/authors",- "/api/taste", "/api/settings", "/api/links", "/api/organize", "/api/orphan-records",+ "/api/taste", "/api/settings", "/api/links", "/api/organize", "/api/organize/example", "/api/orphan-records","/api/feeds", "/api/feeds/check", "/api/feeds/discoveries", "/api/feeds/lookup","/api/downloads", "/api/resources/search","/api/wants", "/api/wants/magnets",diff --git a/tests/test_web_links.py b/tests/test_web_links.pyindex 788305b5c..d9002c700 100644--- a/tests/test_web_links.py+++ b/tests/test_web_links.py@@ -138,6 +138,11 @@ class SummaryTests(unittest.TestCase):self.assertEqual(info["by_kind"], {"social": 1, "official": 2})self.assertEqual(info["by_entity_kind"], {"performer": 2, "studio": 1})self.assertIn(("moodyz.com", 1), info["top_hosts"])+ with self.contract.read_connection() as connection:+ for host, _ in info["top_hosts"]:+ link = connection.execute("SELECT url FROM entity_link WHERE id=?",+ (info["top_host_link_ids"][host],)).fetchone()+ self.assertEqual(web_links.urlsplit(link["url"]).hostname, host)def test_prune_refuses_without_a_finished_check(self):"""没有检查结果就删除,等于凭空删。"""