feat: 增加模板版本独立预览入口

This commit is contained in:
que01 committed 2026-10-16 17:28:07 +08:00
1 parent 413413efda
commit 6edde5a8d6
15 files changed
+781 -14

No files matched your search

@@ -5,7 +5,6 @@ import {
PayloadTooLargeException,
} from "@nestjs/common";
import { validateTemplatePackageManifest } from "@personal-brand/config";
import { Prisma } from "@prisma/client";
import type { FastifyRequest } from "fastify";
import { createHash } from "node:crypto";
import { execFile } from "node:child_process";
@@ -35,6 +34,16 @@ type ChecksumFile = {
files: Array<{ path: string; bytes: number; sha256: string }>;
};
type JsonRecord = Record<string, unknown>;
function record(value: unknown): JsonRecord {
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : {};
}
function array(value: unknown): unknown[] {
return Array.isArray(value) ? value : [];
}
function sha256(value: Buffer | string): string {
return createHash("sha256").update(value).digest("hex");
}
@@ -137,6 +146,7 @@ export class AdminTemplatePackageService {
contentChecksum: true,
archiveChecksum: true,
validationReport: true,
manifestSnapshot: true,
importedAt: true,
updatedAt: true,
templatePackage: { select: { id: true } },
@@ -145,9 +155,21 @@ export class AdminTemplatePackageService {
}),
]);
return {
items: versions.map(({ templatePackage, ...version }) => ({
items: versions.map(({ templatePackage, manifestSnapshot, ...version }) => ({
...version,
templatePackageId: templatePackage.id,
manifestSummary: {
runtimeContract:
typeof record(manifestSnapshot).runtimeContract === "string"
? (record(manifestSnapshot).runtimeContract as string)
: "",
supportedIndustryTemplates: array(
record(manifestSnapshot).supportedIndustryTemplates,
).filter((item): item is string => typeof item === "string"),
requiredCapabilities: array(record(manifestSnapshot).requiredCapabilities).filter(
(item): item is string => typeof item === "string",
),
},
})),
page,
pageSize,
@@ -164,7 +186,7 @@ export class AdminTemplatePackageService {
let storedArtifactPath: string | null = null;
try {
for await (const chunk of part.file) {
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
const buffer = Buffer.from(chunk as Uint8Array);
bytes += buffer.length;
if (bytes > MAX_ARCHIVE_BYTES) throw new PayloadTooLargeException("模板包超过 50 MB 限制");
chunks.push(buffer);
@@ -272,13 +294,13 @@ export class AdminTemplatePackageService {
version: manifest.version,
displayName: manifest.displayName,
visualTemplateKey: manifest.visualTemplateKey ?? null,
manifestSnapshot: manifest as unknown as Prisma.InputJsonValue,
manifestSnapshot: manifest,
validationReport: {
formatVersion: "1",
errors: [],
fileCount: files.length,
extractedBytes: files.reduce((total, file) => total + file.bytes, 0),
} as Prisma.InputJsonValue,
},
contentChecksum: checksums.contentChecksum,
archiveChecksum,
artifactPath,