feat: add generated image gallery
This commit is contained in:
@@ -690,6 +690,26 @@
|
||||
"message": "auto-save 2026-05-19 13:40 (+1, ~2)",
|
||||
"hash": "f4ce3d4",
|
||||
"files_changed": 3
|
||||
},
|
||||
{
|
||||
"ts": "2026-05-19T13:45:51+08:00",
|
||||
"type": "commit",
|
||||
"message": "chore: align local docker environment",
|
||||
"hash": "c49e1b3",
|
||||
"files_changed": 6
|
||||
},
|
||||
{
|
||||
"ts": "2026-05-19T05:50:02Z",
|
||||
"type": "session-heartbeat",
|
||||
"message": "Codex 会话活跃 · 最近命令:codex · 分支 master · 1 项未提交变更 · 最近提交:chore: align local docker environment",
|
||||
"files_changed": 1
|
||||
},
|
||||
{
|
||||
"ts": "2026-05-19T13:56:44+08:00",
|
||||
"type": "commit",
|
||||
"message": "auto-save 2026-05-19 13:56 (+1, ~1)",
|
||||
"hash": "cdda350",
|
||||
"files_changed": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
185
src/app/api/gallery/[sessionId]/route.ts
Normal file
185
src/app/api/gallery/[sessionId]/route.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import { promises as fs } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { loadSession } from '@/lib/storage';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
type GalleryItem = {
|
||||
filename: string;
|
||||
url: string;
|
||||
group: string;
|
||||
label: string;
|
||||
sizeKb: number;
|
||||
mtime: number;
|
||||
current: boolean;
|
||||
};
|
||||
|
||||
function escapeHtml(input: string): string {
|
||||
return input
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function packGroup(filename: string): string {
|
||||
const match = filename.match(/^pack_([^_]+)_/);
|
||||
return match?.[1] ?? 'unknown';
|
||||
}
|
||||
|
||||
function fileLabel(filename: string): string {
|
||||
return filename
|
||||
.replace(/^pack_[^_]+_[^_]+_[^_]+_/, '')
|
||||
.replace(/\.[a-z0-9]+$/i, '')
|
||||
.replace(/_/g, ' ');
|
||||
}
|
||||
|
||||
async function listPackItems(currentFilenames: Set<string>): Promise<GalleryItem[]> {
|
||||
const dir = path.join(process.cwd(), 'data', 'packs');
|
||||
let files: string[] = [];
|
||||
try {
|
||||
files = await fs.readdir(dir);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
const items = await Promise.all(
|
||||
files
|
||||
.filter(filename => /\.(png|jpe?g|webp)$/i.test(filename))
|
||||
.map(async filename => {
|
||||
const stat = await fs.stat(path.join(dir, filename));
|
||||
return {
|
||||
filename,
|
||||
url: `/api/img/packs/${encodeURIComponent(filename)}`,
|
||||
group: packGroup(filename),
|
||||
label: fileLabel(filename),
|
||||
sizeKb: Math.round(stat.size / 1024),
|
||||
mtime: stat.mtimeMs,
|
||||
current: currentFilenames.has(filename),
|
||||
} satisfies GalleryItem;
|
||||
}),
|
||||
);
|
||||
|
||||
return items.sort((a, b) => {
|
||||
if (a.current !== b.current) return a.current ? -1 : 1;
|
||||
if (a.group !== b.group) return a.group.localeCompare(b.group);
|
||||
return a.mtime - b.mtime;
|
||||
});
|
||||
}
|
||||
|
||||
function renderItems(title: string, items: GalleryItem[]): string {
|
||||
const cards = items.map(item => `
|
||||
<article class="card">
|
||||
<a href="${item.url}" target="_blank" rel="noreferrer">
|
||||
<img src="${item.url}" loading="lazy" alt="${escapeHtml(item.filename)}" />
|
||||
</a>
|
||||
<div class="meta">
|
||||
<strong>${escapeHtml(item.group)} · ${escapeHtml(item.label)}</strong>
|
||||
<span>${escapeHtml(item.filename)}</span>
|
||||
<span>${item.sizeKb} KB</span>
|
||||
</div>
|
||||
</article>
|
||||
`).join('');
|
||||
|
||||
return `
|
||||
<section>
|
||||
<h2>${escapeHtml(title)} <small>${items.length}</small></h2>
|
||||
<div class="grid">${cards || '<p class="empty">No images.</p>'}</div>
|
||||
</section>
|
||||
`;
|
||||
}
|
||||
|
||||
function renderPage(opts: {
|
||||
sessionId: string;
|
||||
current: GalleryItem[];
|
||||
archived: GalleryItem[];
|
||||
packsSummary: string;
|
||||
sourceHtml: string;
|
||||
}): string {
|
||||
const total = opts.current.length + opts.archived.length;
|
||||
return `<!doctype html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>${escapeHtml(opts.sessionId)} Gallery</title>
|
||||
<style>
|
||||
:root { color-scheme: dark; font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: #0b0b0d; color: #f5f5f5; }
|
||||
body { margin: 0; padding: 28px; }
|
||||
header { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
|
||||
h1 { margin: 0 0 8px; font-size: 24px; }
|
||||
h2 { margin: 34px 0 14px; font-size: 18px; }
|
||||
small, .muted { color: #9ca3af; font-weight: 400; }
|
||||
.pillrow { display: flex; flex-wrap: wrap; gap: 8px; }
|
||||
.pill { border: 1px solid #2d2f36; border-radius: 999px; padding: 6px 10px; background: #15161b; color: #d1d5db; font-size: 12px; }
|
||||
.source { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; }
|
||||
.source a { color: #f5f5f5; text-decoration: none; border: 1px solid #2d2f36; border-radius: 10px; padding: 8px 10px; background: #15161b; }
|
||||
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
|
||||
.card { background: #15161b; border: 1px solid #2d2f36; border-radius: 12px; overflow: hidden; }
|
||||
.card img { width: 100%; aspect-ratio: 1 / 1; object-fit: contain; display: block; background: #fff; }
|
||||
.meta { padding: 10px; display: grid; gap: 5px; font-size: 11px; color: #9ca3af; }
|
||||
.meta strong { color: #f5f5f5; font-size: 12px; line-height: 1.25; }
|
||||
.empty { color: #9ca3af; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div>
|
||||
<h1>MUSE MATE 生成图库</h1>
|
||||
<div class="muted">Session: ${escapeHtml(opts.sessionId)}</div>
|
||||
${opts.sourceHtml}
|
||||
</div>
|
||||
<div class="pillrow">
|
||||
<span class="pill">总图 ${total}</span>
|
||||
<span class="pill">当前有效 ${opts.current.length}</span>
|
||||
<span class="pill">历史未挂载 ${opts.archived.length}</span>
|
||||
${opts.packsSummary}
|
||||
</div>
|
||||
</header>
|
||||
${renderItems('当前 session 有效图', opts.current)}
|
||||
${renderItems('历史生成图 / 未挂载但已保存', opts.archived)}
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
export async function GET(_req: Request, ctx: { params: Promise<{ sessionId: string }> }) {
|
||||
const { sessionId } = await ctx.params;
|
||||
if (!/^s_[a-z0-9_-]+$/i.test(sessionId)) {
|
||||
return NextResponse.json({ error: 'bad sessionId' }, { status: 400 });
|
||||
}
|
||||
|
||||
const session = await loadSession(sessionId);
|
||||
if (!session) return NextResponse.json({ error: 'session not found' }, { status: 404 });
|
||||
|
||||
const currentFilenames = new Set(
|
||||
(session.packs ?? [])
|
||||
.flatMap(pack => pack.assets ?? [])
|
||||
.map(asset => asset.url?.match(/\/api\/img\/packs\/([^/?#]+)$/)?.[1])
|
||||
.filter((filename): filename is string => Boolean(filename))
|
||||
.map(filename => decodeURIComponent(filename)),
|
||||
);
|
||||
const allItems = await listPackItems(currentFilenames);
|
||||
const sourceLinks = [
|
||||
session.characterSpec?.cleanReferenceImageUrl ? `<a href="${session.characterSpec.cleanReferenceImageUrl}" target="_blank" rel="noreferrer">L1 白底锚图</a>` : '',
|
||||
...(session.uploadedImages ?? []).map(upload => `<a href="${upload.url}" target="_blank" rel="noreferrer">上传图 ${escapeHtml(upload.originalFilename ?? upload.filename)}</a>`),
|
||||
].filter(Boolean).join('');
|
||||
const packsSummary = (session.packs ?? []).map(pack => (
|
||||
`<span class="pill">${escapeHtml(pack.kind)} ${pack.status} ${pack.assets.length}</span>`
|
||||
)).join('');
|
||||
|
||||
return new NextResponse(renderPage({
|
||||
sessionId,
|
||||
current: allItems.filter(item => item.current),
|
||||
archived: allItems.filter(item => !item.current),
|
||||
packsSummary,
|
||||
sourceHtml: sourceLinks ? `<div class="source">${sourceLinks}</div>` : '',
|
||||
}), {
|
||||
headers: {
|
||||
'Content-Type': 'text/html; charset=utf-8',
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user