Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(web): 统一作品资源展示与查询入口

longmeidao6de32e9e34b5
diff --git a/frontend/src/react/wants/resource-display.ts b/frontend/src/react/wants/resource-display.ts
new file mode 100644
index 000000000..380c3f10c
--- /dev/null
+++ b/frontend/src/react/wants/resource-display.ts
@@ -0,0 +1,14 @@
+import { fmtSize } from '@peach/legacy/core';
+
+/** 来源的字节数按馆藏容量口径展示,其他来源单位保留。 */
+export function resourceSize(value: string): string {
+ const bytes = /^([\d,]+)\s*B$/i.exec(value.trim());
+ return bytes ? fmtSize(Number(bytes[1].replaceAll(',', ''))) : value || '大小未知';
+}
+
+/** 缓存中的来源限制只表述本次取得范围,不推断账号权限。 */
+export function resourceNotice(value: string): string {
+ return /^更多评论.*(?:VIP|官方 App)/.test(value)
+ ? '来源只返回了部分评论,资源链接可能不完整。'
+ : value;
+}
diff --git a/frontend/src/react/wants/want-magnets.tsx b/frontend/src/react/wants/want-magnets.tsx
index 5b6c7a0f7..0d11a5518 100644
--- a/frontend/src/react/wants/want-magnets.tsx
+++ b/frontend/src/react/wants/want-magnets.tsx
@@ -1,6 +1,6 @@
/* 卡片下方的磁链候选。可见时查询,提交复用云下载的幂等与失败分类。 */
import { useEffect, useMemo, useRef, useState, type ComponentType, type ReactNode } from 'react';
-import { RiP2pLine, RiFileDownloadLine, RiLink, RiHardDrive2Line, RiFileList2Line,
+import { RiLink, RiHardDrive2Line, RiFileList2Line,
RiCalendarLine, RiPriceTag3Line, RiExternalLinkLine, RiFileCopyLine, RiCloudLine, RiRefreshLine } from '@remixicon/react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Button, ButtonLink } from '@/components/base/buttons/button';
@@ -15,6 +15,7 @@ import type { Want } from './wants';
import { Disclosure } from '../settings/section';
import { cachedResources, rememberResources, resourceLifetime } from './resource-cache';
import { PROVIDER_ICONS } from '../components/source-label';
+import { resourceNotice, resourceSize } from './resource-display';
export interface MagnetCandidate {
id: string; protocol: 'magnet' | 'ed2k' | 'url'; info_hash: string; uri: string;
@@ -31,9 +32,10 @@ function ResourceFact({ icon: Icon, children }: { icon: ComponentType<{ classNam
return <span className="inline-flex items-center gap-1"><Icon aria-hidden className="size-3.5 shrink-0" />{children}</span>;
}
-export function WantMagnets({ want, readOnly, downloads, provider, toast, onSource }: {
- want: Want; readOnly: boolean; downloads?: DownloadsSnapshot; provider: string; toast(message: string): void;
+export function WantMagnets({ want, readOnly, downloads, provider, toast, onSource, queryUrl }: {
+ want: Pick<Want, 'id' | 'code' | 'title' | 'release_date'>; readOnly: boolean; downloads?: DownloadsSnapshot; provider: string; toast(message: string): void;
onSource?(url: string): void;
+ queryUrl?: string;
}) {
const root = useRef<HTMLDivElement>(null);
const [visible, setVisible] = useState(typeof IntersectionObserver === 'undefined');
@@ -41,7 +43,8 @@ export function WantMagnets({ want, readOnly, downloads, provider, toast, onSour
const action = useAction();
const client = useQueryClient();
const refresh = useRef(false);
- const cached = useMemo(() => cachedResources(want.code || '', want.release_date), [want.code, want.release_date]);
+ const cacheCode = queryUrl ? `${queryUrl}:${want.code}` : want.code || '';
+ const cached = useMemo(() => cachedResources(cacheCode, want.release_date), [cacheCode, want.release_date]);
useEffect(() => {
if (!root.current || visible) return;
const observer = new IntersectionObserver((entries) => {
@@ -51,12 +54,13 @@ export function WantMagnets({ want, readOnly, downloads, provider, toast, onSour
return () => observer.disconnect();
}, [visible]);
const query = useQuery({
- queryKey: ['want-magnets', want.id, want.code],
+ queryKey: queryUrl ? ['work-resources', queryUrl, want.code] : ['want-magnets', want.id, want.code],
queryFn: async ({ signal }) => {
const forced = refresh.current;
- const result = await apiGet<MagnetResult>(`/api/wants/magnets?id=${want.id}${forced ? '&refresh=1' : ''}`, signal);
+ const url = queryUrl || `/api/wants/magnets?id=${want.id}`;
+ const result = await apiGet<MagnetResult>(`${url}${forced ? `${url.includes('?') ? '&' : '?'}refresh=1` : ''}`, signal);
refresh.current = forced && result.state === 'busy';
- rememberResources(want.code || '', result);
+ rememberResources(cacheCode, result);
return result;
},
initialData: cached?.result, initialDataUpdatedAt: cached?.saved,
@@ -79,7 +83,7 @@ export function WantMagnets({ want, readOnly, downloads, provider, toast, onSour
void action.run(candidate.id, async (signal) => {
const result = await apiSend<DownloadSubmitResult>(DOWNLOADS_URL, {
magnet: candidate.uri, provider: configured.key, target: configured.target,
- code: want.code, title: want.title, origin: `wishlist:${want.id}`,
+ code: want.code, title: want.title, origin: queryUrl ? `work:${want.code}` : `wishlist:${want.id}`,
}, 'POST', signal);
if (!result.ok) throw new Error(result.task.failure_detail || result.task.failure_label || '添加下载失败');
return result;
@@ -105,7 +109,9 @@ export function WantMagnets({ want, readOnly, downloads, provider, toast, onSour
</div>
{waiting ? <LoadingDots label="正在查找资源链接…" /> : null}
{failure ? <Note tone="error" title="资源链接未取得">{failure}</Note> : null}
- {query.data?.warnings?.map((warning) => <Note key={warning} tone="neutral">{warning}</Note>)}
+ {query.data?.warnings?.map((warning) => <Note key={warning} tone="neutral">{resourceNotice(warning)}</Note>)}
+ {query.data?.warnings?.length && query.data.source_url ? <ButtonLink variant="secondary" size="small" className="self-start"
+ trailingIcon={RiExternalLinkLine} href={query.data.source_url} target="_blank" rel="noopener noreferrer">查看来源页</ButtonLink> : null}
{query.data?.state === 'ready' && !query.data.items.length && !query.data.warnings?.length
? <Note tone="neutral">暂未找到资源链接。</Note> : null}
{action.error ? <Note tone="error" title="操作未完成">{action.error}</Note> : null}
@@ -113,21 +119,17 @@ export function WantMagnets({ want, readOnly, downloads, provider, toast, onSour
const rows = query.data.items.map((candidate) => {
const added = submitted.includes(candidate.id) || downloads?.tasks.some((task) => candidate.protocol === 'magnet' &&
task.info_hash === candidate.info_hash && (task.cancellable || task.state === 'ingested'));
- return <li key={candidate.id} data-resource-protocol={candidate.protocol} className="flex min-w-0 items-start justify-between gap-3 max-sm:flex-col">
+ return <li key={candidate.id} data-resource-protocol={candidate.protocol} className="flex min-w-0 items-center justify-between gap-3 max-sm:flex-col max-sm:items-start">
<div className="flex min-w-0 flex-1 flex-col gap-1">
<div className="flex min-w-0 flex-wrap items-center gap-2">
<Chip variant="caption" color={candidate.protocol === 'magnet' ? 'blue' : candidate.protocol === 'ed2k' ? 'purple' : 'neutral'}>
- <span className="inline-flex items-center gap-1">
- {candidate.protocol === 'magnet' ? <RiP2pLine aria-hidden className="size-3.5" /> :
- candidate.protocol === 'ed2k' ? <RiFileDownloadLine aria-hidden className="size-3.5" /> : <RiLink aria-hidden className="size-3.5" />}
- {candidate.protocol === 'magnet' ? '磁链' : candidate.protocol === 'ed2k' ? 'ed2k' : '网页链接'}
- </span>
+ {candidate.protocol === 'magnet' ? '磁链' : candidate.protocol === 'ed2k' ? 'ED2K' : '网页链接'}
</Chip>
<p className="min-w-0 break-all text-body-2-medium text-text-primary">{candidate.name}</p>
</div>
<div className="flex flex-wrap gap-x-3 gap-y-1 break-words text-caption-1-regular text-text-secondary">
<ResourceFact icon={RiLink}>{candidate.source}</ResourceFact>
- <ResourceFact icon={RiHardDrive2Line}>{candidate.size || '大小未知'}</ResourceFact>
+ <ResourceFact icon={RiHardDrive2Line}>{resourceSize(candidate.size)}</ResourceFact>
{candidate.files ? <ResourceFact icon={RiFileList2Line}>{candidate.files.replaceAll('個', '个')}</ResourceFact> : null}
<ResourceFact icon={RiCalendarLine}>{candidate.date ? `${candidate.source === 'JavDB 评论' ? '评论' : '收录'} ${candidate.date}` : '日期未知'}</ResourceFact>
{candidate.attributes.map((value) => <ResourceFact key={value} icon={RiPriceTag3Line}>{value}</ResourceFact>)}
diff --git a/frontend/test/react/want-magnets.test.tsx b/frontend/test/react/want-magnets.test.tsx
index a3144ccd2..bf8455359 100644
--- a/frontend/test/react/want-magnets.test.tsx
+++ b/frontend/test/react/want-magnets.test.tsx
@@ -6,6 +6,7 @@ import { WantMagnets, type MagnetCandidate } from '../../src/react/wants/want-ma
import type { Want } from '../../src/react/wants/wants';
import { buttonNamed, click, mount, mountRoot, pending, settle } from './render';
import { cachedResources, rememberResources, resourceLifetime } from '../../src/react/wants/resource-cache';
+import { resourceNotice, resourceSize } from '../../src/react/wants/resource-display';
notifyManager.setScheduler((notify) => notify());
afterEach(() => { queryClient.clear(); localStorage.clear() });
@@ -88,6 +89,33 @@ it('更多资源默认折叠且协议有独立标记', async () => {
expect(more.open).toBe(true);
});
+it('ED2K 类型使用文字,字节容量按馆藏口径展示', async () => {
+ const { host } = await open([{ ...candidate, protocol: 'ed2k', size: '27,311,519,717 B' }]);
+ expect(host.textContent).toContain('ED2K');
+ expect(host.textContent).toContain('25.4 GB');
+ expect(buttonNamed('添加下载', host)).toBeNull();
+ expect(resourceSize('1.61 GB')).toBe('1.61 GB');
+ expect(resourceSize('')).toBe('大小未知');
+ expect(resourceNotice('更多评论需要 JavDB VIP 或官方 App。')).toBe('来源只返回了部分评论,资源链接可能不完整。');
+});
+
+it('作品详情按自身来源查询资源,想要缓存不能代替公开来源结果', async () => {
+ vi.stubGlobal('IntersectionObserver', undefined);
+ rememberResources('DEMO-001', { state: 'ready', items: [candidate], error: '', checked_at: null });
+ const fetcher = vi.fn(async (_url: string) => ({ ok: true, status: 200,
+ json: async () => ({ state: 'ready', items: [], error: '', checked_at: null }) }));
+ vi.stubGlobal('fetch', fetcher);
+ const host = await mount(<QueryClientProvider client={queryClient}><WantMagnets
+ want={{ id: 0, code: 'DEMO-001', title: '示例作品', release_date: '2010-01-01' }}
+ queryUrl="/api/jav/resources?code=DEMO-001" readOnly provider="" toast={vi.fn()} /></QueryClientProvider>);
+ await settle();
+ expect(fetcher.mock.calls[0]?.[0]).toBe('/api/jav/resources?code=DEMO-001');
+ expect(host.textContent).toContain('暂未找到资源链接');
+ await click(buttonNamed('重新查询', host));
+ await settle();
+ expect(fetcher.mock.calls[1]?.[0]).toBe('/api/jav/resources?code=DEMO-001&refresh=1');
+});
+
it('发行三个月边界、月末与缓存有效期一致', () => {
const now = Date.parse('2026-10-03T12:00:00Z');
expect(resourceLifetime('2026-07-03', now)).toBe(7 * 86_400_000);
diff --git a/src/peach/sources/javdb.py b/src/peach/sources/javdb.py
index 9e76267da..a2f791384 100644
--- a/src/peach/sources/javdb.py
+++ b/src/peach/sources/javdb.py
@@ -102,7 +102,7 @@ class JavDBSource(SiteSource):
warnings.append("评论链接未取得,请打开来源页查看。")
break
if body.select_one('.more a[href="/plans"]'):
- warnings.append("更多评论需要 JavDB VIP 或官方 App。")
+ warnings.append("来源只返回了部分评论,资源链接可能不完整。")
following = body.select_one('a[rel="next"], .pagination-next:not([disabled])')
next_url = str(following.get('href', '')) if following else ""
else: