mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-11-30 16:02:58 +08:00
feat: 日报新增模板市场,支持社区模板安装与使用
This commit is contained in:
1 parent
1156362c3c
commit
976aac32c5
35 files changed
+2957
-82
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import crypto from 'node:crypto'
|
||||
import { appendFileSync } from 'node:fs'
|
||||
import fs from 'fs-extra'
|
||||
import os from 'os'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import {
|
||||
GroupReportExportRequest,
|
||||
GroupReportExportResult,
|
||||
@@ -15,6 +18,8 @@ import {
|
||||
import { resolveMd5, getGroupSnapshot } from './services/chat-service'
|
||||
import { imageInsightService } from './services/image-insight-service'
|
||||
import { getReportTemplate } from '../shared/report-templates'
|
||||
import type { ReportTemplateRef } from '../shared/report-template-package'
|
||||
import { reportTemplateService, validateReportTemplateHtml } from './report-template-service'
|
||||
|
||||
const LEGACY_TEMPLATE_FILES: Record<string, string> = {
|
||||
v1: 'mobile_daily_report_v1.html',
|
||||
@@ -33,6 +38,40 @@ const templatePath = (templateId?: string): string => {
|
||||
return found
|
||||
}
|
||||
|
||||
const resolveTemplateSelection = async (templateId?: string, templateRef?: ReportTemplateRef) => {
|
||||
if (!templateRef) {
|
||||
const definition = getReportTemplate(templateId)
|
||||
return {
|
||||
definition,
|
||||
entryPath: templatePath(templateId),
|
||||
captureMaxHeight: 20000,
|
||||
source: 'builtin' as const
|
||||
}
|
||||
}
|
||||
const installed = await reportTemplateService.resolve(templateRef)
|
||||
return {
|
||||
definition: {
|
||||
id: installed.id,
|
||||
order: 999,
|
||||
platform: 'default' as const,
|
||||
label: installed.name,
|
||||
name: installed.name,
|
||||
tagline: `${installed.author} · ${installed.version}`,
|
||||
fileLabel: installed.name,
|
||||
cssClass: `template-external-${installed.id.replace(/[^a-z0-9-]/gi, '-')}`,
|
||||
resourceFile: installed.entryPath,
|
||||
captureWidth: installed.capture.width,
|
||||
maxCaptureWidth: installed.capture.maxWidth
|
||||
},
|
||||
entryPath: installed.entryPath,
|
||||
captureMaxHeight: installed.capture.maxHeight,
|
||||
source: 'installed' as const
|
||||
}
|
||||
}
|
||||
|
||||
const resolveTemplate = async (request: GroupReportExportRequest) =>
|
||||
resolveTemplateSelection(request.templateId, request.templateRef)
|
||||
|
||||
const escapeHtml = (value: unknown): string =>
|
||||
String(value ?? '')
|
||||
.replace(/&/g, '&')
|
||||
@@ -148,6 +187,52 @@ const heatClass = (heat: ReportHeat): string => {
|
||||
const replacePlaceholder = (html: string, key: string, value: string): string =>
|
||||
html.replaceAll(`{{${key}}}`, value)
|
||||
|
||||
const addReportCsp = (html: string, allowFileImages = true): string =>
|
||||
html.replace(
|
||||
/<head(\s[^>]*)?>/i,
|
||||
(head) =>
|
||||
`${head}<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src data:${allowFileImages ? ' file:' : ''}; style-src 'unsafe-inline'; font-src 'none'; base-uri 'none'; form-action 'none'">`
|
||||
)
|
||||
|
||||
const inlineTemplateAssets = async (html: string, templateRoot: string): Promise<string> => {
|
||||
const matches = [...html.matchAll(/\bsrc=(['"])(assets\/[A-Za-z0-9._/-]+)\1/g)]
|
||||
let result = html
|
||||
for (const match of matches) {
|
||||
const relative = match[2]
|
||||
const assetPath = path.resolve(templateRoot, relative)
|
||||
if (!assetPath.startsWith(`${path.resolve(templateRoot)}${path.sep}`)) continue
|
||||
const extension = path.extname(assetPath).toLowerCase()
|
||||
const mime =
|
||||
extension === '.png' ? 'image/png' : extension === '.webp' ? 'image/webp' : 'image/jpeg'
|
||||
const data = (await fs.readFile(assetPath)).toString('base64')
|
||||
result = result.replace(match[0], `src="data:${mime};base64,${data}"`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
const recordBlockedTemplateRequest = (
|
||||
details: Electron.OnBeforeRequestListenerDetails,
|
||||
reason: string
|
||||
): void => {
|
||||
// 仅测试入口设置该路径,用于保留运行时拦截证据;生产默认不记录请求明细。
|
||||
const logPath = process.env.TRACEMEMO_TEMPLATE_SECURITY_LOG
|
||||
if (!logPath) return
|
||||
try {
|
||||
appendFileSync(
|
||||
logPath,
|
||||
`${JSON.stringify({
|
||||
url: details.url,
|
||||
method: details.method,
|
||||
resourceType: details.resourceType,
|
||||
reason
|
||||
})}\n`,
|
||||
'utf8'
|
||||
)
|
||||
} catch (error) {
|
||||
console.warn('[GroupReport] failed to record template security block:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const sectionMeta = (
|
||||
request: GroupReportExportRequest,
|
||||
key: keyof NonNullable<typeof request.report.sectionMeta>
|
||||
@@ -170,7 +255,8 @@ const overflowNote = (
|
||||
|
||||
const renderReportHtml = async (request: GroupReportExportRequest): Promise<string> => {
|
||||
const { report, metadata } = request
|
||||
const template = getReportTemplate(request.templateId)
|
||||
const resolvedTemplate = await resolveTemplate(request)
|
||||
const template = resolvedTemplate.definition
|
||||
const avatarNames = new Set<string>(metadata.heroParticipants)
|
||||
report.topics.forEach((topic) => topic.participants.forEach((name) => avatarNames.add(name)))
|
||||
report.importantMessages.forEach((message) => avatarNames.add(message.sender))
|
||||
@@ -237,7 +323,7 @@ const renderReportHtml = async (request: GroupReportExportRequest): Promise<stri
|
||||
}
|
||||
}
|
||||
if (!imageUrl) return ''
|
||||
return `<div class="topic-inline-image"><img src="${imageUrl}" alt="热点图片"><div>${escapeHtml(topic.image.note)}</div></div>`
|
||||
return `<div class="topic-inline-image"><img src="${escapeHtml(imageUrl)}" alt="热点图片"><div>${escapeHtml(topic.image.note)}</div></div>`
|
||||
})()
|
||||
: ''
|
||||
}
|
||||
@@ -349,7 +435,7 @@ const renderReportHtml = async (request: GroupReportExportRequest): Promise<stri
|
||||
.filter((item) => item.imageUrl) // 只显示加载成功的图
|
||||
.map(
|
||||
(item) => `<div class="vision-card">
|
||||
<img class="vision-image" src="${item.imageUrl}" alt="AI 识别的图片">
|
||||
<img class="vision-image" src="${escapeHtml(item.imageUrl)}" alt="AI 识别的图片">
|
||||
<div class="vision-body">
|
||||
<div class="important-meta"><b>${escapeHtml(item.sender)}</b><span>${escapeHtml(item.time)}</span></div>
|
||||
<div class="vision-description">${escapeHtml(item.description)}</div>
|
||||
@@ -441,7 +527,11 @@ const renderReportHtml = async (request: GroupReportExportRequest): Promise<stri
|
||||
unresolvedCount: report.unresolved.length
|
||||
}
|
||||
|
||||
let html = await fs.readFile(templatePath(request.templateId), 'utf8')
|
||||
let html = await fs.readFile(resolvedTemplate.entryPath, 'utf8')
|
||||
if (resolvedTemplate.source === 'installed') {
|
||||
validateReportTemplateHtml(html, path.basename(resolvedTemplate.entryPath))
|
||||
html = await inlineTemplateAssets(html, path.dirname(resolvedTemplate.entryPath))
|
||||
}
|
||||
const values: Record<string, string> = {
|
||||
TEMPLATE_CLASS: template.cssClass,
|
||||
TEMPLATE_LABEL: escapeHtml(template.label),
|
||||
@@ -547,14 +637,19 @@ const renderReportHtml = async (request: GroupReportExportRequest): Promise<stri
|
||||
for (const [key, value] of Object.entries(values)) html = replacePlaceholder(html, key, value)
|
||||
// 清空模板中残留的未使用占位符(模板独有但 values 没提供的键)
|
||||
html = html.replace(/\{\{[A-Z_]+\}\}/g, '')
|
||||
return html
|
||||
return addReportCsp(html, resolvedTemplate.source === 'builtin')
|
||||
}
|
||||
|
||||
const renderReportSnapshotHtml = async (
|
||||
request: GroupReportRenderSnapshotExportRequest
|
||||
): Promise<string> => {
|
||||
const template = getReportTemplate(request.templateId)
|
||||
let html = await fs.readFile(templatePath(request.templateId), 'utf8')
|
||||
const resolvedTemplate = await resolveTemplateSelection(request.templateId, request.templateRef)
|
||||
const template = resolvedTemplate.definition
|
||||
let html = await fs.readFile(resolvedTemplate.entryPath, 'utf8')
|
||||
if (resolvedTemplate.source === 'installed') {
|
||||
validateReportTemplateHtml(html, path.basename(resolvedTemplate.entryPath))
|
||||
html = await inlineTemplateAssets(html, path.dirname(resolvedTemplate.entryPath))
|
||||
}
|
||||
const values = {
|
||||
...request.snapshot.values,
|
||||
TEMPLATE_CLASS: template.cssClass,
|
||||
@@ -565,7 +660,7 @@ const renderReportSnapshotHtml = async (
|
||||
REPORT_DATE: request.snapshot.values.REPORT_DATE || escapeHtml(request.snapshot.reportDate)
|
||||
}
|
||||
for (const [key, value] of Object.entries(values)) html = replacePlaceholder(html, key, value)
|
||||
return html.replace(/\{\{[A-Z0-9_]+\}\}/g, '')
|
||||
return addReportCsp(html.replace(/\{\{[A-Z0-9_]+\}\}/g, ''), resolvedTemplate.source === 'builtin')
|
||||
}
|
||||
|
||||
export const extractGroupReportRenderSnapshot = async (
|
||||
@@ -777,20 +872,61 @@ export const extractGroupReportRenderSnapshot = async (
|
||||
const captureFullPage = async (
|
||||
htmlPath: string,
|
||||
pngPath: string,
|
||||
templateId?: string
|
||||
templateId?: string,
|
||||
templateOverride?: {
|
||||
captureWidth: number
|
||||
maxCaptureWidth: number
|
||||
maxCaptureHeight?: number
|
||||
}
|
||||
): Promise<string> => {
|
||||
const template = getReportTemplate(templateId)
|
||||
const template = templateOverride || getReportTemplate(templateId)
|
||||
const captureWidth = LEGACY_TEMPLATE_FILES[templateId || ''] ? 430 : template.captureWidth
|
||||
const maxCaptureWidth = LEGACY_TEMPLATE_FILES[templateId || ''] ? 1200 : template.maxCaptureWidth
|
||||
const maxCaptureHeight = templateOverride?.maxCaptureHeight || 20000
|
||||
console.log(`[GroupReport] capture begin html=${htmlPath}`)
|
||||
const reportSessionPartition = `report-template-${crypto.randomUUID()}`
|
||||
const reportWindow = new BrowserWindow({
|
||||
show: false,
|
||||
width: captureWidth,
|
||||
height: 800,
|
||||
frame: false,
|
||||
backgroundColor: '#f3f5f7',
|
||||
webPreferences: { sandbox: true }
|
||||
webPreferences: {
|
||||
sandbox: true,
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
partition: reportSessionPartition
|
||||
}
|
||||
})
|
||||
const allowedDocumentPath = path.resolve(htmlPath)
|
||||
const requestHandler = (
|
||||
details: Electron.OnBeforeRequestListenerDetails,
|
||||
callback: (response: { cancel: boolean }) => void
|
||||
): void => {
|
||||
if (details.url.startsWith('file:')) {
|
||||
try {
|
||||
const requestedPath = path.resolve(fileURLToPath(details.url))
|
||||
if (requestedPath === allowedDocumentPath) {
|
||||
callback({ cancel: false })
|
||||
} else {
|
||||
recordBlockedTemplateRequest(details, 'file-path-not-allowed')
|
||||
callback({ cancel: true })
|
||||
}
|
||||
} catch {
|
||||
recordBlockedTemplateRequest(details, 'invalid-file-url')
|
||||
callback({ cancel: true })
|
||||
}
|
||||
return
|
||||
}
|
||||
recordBlockedTemplateRequest(details, 'scheme-not-allowed')
|
||||
callback({ cancel: true })
|
||||
}
|
||||
reportWindow.webContents.session.webRequest.onBeforeRequest(
|
||||
{ urls: ['http://*/*', 'https://*/*', 'ws://*/*', 'wss://*/*', 'file://*/*'] },
|
||||
requestHandler
|
||||
)
|
||||
reportWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' }))
|
||||
reportWindow.webContents.on('will-navigate', (event) => event.preventDefault())
|
||||
|
||||
try {
|
||||
await reportWindow.loadFile(htmlPath)
|
||||
@@ -808,7 +944,7 @@ const captureFullPage = async (
|
||||
height: Math.ceil(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, 800))
|
||||
})`)) as { width: number; height: number }
|
||||
const width = Math.max(captureWidth, Math.min(maxCaptureWidth, Math.ceil(metrics.width)))
|
||||
const height = Math.max(800, Math.min(20000, Math.ceil(metrics.height)))
|
||||
const height = Math.max(800, Math.min(maxCaptureHeight, Math.ceil(metrics.height)))
|
||||
reportWindow.setContentSize(width, height)
|
||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||
console.log(`[GroupReport] capture native page width=${width} height=${height}`)
|
||||
@@ -819,6 +955,7 @@ const captureFullPage = async (
|
||||
console.log(`[GroupReport] capture ok bytes=${png.length} png=${pngPath}`)
|
||||
return `data:image/png;base64,${png.toString('base64')}`
|
||||
} finally {
|
||||
reportWindow.webContents.session.webRequest.onBeforeRequest(null)
|
||||
reportWindow.destroy()
|
||||
}
|
||||
}
|
||||
@@ -830,10 +967,14 @@ export const exportGroupReport = async (
|
||||
// === enrich 在 render 之前:从群成员快照反推真头像 ===
|
||||
await enrichAvatarsFromGroup(request.metadata)
|
||||
|
||||
const outputDir = path.join(os.homedir(), 'Documents', '微信聊天记录')
|
||||
const outputDir =
|
||||
process.env.TRACEMEMO_REPORT_OUTPUT_DIR ||
|
||||
path.join(os.homedir(), 'Documents', '微信聊天记录')
|
||||
await fs.ensureDir(outputDir)
|
||||
const templateLabel =
|
||||
request.templateId === 'v1'
|
||||
const resolvedTemplate = await resolveTemplate(request)
|
||||
const templateLabel = request.templateRef
|
||||
? resolvedTemplate.definition.fileLabel
|
||||
: request.templateId === 'v1'
|
||||
? '经典版'
|
||||
: request.templateId === 'v2'
|
||||
? '丰富版'
|
||||
@@ -846,7 +987,15 @@ export const exportGroupReport = async (
|
||||
await fs.writeFile(htmlPath, html, 'utf8')
|
||||
const htmlEndedAt = new Date()
|
||||
const pngStartedAt = new Date()
|
||||
const imageDataUrl = await captureFullPage(htmlPath, pngPath, request.templateId)
|
||||
const imageDataUrl = await captureFullPage(
|
||||
htmlPath,
|
||||
pngPath,
|
||||
request.templateId,
|
||||
{
|
||||
...resolvedTemplate.definition,
|
||||
maxCaptureHeight: resolvedTemplate.captureMaxHeight
|
||||
}
|
||||
)
|
||||
const pngEndedAt = new Date()
|
||||
return {
|
||||
success: true,
|
||||
@@ -877,9 +1026,12 @@ export const exportGroupReportSnapshot = async (
|
||||
request: GroupReportRenderSnapshotExportRequest
|
||||
): Promise<GroupReportExportResult> => {
|
||||
try {
|
||||
const outputDir = path.join(os.homedir(), 'Documents', '微信聊天记录')
|
||||
const outputDir =
|
||||
process.env.TRACEMEMO_REPORT_OUTPUT_DIR ||
|
||||
path.join(os.homedir(), 'Documents', '微信聊天记录')
|
||||
await fs.ensureDir(outputDir)
|
||||
const templateLabel = getReportTemplate(request.templateId).fileLabel
|
||||
const resolvedTemplate = await resolveTemplateSelection(request.templateId, request.templateRef)
|
||||
const templateLabel = resolvedTemplate.definition.fileLabel
|
||||
const baseName = `${sanitizeFileName(request.snapshot.groupName)}日报_${request.snapshot.reportDate}_${templateLabel}`
|
||||
const htmlPath = path.join(outputDir, `${baseName}.html`)
|
||||
const pngPath = path.join(outputDir, `${baseName}.png`)
|
||||
@@ -888,7 +1040,15 @@ export const exportGroupReportSnapshot = async (
|
||||
await fs.writeFile(htmlPath, html, 'utf8')
|
||||
const htmlEndedAt = new Date()
|
||||
const pngStartedAt = new Date()
|
||||
const imageDataUrl = await captureFullPage(htmlPath, pngPath, request.templateId)
|
||||
const imageDataUrl = await captureFullPage(
|
||||
htmlPath,
|
||||
pngPath,
|
||||
request.templateId,
|
||||
{
|
||||
...resolvedTemplate.definition,
|
||||
maxCaptureHeight: resolvedTemplate.captureMaxHeight
|
||||
}
|
||||
)
|
||||
const pngEndedAt = new Date()
|
||||
return {
|
||||
success: true,
|
||||
|
||||
@@ -334,6 +334,11 @@ const routes: Record<string, RouteHandler> = {
|
||||
if (!request?.report || !request?.metadata) {
|
||||
return sendError(res, 400, '请求体需包含 report 和 metadata 字段')
|
||||
}
|
||||
if (request.templateRef !== undefined) {
|
||||
return sendError(res, 400, 'HTTP API 暂不支持外部日报模板,请使用内置 templateId', {
|
||||
code: 'external_template_unsupported'
|
||||
})
|
||||
}
|
||||
const result = await exportGroupReport(request)
|
||||
sendJson(res, result.success ? 200 : 500, result)
|
||||
},
|
||||
|
||||
+4
-5
@@ -32,7 +32,6 @@ import {
|
||||
type DecodedImage
|
||||
} from './image-decrypt-service'
|
||||
import {
|
||||
exportGroupReport,
|
||||
exportGroupReportSnapshot,
|
||||
extractGroupReportRenderSnapshot
|
||||
} from './group-report-service'
|
||||
@@ -43,8 +42,9 @@ import {
|
||||
saveGeneratedReport,
|
||||
updateGeneratedReportTemplate
|
||||
} from './report-history-service'
|
||||
import { reportTemplateService } from './report-template-service'
|
||||
import { registerReportTemplateIpc } from './report-template-ipc'
|
||||
import type {
|
||||
GroupReportExportRequest,
|
||||
GroupReportRenderSnapshotExportRequest
|
||||
} from '../shared/group-report'
|
||||
import type {
|
||||
@@ -667,6 +667,8 @@ app.whenReady().then(async () => {
|
||||
})
|
||||
|
||||
// Create the renderer before native WCDB bootstrap so startup progress is visible immediately.
|
||||
await reportTemplateService.recover()
|
||||
registerReportTemplateIpc()
|
||||
createWindow()
|
||||
wcdbBootstrapPromise = bootstrapWcdbNativeAsync().then(() => {
|
||||
console.log('[WCDB4] async bootstrap complete')
|
||||
@@ -1281,9 +1283,6 @@ app.whenReady().then(async () => {
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.handle('report:export', async (_, request: GroupReportExportRequest) => {
|
||||
return exportGroupReport(request)
|
||||
})
|
||||
ipcMain.handle(
|
||||
'report:exportSnapshot',
|
||||
async (_, request: GroupReportRenderSnapshotExportRequest) => exportGroupReportSnapshot(request)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { ipcMain } from 'electron'
|
||||
import { exportGroupReport } from './group-report-service'
|
||||
import { reportTemplateService } from './report-template-service'
|
||||
import { reportTemplateMarketService } from './report-template-market-service'
|
||||
import type { GroupReportExportRequest } from '../shared/group-report'
|
||||
|
||||
let registered = false
|
||||
|
||||
export function registerReportTemplateIpc(): void {
|
||||
if (registered) return
|
||||
registered = true
|
||||
ipcMain.handle('report-template:list', () => reportTemplateService.list())
|
||||
ipcMain.handle('report-template:install', async (_, packagePath: string) => {
|
||||
try {
|
||||
return { success: true, template: await reportTemplateService.install(packagePath) }
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
code: error instanceof Error && 'code' in error ? String((error as { code?: unknown }).code) : 'install_failed',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
ipcMain.handle('report-template:uninstall', async (_, id: string, version: string) => {
|
||||
try {
|
||||
await reportTemplateService.uninstall(id, version)
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
code: error instanceof Error && 'code' in error ? String((error as { code?: unknown }).code) : 'uninstall_failed',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
ipcMain.handle('report-template-market:list', () => reportTemplateMarketService.listCatalog())
|
||||
ipcMain.handle('report-template-market:install', (_, id: string, version: string) =>
|
||||
reportTemplateMarketService.installFromCatalog(String(id || ''), String(version || ''))
|
||||
)
|
||||
ipcMain.handle('report:export', (_, request: GroupReportExportRequest) => exportGroupReport(request))
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
import crypto from 'node:crypto'
|
||||
import { promises as fs } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { app } from 'electron'
|
||||
import {
|
||||
REPORT_TEMPLATE_INTERFACE_VERSION,
|
||||
REPORT_TEMPLATE_LIMITS,
|
||||
ReportTemplateError,
|
||||
type ReportTemplateOperationResult
|
||||
} from '../shared/report-template-package'
|
||||
import {
|
||||
REPORT_TEMPLATE_CATALOG_URL,
|
||||
type ReportTemplateCatalog,
|
||||
type ReportTemplateCatalogEntry,
|
||||
type ReportTemplateCatalogInstallResult,
|
||||
type ReportTemplateCatalogResult
|
||||
} from '../shared/report-template-market'
|
||||
import { reportTemplateService } from './report-template-service'
|
||||
|
||||
const CATALOG_MAX_BYTES = 2 * 1024 * 1024
|
||||
const REQUEST_TIMEOUT_MS = 15_000
|
||||
const SAFE_ID = /^community\.github\.[a-z0-9][a-z0-9-]{0,38}\.[a-z0-9][a-z0-9-]{0,63}$/
|
||||
const SAFE_VERSION = /^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/
|
||||
const SHA256 = /^[a-f0-9]{64}$/
|
||||
const COMMIT = /^[a-f0-9]{40}$/
|
||||
const RAW_HOST = 'raw.githubusercontent.com'
|
||||
const RAW_REPOSITORY_PREFIX = '/Wxw-Gu/TraceMemo-Templates/'
|
||||
|
||||
const operationError = (fallbackCode: string, error: unknown): ReportTemplateOperationResult => ({
|
||||
success: false,
|
||||
code: error instanceof ReportTemplateError ? error.code : fallbackCode,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
|
||||
const assertAllowedRemoteUrl = (value: unknown, label: string): URL => {
|
||||
let parsed: URL
|
||||
try {
|
||||
parsed = new URL(String(value || ''))
|
||||
} catch {
|
||||
throw new ReportTemplateError('invalid_catalog', `${label} URL 无效`)
|
||||
}
|
||||
if (
|
||||
parsed.protocol !== 'https:' ||
|
||||
parsed.hostname !== RAW_HOST ||
|
||||
parsed.port !== '' ||
|
||||
parsed.username ||
|
||||
parsed.password ||
|
||||
parsed.search ||
|
||||
parsed.hash ||
|
||||
!parsed.pathname.startsWith(RAW_REPOSITORY_PREFIX)
|
||||
) {
|
||||
throw new ReportTemplateError('invalid_catalog', `${label} 只允许 GitHub raw HTTPS 地址`)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
const assertAllowedPackageUrl = (value: unknown, label: string): URL => {
|
||||
const parsed = assertAllowedRemoteUrl(value, label)
|
||||
const segments = parsed.pathname.split('/').filter(Boolean)
|
||||
if (segments.length < 4 || !COMMIT.test(segments[2])) {
|
||||
throw new ReportTemplateError('invalid_catalog', `${label} 必须固定到模板仓库 commit`)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
const remoteCommit = (value: string): string | undefined => {
|
||||
try {
|
||||
return new URL(value).pathname.split('/').filter(Boolean)[2]
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
const fetchBytes = async (url: URL, maxBytes: number): Promise<Buffer> => {
|
||||
const response = await fetch(url, {
|
||||
headers: { Accept: 'application/json, application/zip, image/png', 'User-Agent': 'TraceMemo' },
|
||||
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
||||
redirect: 'error'
|
||||
})
|
||||
if (!response.ok) throw new ReportTemplateError('catalog_fetch_failed', `远端请求失败:HTTP ${response.status}`)
|
||||
const contentLength = Number(response.headers.get('content-length') || 0)
|
||||
if (contentLength > maxBytes) throw new ReportTemplateError('catalog_too_large', '远端模板目录或包超过大小限制')
|
||||
if (!response.body) {
|
||||
const bytes = Buffer.from(await response.arrayBuffer())
|
||||
if (bytes.length > maxBytes) throw new ReportTemplateError('catalog_too_large', '远端模板目录或包超过大小限制')
|
||||
return bytes
|
||||
}
|
||||
const reader = response.body.getReader()
|
||||
const chunks: Buffer[] = []
|
||||
let total = 0
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
total += value.byteLength
|
||||
if (total > maxBytes) {
|
||||
await reader.cancel()
|
||||
throw new ReportTemplateError('catalog_too_large', '远端模板目录或包超过大小限制')
|
||||
}
|
||||
chunks.push(Buffer.from(value))
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock()
|
||||
}
|
||||
return Buffer.concat(chunks, total)
|
||||
}
|
||||
|
||||
const validateCatalogEntry = (value: unknown): ReportTemplateCatalogEntry => {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new ReportTemplateError('invalid_catalog', '目录条目必须是对象')
|
||||
}
|
||||
const entry = value as Record<string, unknown>
|
||||
const id = String(entry.id || '')
|
||||
const version = String(entry.version || '')
|
||||
const interfaceVersion = String(entry.interfaceVersion || '')
|
||||
const sizeBytes = Number(entry.sizeBytes)
|
||||
if (!SAFE_ID.test(id) || !SAFE_VERSION.test(version)) {
|
||||
throw new ReportTemplateError('invalid_catalog', '目录模板 ID 或版本无效')
|
||||
}
|
||||
if (interfaceVersion !== REPORT_TEMPLATE_INTERFACE_VERSION) {
|
||||
throw new ReportTemplateError('unsupported_interface', `模板接口版本不兼容:${interfaceVersion}`)
|
||||
}
|
||||
if (entry.status !== 'published' || !Number.isSafeInteger(sizeBytes) || sizeBytes <= 0 || sizeBytes > REPORT_TEMPLATE_LIMITS.maxCompressedBytes) {
|
||||
throw new ReportTemplateError('invalid_catalog', `${id}@${version} 目录状态或大小无效`)
|
||||
}
|
||||
if (typeof entry.name !== 'string' || !entry.name.trim() || typeof entry.description !== 'string' || typeof entry.author !== 'string') {
|
||||
throw new ReportTemplateError('invalid_catalog', `${id}@${version} 目录名称或作者无效`)
|
||||
}
|
||||
if (!SHA256.test(String(entry.sha256 || ''))) throw new ReportTemplateError('invalid_catalog', `${id}@${version} SHA-256 无效`)
|
||||
const download = assertAllowedPackageUrl(entry.download, 'download')
|
||||
const preview = entry.preview === undefined || entry.preview === null ? undefined : assertAllowedPackageUrl(entry.preview, 'preview')
|
||||
const platform = entry.platform === 'mobile' || entry.platform === 'desktop' || entry.platform === 'default' ? entry.platform : undefined
|
||||
const tags = Array.isArray(entry.tags) && entry.tags.every((tag) => typeof tag === 'string') ? entry.tags : []
|
||||
return {
|
||||
id,
|
||||
version,
|
||||
interfaceVersion,
|
||||
name: entry.name.trim(),
|
||||
description: entry.description,
|
||||
author: entry.author,
|
||||
...(platform ? { platform } : {}),
|
||||
tags,
|
||||
license: typeof entry.license === 'string' ? entry.license : '',
|
||||
minAppVersion: typeof entry.minAppVersion === 'string' ? entry.minAppVersion : null,
|
||||
download: download.toString(),
|
||||
sizeBytes,
|
||||
sha256: String(entry.sha256),
|
||||
...(preview ? { preview: preview.toString() } : {}),
|
||||
publishedAt: typeof entry.publishedAt === 'string' ? entry.publishedAt : null,
|
||||
status: 'published'
|
||||
}
|
||||
}
|
||||
|
||||
const parseCatalog = (bytes: Buffer): ReportTemplateCatalog => {
|
||||
let value: unknown
|
||||
try {
|
||||
value = JSON.parse(bytes.toString('utf8'))
|
||||
} catch {
|
||||
throw new ReportTemplateError('invalid_catalog', '远端模板目录不是有效 JSON')
|
||||
}
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new ReportTemplateError('invalid_catalog', '远端模板目录格式无效')
|
||||
}
|
||||
const raw = value as Record<string, unknown>
|
||||
if (raw.schemaVersion !== '1' || raw.status !== 'published' || !Array.isArray(raw.templates) || raw.templates.length > 100) {
|
||||
throw new ReportTemplateError('invalid_catalog', '远端模板目录版本或状态无效')
|
||||
}
|
||||
const seen = new Set<string>()
|
||||
const templates = raw.templates.map(validateCatalogEntry)
|
||||
for (const entry of templates) {
|
||||
const key = `${entry.id}@${entry.version}`
|
||||
if (seen.has(key)) throw new ReportTemplateError('invalid_catalog', `目录包含重复模板:${key}`)
|
||||
seen.add(key)
|
||||
}
|
||||
const source = raw.source && typeof raw.source === 'object' && !Array.isArray(raw.source)
|
||||
? raw.source as Record<string, unknown>
|
||||
: undefined
|
||||
if (
|
||||
!source ||
|
||||
source.repository !== 'Wxw-Gu/TraceMemo-Templates' ||
|
||||
!COMMIT.test(String(source.commit || ''))
|
||||
) {
|
||||
throw new ReportTemplateError('invalid_catalog', '目录 source.repository 或 source.commit 无效')
|
||||
}
|
||||
for (const entry of templates) {
|
||||
if (
|
||||
remoteCommit(entry.download) !== source.commit ||
|
||||
(entry.preview !== undefined && remoteCommit(entry.preview) !== source.commit)
|
||||
) {
|
||||
throw new ReportTemplateError('invalid_catalog', `${entry.id}@${entry.version} 未固定到目录 source.commit`)
|
||||
}
|
||||
}
|
||||
return {
|
||||
schemaVersion: '1',
|
||||
...(typeof raw.generatedAt === 'string' ? { generatedAt: raw.generatedAt } : {}),
|
||||
...(source ? { source: { ...(typeof source.repository === 'string' ? { repository: source.repository } : {}), ...(typeof source.commit === 'string' ? { commit: source.commit } : {}) } } : {}),
|
||||
status: 'published',
|
||||
templates
|
||||
}
|
||||
}
|
||||
|
||||
const fetchCatalog = async (): Promise<ReportTemplateCatalog> => {
|
||||
const url = assertAllowedRemoteUrl(REPORT_TEMPLATE_CATALOG_URL, 'catalog')
|
||||
return parseCatalog(await fetchBytes(url, CATALOG_MAX_BYTES))
|
||||
}
|
||||
|
||||
export class ReportTemplateMarketService {
|
||||
async listCatalog(): Promise<ReportTemplateCatalogResult> {
|
||||
try {
|
||||
return { success: true, catalog: await fetchCatalog() }
|
||||
} catch (error) {
|
||||
return operationError('catalog_fetch_failed', error) as ReportTemplateCatalogResult
|
||||
}
|
||||
}
|
||||
|
||||
async installFromCatalog(id: string, version: string): Promise<ReportTemplateCatalogInstallResult> {
|
||||
let temporaryDirectory: string | undefined
|
||||
try {
|
||||
const catalog = await fetchCatalog()
|
||||
const entry = catalog.templates.find((candidate) => candidate.id === id && candidate.version === version)
|
||||
if (!entry) return { success: false, code: 'catalog_template_not_found', error: `远端目录不存在:${id}@${version}` }
|
||||
const bytes = await fetchBytes(assertAllowedPackageUrl(entry.download, 'download'), REPORT_TEMPLATE_LIMITS.maxCompressedBytes)
|
||||
const digest = crypto.createHash('sha256').update(bytes).digest('hex')
|
||||
if (bytes.length !== entry.sizeBytes || digest !== entry.sha256) {
|
||||
return { success: false, code: 'download_integrity_failed', error: `模板包校验失败:${id}@${version}`, catalogEntry: entry }
|
||||
}
|
||||
temporaryDirectory = await fs.mkdtemp(path.join(app.getPath('temp'), 'tracememo-template-market-'))
|
||||
const packagePath = path.join(temporaryDirectory, `${id}-${version}.zip`)
|
||||
await fs.writeFile(packagePath, bytes, { flag: 'wx' })
|
||||
const template = await reportTemplateService.install(packagePath, {
|
||||
id: entry.id,
|
||||
version: entry.version
|
||||
})
|
||||
return { success: true, template, catalogEntry: entry }
|
||||
} catch (error) {
|
||||
return { ...(operationError('market_install_failed', error) as ReportTemplateCatalogInstallResult) }
|
||||
} finally {
|
||||
if (temporaryDirectory) await fs.rm(temporaryDirectory, { recursive: true, force: true }).catch(() => undefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const reportTemplateMarketService = new ReportTemplateMarketService()
|
||||
@@ -0,0 +1,403 @@
|
||||
import crypto from 'node:crypto'
|
||||
import { promises as fs } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { Open } from 'unzipper'
|
||||
import { parseFragment } from 'parse5'
|
||||
import * as csstree from 'css-tree'
|
||||
import { app } from 'electron'
|
||||
import {
|
||||
DEFAULT_REPORT_TEMPLATE,
|
||||
REPORT_TEMPLATES,
|
||||
type ReportTemplateDefinition
|
||||
} from '../shared/report-templates'
|
||||
import {
|
||||
REPORT_TEMPLATE_ALLOWED_ASSET_EXTENSIONS,
|
||||
REPORT_TEMPLATE_ALLOWED_ATTRS,
|
||||
REPORT_TEMPLATE_ALLOWED_TAGS,
|
||||
REPORT_TEMPLATE_INTERFACE_VERSION,
|
||||
REPORT_TEMPLATE_LIMITS,
|
||||
REPORT_TEMPLATE_PACKAGE_PROTOCOL_VERSION,
|
||||
REPORT_TEMPLATE_PLACEHOLDERS,
|
||||
type InstalledReportTemplate,
|
||||
type ReportTemplateManifest,
|
||||
type ReportTemplateRef,
|
||||
ReportTemplateError
|
||||
} from '../shared/report-template-package'
|
||||
|
||||
const INDEX_FILE = 'index.json'
|
||||
const INSTALLED_DIR = 'installed'
|
||||
const STAGING_DIR = 'staging'
|
||||
const SAFE_ID = /^community\.github\.[a-z0-9][a-z0-9-]{0,38}\.[a-z0-9][a-z0-9-]{0,63}$/
|
||||
const SAFE_VERSION = /^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/
|
||||
const SAFE_RELATIVE_PATH = /^[^\\/][^:]*$/
|
||||
|
||||
interface TemplateIndex {
|
||||
version: 1
|
||||
templates: InstalledReportTemplate[]
|
||||
}
|
||||
|
||||
interface EntryInfo {
|
||||
path: string
|
||||
type: 'file' | 'directory'
|
||||
size: number
|
||||
entry: Awaited<ReturnType<typeof Open.file>>['files'][number]
|
||||
}
|
||||
|
||||
function rootPath(): string {
|
||||
return path.join(app.getPath('userData'), 'report-templates')
|
||||
}
|
||||
|
||||
function isWithin(root: string, candidate: string): boolean {
|
||||
const relative = path.relative(root, candidate)
|
||||
return relative === '' || (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
|
||||
}
|
||||
|
||||
function validateRelativePath(value: unknown, label: string): string {
|
||||
if (typeof value !== 'string' || !value || !SAFE_RELATIVE_PATH.test(value)) {
|
||||
throw new ReportTemplateError('invalid_path', `${label} 必须是包内相对路径`)
|
||||
}
|
||||
const normalized = path.posix.normalize(value.replace(/\\/g, '/'))
|
||||
if (normalized === '.' || normalized.startsWith('../') || normalized.includes('/../') || path.posix.isAbsolute(normalized)) {
|
||||
throw new ReportTemplateError('invalid_path', `${label} 不能越出模板包目录`)
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
function parseManifest(value: unknown): ReportTemplateManifest {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new ReportTemplateError('invalid_manifest', 'manifest.json 必须是对象')
|
||||
}
|
||||
const manifest = value as Record<string, unknown>
|
||||
const author = manifest.author as Record<string, unknown> | undefined
|
||||
const capture = manifest.capture as Record<string, unknown> | undefined
|
||||
const license = manifest.license as Record<string, unknown> | undefined
|
||||
if (manifest.protocolVersion !== REPORT_TEMPLATE_PACKAGE_PROTOCOL_VERSION) {
|
||||
throw new ReportTemplateError('unsupported_protocol', '模板包协议版本不受支持')
|
||||
}
|
||||
if (manifest.kind !== 'daily-report' || typeof manifest.id !== 'string' || !SAFE_ID.test(manifest.id)) {
|
||||
throw new ReportTemplateError('invalid_manifest', '模板类型或 ID 无效')
|
||||
}
|
||||
if (typeof manifest.name !== 'string' || !manifest.name.trim() || !author || typeof author.name !== 'string') {
|
||||
throw new ReportTemplateError('invalid_manifest', '模板名称和作者不能为空')
|
||||
}
|
||||
if (typeof manifest.templateVersion !== 'string' || !SAFE_VERSION.test(manifest.templateVersion)) {
|
||||
throw new ReportTemplateError('invalid_version', 'templateVersion 必须是 semver')
|
||||
}
|
||||
if (manifest.interfaceVersion !== REPORT_TEMPLATE_INTERFACE_VERSION) {
|
||||
throw new ReportTemplateError('unsupported_interface', '占位符接口版本不受支持')
|
||||
}
|
||||
const entry = validateRelativePath(manifest.entry, 'entry')
|
||||
const preview = manifest.preview === undefined ? undefined : validateRelativePath(manifest.preview, 'preview')
|
||||
const captureWidth = capture?.width
|
||||
const captureMaxWidth = capture?.maxWidth
|
||||
const captureMaxHeight = capture?.maxHeight
|
||||
if (!capture || !Number.isInteger(captureWidth) || !Number.isInteger(captureMaxWidth) || !Number.isInteger(captureMaxHeight)) {
|
||||
throw new ReportTemplateError('invalid_manifest', 'capture 尺寸无效')
|
||||
}
|
||||
if ((captureWidth as number) < 320 || (captureWidth as number) > 1920 || (captureMaxWidth as number) < (captureWidth as number) || (captureMaxWidth as number) > 1920 || (captureMaxHeight as number) < 800 || (captureMaxHeight as number) > 20000) {
|
||||
throw new ReportTemplateError('invalid_manifest', 'capture 尺寸超出允许范围')
|
||||
}
|
||||
if (!license || typeof license.spdx !== 'string' || !license.spdx.trim()) {
|
||||
throw new ReportTemplateError('invalid_manifest', '必须声明 license.spdx')
|
||||
}
|
||||
return {
|
||||
protocolVersion: manifest.protocolVersion,
|
||||
kind: 'daily-report',
|
||||
id: manifest.id,
|
||||
name: manifest.name.trim(),
|
||||
author: { name: author.name.trim(), ...(typeof author.homepage === 'string' ? { homepage: author.homepage } : {}) },
|
||||
templateVersion: manifest.templateVersion,
|
||||
interfaceVersion: manifest.interfaceVersion,
|
||||
entry,
|
||||
...(preview ? { preview } : {}),
|
||||
capture: { width: captureWidth as number, maxWidth: captureMaxWidth as number, maxHeight: captureMaxHeight as number },
|
||||
license: { spdx: license.spdx.trim(), ...(typeof license.notice === 'string' ? { notice: license.notice } : {}) },
|
||||
...(typeof manifest.minAppVersion === 'string' ? { minAppVersion: manifest.minAppVersion } : {}),
|
||||
...(manifest.platform === 'mobile' || manifest.platform === 'desktop' || manifest.platform === 'default' ? { platform: manifest.platform } : {})
|
||||
}
|
||||
}
|
||||
|
||||
function validateCss(css: string, fileName: string): void {
|
||||
if (Buffer.byteLength(css, 'utf8') > REPORT_TEMPLATE_LIMITS.maxCssBytes) {
|
||||
throw new ReportTemplateError('file_too_large', `${fileName} 超过 CSS 大小限制`)
|
||||
}
|
||||
let ast: csstree.CssNode
|
||||
try {
|
||||
ast = csstree.parse(css, { positions: false })
|
||||
} catch (error) {
|
||||
throw new ReportTemplateError('invalid_css', `${fileName} CSS 解析失败:${String(error)}`)
|
||||
}
|
||||
csstree.walk(ast, (node) => {
|
||||
if (node.type === 'Atrule' && node.name.toLowerCase() === 'import') {
|
||||
throw new ReportTemplateError('unsafe_css', `${fileName} 不允许 @import`)
|
||||
}
|
||||
if (node.type === 'Url' || (node.type === 'Function' && node.name.toLowerCase() === 'url')) {
|
||||
const raw = csstree.generate(node)
|
||||
if (/^(?:url\()?\s*(?:https?:|file:|data:|javascript:)/i.test(raw.trim())) {
|
||||
throw new ReportTemplateError('unsafe_url', `${fileName} 包含危险资源 URL`)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function validateReportTemplateHtml(html: string, fileName = 'template.html'): void {
|
||||
if (Buffer.byteLength(html, 'utf8') > REPORT_TEMPLATE_LIMITS.maxHtmlBytes) {
|
||||
throw new ReportTemplateError('file_too_large', `${fileName} 超过 HTML 大小限制`)
|
||||
}
|
||||
const placeholders = [...html.matchAll(/\{\{([A-Z0-9_]+)\}\}/g)].map((match) => match[1])
|
||||
for (const key of placeholders) {
|
||||
if (!REPORT_TEMPLATE_PLACEHOLDERS[key]) throw new ReportTemplateError('unknown_placeholder', `不支持占位符 {{${key}}}`)
|
||||
}
|
||||
const fragment = parseFragment(html)
|
||||
const visit = (node: { nodeName: string; attrs?: Array<{ name: string; value: string }>; childNodes?: unknown[] }): void => {
|
||||
const tag = node.nodeName.toLowerCase()
|
||||
if (tag === '#text' || tag === '#comment' || tag === '#document-fragment') {
|
||||
for (const child of (node.childNodes || []) as Array<{ nodeName: string; attrs?: Array<{ name: string; value: string }>; childNodes?: unknown[] }>) visit(child)
|
||||
return
|
||||
}
|
||||
if (!REPORT_TEMPLATE_ALLOWED_TAGS.has(tag)) throw new ReportTemplateError('unsafe_html', `${fileName} 不允许标签 <${tag}>`)
|
||||
for (const attr of node.attrs || []) {
|
||||
const name = attr.name.toLowerCase()
|
||||
if (!REPORT_TEMPLATE_ALLOWED_ATTRS.has(name) || name.startsWith('on')) throw new ReportTemplateError('unsafe_html', `${fileName} 不允许属性 ${attr.name}`)
|
||||
if (/\{\{[A-Z0-9_]+\}\}/.test(attr.value)) {
|
||||
const keys = [...attr.value.matchAll(/\{\{([A-Z0-9_]+)\}\}/g)].map((match) => match[1])
|
||||
if (name !== 'class' || keys.some((key) => REPORT_TEMPLATE_PLACEHOLDERS[key] !== 'class')) {
|
||||
throw new ReportTemplateError('invalid_placeholder_context', `占位符不能出现在 ${name} 属性中`)
|
||||
}
|
||||
}
|
||||
if (name === 'src') {
|
||||
const assetPath = path.posix.normalize(attr.value.replace(/\\/g, '/'))
|
||||
if (!assetPath.startsWith('assets/') || !REPORT_TEMPLATE_ALLOWED_ASSET_EXTENSIONS.has(path.posix.extname(assetPath).toLowerCase())) {
|
||||
throw new ReportTemplateError('unsafe_url', `${fileName} 只允许 assets/ 下的图片资源`)
|
||||
}
|
||||
}
|
||||
if (['href', 'action', 'poster'].includes(name)) throw new ReportTemplateError('unsafe_url', `${fileName} 不允许 URL 属性`)
|
||||
}
|
||||
if (tag === 'style') {
|
||||
const text = (node.childNodes || []).map((child) => (child as { value?: string }).value || '').join('')
|
||||
if (/\{\{[A-Z0-9_]+\}\}/.test(text)) throw new ReportTemplateError('invalid_placeholder_context', '占位符不能出现在 style 中')
|
||||
validateCss(text, fileName)
|
||||
}
|
||||
for (const child of (node.childNodes || []) as Array<{ nodeName: string; attrs?: Array<{ name: string; value: string }>; childNodes?: unknown[] }>) visit(child)
|
||||
}
|
||||
visit(fragment)
|
||||
}
|
||||
|
||||
function collectTemplateAssetRefs(html: string): string[] {
|
||||
const refs: string[] = []
|
||||
const fragment = parseFragment(html)
|
||||
const visit = (node: { nodeName: string; attrs?: Array<{ name: string; value: string }>; childNodes?: unknown[] }): void => {
|
||||
if (node.nodeName.toLowerCase() === 'img') {
|
||||
const src = node.attrs?.find((attr) => attr.name.toLowerCase() === 'src')?.value
|
||||
if (src) refs.push(path.posix.normalize(src.replace(/\\/g, '/')))
|
||||
}
|
||||
for (const child of (node.childNodes || []) as Array<{ nodeName: string; attrs?: Array<{ name: string; value: string }>; childNodes?: unknown[] }>) visit(child)
|
||||
}
|
||||
visit(fragment)
|
||||
return refs
|
||||
}
|
||||
|
||||
async function readIndex(directory: string): Promise<TemplateIndex> {
|
||||
try {
|
||||
const value = JSON.parse(await fs.readFile(path.join(directory, INDEX_FILE), 'utf8')) as Partial<TemplateIndex>
|
||||
if (value.version !== 1 || !Array.isArray(value.templates)) throw new Error('invalid index')
|
||||
return { version: 1, templates: value.templates }
|
||||
} catch {
|
||||
return { version: 1, templates: [] }
|
||||
}
|
||||
}
|
||||
|
||||
async function writeIndex(directory: string, index: TemplateIndex): Promise<void> {
|
||||
const temporary = `${path.join(directory, INDEX_FILE)}.tmp-${process.pid}-${Date.now()}`
|
||||
await fs.writeFile(temporary, JSON.stringify(index, null, 2), 'utf8')
|
||||
await fs.rename(temporary, path.join(directory, INDEX_FILE))
|
||||
}
|
||||
|
||||
function builtInTemplate(template: ReportTemplateDefinition): InstalledReportTemplate {
|
||||
const resourceRoot = path.join(process.resourcesPath || process.cwd(), 'resources')
|
||||
const entryPath = path.join(resourceRoot, template.resourceFile)
|
||||
return {
|
||||
id: template.id,
|
||||
version: 'builtin',
|
||||
interfaceVersion: REPORT_TEMPLATE_INTERFACE_VERSION,
|
||||
source: 'builtin',
|
||||
name: template.name,
|
||||
author: 'TraceMemo',
|
||||
entryPath,
|
||||
capture: { width: template.captureWidth, maxWidth: template.maxCaptureWidth, maxHeight: 20000 },
|
||||
license: { spdx: 'NOASSERTION' }
|
||||
}
|
||||
}
|
||||
|
||||
export class ReportTemplateService {
|
||||
private readonly directory = rootPath()
|
||||
|
||||
async list(): Promise<InstalledReportTemplate[]> {
|
||||
await fs.mkdir(path.join(this.directory, INSTALLED_DIR), { recursive: true })
|
||||
const index = await readIndex(this.directory)
|
||||
return [builtInTemplate(DEFAULT_REPORT_TEMPLATE), ...REPORT_TEMPLATES.map(builtInTemplate), ...index.templates]
|
||||
}
|
||||
|
||||
async resolve(ref: ReportTemplateRef): Promise<InstalledReportTemplate> {
|
||||
const id = String(ref?.id || '').trim()
|
||||
if (!id) throw new ReportTemplateError('missing_template', '缺少模板 ID')
|
||||
const templates = await this.list()
|
||||
const candidates = templates.filter((template) => template.id === id)
|
||||
if (!candidates.length) throw new ReportTemplateError('template_not_found', `模板不存在:${id}`)
|
||||
if (candidates[0].source === 'builtin') {
|
||||
throw new ReportTemplateError('builtin_template_ref', `外部模板引用不能使用内置模板:${id}`)
|
||||
}
|
||||
const found = ref.version ? candidates.find((template) => template.version === ref.version) : candidates.sort((a, b) => b.version.localeCompare(a.version))[0]
|
||||
if (!found) throw new ReportTemplateError('template_version_not_found', `模板版本不存在:${id}@${ref.version}`)
|
||||
if (found.interfaceVersion !== REPORT_TEMPLATE_INTERFACE_VERSION) throw new ReportTemplateError('unsupported_interface', `模板接口版本不兼容:${found.interfaceVersion}`)
|
||||
return found
|
||||
}
|
||||
|
||||
async install(packagePath: string, expectedRef?: ReportTemplateRef): Promise<InstalledReportTemplate> {
|
||||
const stat = await fs.stat(packagePath)
|
||||
if (!stat.isFile() || stat.size > REPORT_TEMPLATE_LIMITS.maxCompressedBytes) throw new ReportTemplateError('package_too_large', '模板 ZIP 超过大小限制')
|
||||
const archive = await Open.file(packagePath)
|
||||
if (archive.files.length === 0 || archive.files.length > REPORT_TEMPLATE_LIMITS.maxFiles) throw new ReportTemplateError('too_many_files', '模板包文件数量无效')
|
||||
const seen = new Set<string>()
|
||||
const entries: EntryInfo[] = []
|
||||
let declaredTotal = 0
|
||||
for (const entry of archive.files) {
|
||||
const normalized = validateRelativePath(entry.path, 'ZIP 条目')
|
||||
const collisionKey = normalized.toLowerCase()
|
||||
if (seen.has(collisionKey)) throw new ReportTemplateError('duplicate_entry', `ZIP 条目规范化后重复:${normalized}`)
|
||||
seen.add(collisionKey)
|
||||
if (entry.type !== 'Directory' && entry.type !== 'File') throw new ReportTemplateError('unsafe_entry', `ZIP 条目类型不受支持:${normalized}`)
|
||||
const type = entry.type === 'Directory' ? 'directory' : 'file'
|
||||
const size = Number(entry.uncompressedSize || 0)
|
||||
if (type === 'file' && size > REPORT_TEMPLATE_LIMITS.maxFileBytes) throw new ReportTemplateError('file_too_large', `ZIP 条目过大:${normalized}`)
|
||||
if (type === 'file') declaredTotal += size
|
||||
if (declaredTotal > REPORT_TEMPLATE_LIMITS.maxExtractedBytes) throw new ReportTemplateError('package_too_large', '模板包解压体积超过限制')
|
||||
if (type === 'file' && (normalized.endsWith('.html') || normalized.endsWith('.css')) && size > REPORT_TEMPLATE_LIMITS.maxHtmlBytes) throw new ReportTemplateError('file_too_large', `模板文件过大:${normalized}`)
|
||||
entries.push({ path: normalized, type, size, entry })
|
||||
}
|
||||
const manifestEntry = entries.find((entry) => entry.path === 'manifest.json' && entry.type === 'file')
|
||||
if (!manifestEntry) throw new ReportTemplateError('invalid_manifest', '模板包缺少 manifest.json')
|
||||
const manifest = parseManifest(JSON.parse((await manifestEntry.entry.buffer()).toString('utf8')))
|
||||
if (manifest.id.startsWith('builtin.')) throw new ReportTemplateError('reserved_id', '外部模板不能使用内置命名空间')
|
||||
if (
|
||||
expectedRef &&
|
||||
(manifest.id !== expectedRef.id || manifest.templateVersion !== expectedRef.version)
|
||||
) {
|
||||
throw new ReportTemplateError(
|
||||
'catalog_manifest_mismatch',
|
||||
`模板包 manifest 与目录条目不一致:${manifest.id}@${manifest.templateVersion}`
|
||||
)
|
||||
}
|
||||
const entry = entries.find((item) => item.path === manifest.entry && item.type === 'file')
|
||||
if (!entry) throw new ReportTemplateError('missing_entry', 'manifest.entry 文件不存在')
|
||||
const html = (await entry.entry.buffer()).toString('utf8')
|
||||
validateReportTemplateHtml(html, manifest.entry)
|
||||
const assetPaths = new Set(entries.filter((item) => item.type === 'file').map((item) => item.path))
|
||||
for (const asset of collectTemplateAssetRefs(html)) {
|
||||
if (!assetPaths.has(asset)) throw new ReportTemplateError('missing_asset', `模板图片资源不存在:${asset}`)
|
||||
}
|
||||
const preview = manifest.preview ? entries.find((item) => item.path === manifest.preview && item.type === 'file') : undefined
|
||||
if (manifest.preview && !preview) throw new ReportTemplateError('missing_preview', 'manifest.preview 文件不存在')
|
||||
for (const item of entries.filter((candidate) => candidate.type === 'file')) {
|
||||
const ext = path.extname(item.path).toLowerCase()
|
||||
if (item.path !== 'manifest.json' && item.path !== manifest.entry && item.path !== manifest.preview && !REPORT_TEMPLATE_ALLOWED_ASSET_EXTENSIONS.has(ext)) {
|
||||
throw new ReportTemplateError('unsupported_resource', `不支持的模板资源:${item.path}`)
|
||||
}
|
||||
}
|
||||
const staging = path.join(this.directory, STAGING_DIR, `${process.pid}-${Date.now()}-${crypto.randomUUID()}`)
|
||||
await fs.mkdir(staging, { recursive: true })
|
||||
try {
|
||||
let actualTotal = 0
|
||||
for (const item of entries) {
|
||||
const destination = path.resolve(staging, item.path)
|
||||
if (!isWithin(staging, destination)) throw new ReportTemplateError('invalid_path', `ZIP 条目越界:${item.path}`)
|
||||
if (item.type === 'directory') {
|
||||
await fs.mkdir(destination, { recursive: true })
|
||||
continue
|
||||
}
|
||||
await fs.mkdir(path.dirname(destination), { recursive: true })
|
||||
const stream = item.entry.stream()
|
||||
const chunks: Buffer[] = []
|
||||
for await (const chunk of stream) {
|
||||
const buffer = Buffer.from(chunk as Uint8Array)
|
||||
actualTotal += buffer.length
|
||||
if (actualTotal > REPORT_TEMPLATE_LIMITS.maxExtractedBytes || buffer.length > REPORT_TEMPLATE_LIMITS.maxFileBytes) throw new ReportTemplateError('package_too_large', '模板包实际解压体积超过限制')
|
||||
chunks.push(buffer)
|
||||
}
|
||||
await fs.writeFile(destination, Buffer.concat(chunks))
|
||||
const fileStat = await fs.lstat(destination)
|
||||
if (!fileStat.isFile()) throw new ReportTemplateError('invalid_file', `模板文件类型无效:${item.path}`)
|
||||
}
|
||||
const target = path.join(this.directory, INSTALLED_DIR, manifest.id, manifest.templateVersion)
|
||||
const existingIndex = await readIndex(this.directory)
|
||||
const existing = existingIndex.templates.find((item) => item.id === manifest.id && item.version === manifest.templateVersion)
|
||||
const digest = crypto.createHash('sha256').update(await fs.readFile(packagePath)).digest('hex')
|
||||
if (existing) {
|
||||
if (existing.sha256 !== digest) throw new ReportTemplateError('version_conflict', `模板版本已存在但内容不同:${manifest.id}@${manifest.templateVersion}`)
|
||||
return existing
|
||||
}
|
||||
await fs.mkdir(path.dirname(target), { recursive: true })
|
||||
await fs.rename(staging, target)
|
||||
const installed: InstalledReportTemplate = {
|
||||
id: manifest.id, version: manifest.templateVersion, interfaceVersion: manifest.interfaceVersion,
|
||||
source: 'installed', name: manifest.name, author: manifest.author.name,
|
||||
entryPath: path.join(target, manifest.entry), ...(manifest.preview ? { previewPath: path.join(target, manifest.preview) } : {}),
|
||||
capture: manifest.capture, license: manifest.license, packagePath, sha256: digest, installedAt: new Date().toISOString()
|
||||
}
|
||||
existingIndex.templates = [...existingIndex.templates.filter((item) => !(item.id === installed.id && item.version === installed.version)), installed]
|
||||
await writeIndex(this.directory, existingIndex)
|
||||
return installed
|
||||
} finally {
|
||||
// 同版本幂等返回也必须清理 staging,避免重复安装逐渐堆积临时目录。
|
||||
await fs.rm(staging, { recursive: true, force: true }).catch(() => undefined)
|
||||
}
|
||||
}
|
||||
|
||||
async uninstall(id: string, version: string): Promise<void> {
|
||||
if (!SAFE_ID.test(id) || !SAFE_VERSION.test(version)) {
|
||||
throw new ReportTemplateError('invalid_template_ref', '模板 ID 或版本无效')
|
||||
}
|
||||
const index = await readIndex(this.directory)
|
||||
const target = index.templates.find((item) => item.id === id && item.version === version)
|
||||
if (!target) throw new ReportTemplateError('template_not_found', `模板版本不存在:${id}@${version}`)
|
||||
await fs.rm(path.join(this.directory, INSTALLED_DIR, id, version), { recursive: true, force: true })
|
||||
index.templates = index.templates.filter((item) => item !== target)
|
||||
await writeIndex(this.directory, index)
|
||||
}
|
||||
|
||||
async recover(): Promise<void> {
|
||||
const installedRoot = path.join(this.directory, INSTALLED_DIR)
|
||||
await fs.mkdir(installedRoot, { recursive: true })
|
||||
const previous = await readIndex(this.directory)
|
||||
const recovered: InstalledReportTemplate[] = []
|
||||
for (const id of await fs.readdir(installedRoot)) {
|
||||
for (const version of await fs.readdir(path.join(installedRoot, id))) {
|
||||
const entryPath = path.join(installedRoot, id, version)
|
||||
try {
|
||||
const manifest = parseManifest(JSON.parse(await fs.readFile(path.join(entryPath, 'manifest.json'), 'utf8')))
|
||||
const recoveredEntryPath = path.join(entryPath, manifest.entry)
|
||||
const entryStat = await fs.lstat(recoveredEntryPath)
|
||||
if (!entryStat.isFile()) continue
|
||||
const candidate = previous.templates.find((item) => item.id === manifest.id && item.version === manifest.templateVersion)
|
||||
recovered.push(candidate && candidate.source === 'installed'
|
||||
? { ...candidate, entryPath: recoveredEntryPath, previewPath: manifest.preview ? path.join(entryPath, manifest.preview) : undefined }
|
||||
: {
|
||||
id: manifest.id,
|
||||
version: manifest.templateVersion,
|
||||
interfaceVersion: manifest.interfaceVersion,
|
||||
source: 'installed',
|
||||
name: manifest.name,
|
||||
author: manifest.author.name,
|
||||
entryPath: recoveredEntryPath,
|
||||
...(manifest.preview ? { previewPath: path.join(entryPath, manifest.preview) } : {}),
|
||||
capture: manifest.capture,
|
||||
license: manifest.license
|
||||
})
|
||||
} catch { /* 忽略损坏的安装目录,保留其他模板 */ }
|
||||
}
|
||||
}
|
||||
await writeIndex(this.directory, { version: 1, templates: recovered })
|
||||
}
|
||||
}
|
||||
|
||||
export const reportTemplateService = new ReportTemplateService()
|
||||
@@ -0,0 +1,44 @@
|
||||
import { app, BrowserWindow, ipcMain } from 'electron'
|
||||
import path from 'node:path'
|
||||
|
||||
const userData = process.env.TRACEMEMO_TEMPLATE_TEST_USER_DATA
|
||||
if (!userData) throw new Error('TRACEMEMO_TEMPLATE_TEST_USER_DATA is required')
|
||||
app.setPath('userData', userData)
|
||||
app.setPath('logs', path.join(userData, 'logs'))
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
const { registerReportTemplateIpc } = await import('./report-template-ipc')
|
||||
const { reportTemplateService } = await import('./report-template-service')
|
||||
const {
|
||||
deleteGeneratedReport,
|
||||
listGeneratedReports,
|
||||
prepareGeneratedReportTemplateSwitch,
|
||||
saveGeneratedReport,
|
||||
updateGeneratedReportTemplate
|
||||
} = await import('./report-history-service')
|
||||
const { exportGroupReportSnapshot, extractGroupReportRenderSnapshot } = await import('./group-report-service')
|
||||
await reportTemplateService.recover()
|
||||
registerReportTemplateIpc()
|
||||
ipcMain.handle('report:listGenerated', () => listGeneratedReports())
|
||||
ipcMain.handle('report:saveGenerated', (_, request) => saveGeneratedReport(request))
|
||||
ipcMain.handle('report:updateGeneratedTemplate', (_, request) => updateGeneratedReportTemplate(request))
|
||||
ipcMain.handle('report:prepareTemplateSwitch', (_, request: { reportId: string }) =>
|
||||
prepareGeneratedReportTemplateSwitch(request.reportId, extractGroupReportRenderSnapshot)
|
||||
)
|
||||
ipcMain.handle('report:deleteGenerated', (_, reportId: string) => deleteGeneratedReport(reportId))
|
||||
ipcMain.handle('report:exportSnapshot', (_, request) => exportGroupReportSnapshot(request))
|
||||
const window = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 800,
|
||||
show: true,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, '../preload/index.js'),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
await window.loadFile(path.join(__dirname, '../renderer/index.html'))
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => app.quit())
|
||||
Reference in new issue
Block a user