feat: 增加模板预览短期隔离上下文
This commit is contained in:
1 parent
b60174885e
commit
d38dbe02aa
8 files changed
+190
-22
No files matched your search
@@ -59,6 +59,7 @@ export class AdminTemplatePackageController {
|
||||
query.industry,
|
||||
subject,
|
||||
request.headers["x-request-id"]?.toString() ?? request.id,
|
||||
query.session,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Type } from "class-transformer";
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString, Max, Min } from "class-validator";
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString, Matches, Max, Min } from "class-validator";
|
||||
|
||||
export class ListTemplatePackagesQueryDto {
|
||||
@IsOptional()
|
||||
@@ -20,4 +20,10 @@ export class PreviewTemplatePackageQueryDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
industry!: string;
|
||||
|
||||
// 短期 session token 是复用隔离预览数据的 capability,不是全局可重放凭据。
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@Matches("^[a-f0-9]{64}$")
|
||||
session?: string;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { BadRequestException, ConflictException, Injectable } from "@nestjs/common";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import {
|
||||
capabilityRegistry,
|
||||
getBrandTemplate,
|
||||
@@ -74,7 +75,95 @@ export class AdminTemplatePreviewService {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly assets: AdminTemplatePreviewAssetsService,
|
||||
) {}
|
||||
async preview(id: string, industry: string, actorId: string, requestId?: string) {
|
||||
private readonly previewSessions = new Map<
|
||||
string,
|
||||
{
|
||||
versionId: string;
|
||||
industry: string;
|
||||
actorId: string;
|
||||
expiresAt: number;
|
||||
response: Awaited<ReturnType<AdminTemplatePreviewService["buildPreview"]>>;
|
||||
}
|
||||
>();
|
||||
|
||||
async preview(
|
||||
id: string,
|
||||
industry: string,
|
||||
actorId: string,
|
||||
requestId?: string,
|
||||
sessionToken?: string,
|
||||
) {
|
||||
if (sessionToken) {
|
||||
const session = this.previewSessions.get(sessionToken);
|
||||
if (!session || session.expiresAt <= Date.now()) {
|
||||
this.previewSessions.delete(sessionToken);
|
||||
throw new BadRequestException("预览 session 不存在或已过期,请重新创建预览");
|
||||
}
|
||||
if (
|
||||
session.actorId !== actorId ||
|
||||
session.versionId !== id ||
|
||||
session.industry !== industry
|
||||
) {
|
||||
throw new BadRequestException("预览 session 与版本、行业或管理员不匹配");
|
||||
}
|
||||
return {
|
||||
...session.response,
|
||||
preview: {
|
||||
...session.response.preview,
|
||||
session: {
|
||||
token: sessionToken,
|
||||
expiresAt: new Date(session.expiresAt).toISOString(),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const projection = await this.buildPreview(id, industry);
|
||||
this.cleanupPreviewSessions();
|
||||
const token = randomBytes(32).toString("hex");
|
||||
const expiresAt = Date.now() + 15 * 60 * 1000;
|
||||
this.previewSessions.set(token, {
|
||||
versionId: projection.template.id,
|
||||
industry,
|
||||
actorId,
|
||||
expiresAt,
|
||||
response: projection,
|
||||
});
|
||||
while (this.previewSessions.size > 20) {
|
||||
const oldest = this.previewSessions.keys().next().value;
|
||||
if (!oldest) break;
|
||||
this.previewSessions.delete(oldest);
|
||||
}
|
||||
await this.prisma.templatePackageAuditLog.create({
|
||||
data: {
|
||||
templatePackageVersionId: projection.template.id,
|
||||
actorId,
|
||||
action: "previewed",
|
||||
...(requestId ? { requestId } : {}),
|
||||
metadata: {
|
||||
industry,
|
||||
contentChecksum: projection.template.contentChecksum,
|
||||
fixtureSource: projection.preview.fixtureSource,
|
||||
},
|
||||
},
|
||||
});
|
||||
return {
|
||||
...projection,
|
||||
preview: {
|
||||
...projection.preview,
|
||||
session: { token, expiresAt: new Date(expiresAt).toISOString() },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private cleanupPreviewSessions() {
|
||||
const now = Date.now();
|
||||
for (const [token, session] of this.previewSessions) {
|
||||
if (session.expiresAt <= now) this.previewSessions.delete(token);
|
||||
}
|
||||
}
|
||||
|
||||
private async buildPreview(id: string, industry: string) {
|
||||
const version = await this.prisma.templatePackageVersion.findUnique({ where: { id } });
|
||||
if (!version) throw new BadRequestException("模板版本不存在");
|
||||
if (version.status === "retired") throw new ConflictException("已撤回的模板版本不能创建新预览");
|
||||
@@ -199,19 +288,6 @@ export class AdminTemplatePreviewService {
|
||||
sample!,
|
||||
fixture?.brand,
|
||||
);
|
||||
await this.prisma.templatePackageAuditLog.create({
|
||||
data: {
|
||||
templatePackageVersionId: version.id,
|
||||
actorId,
|
||||
action: "previewed",
|
||||
...(requestId ? { requestId } : {}),
|
||||
metadata: {
|
||||
industry,
|
||||
contentChecksum: version.contentChecksum,
|
||||
fixtureSource: fixtureDeclared ? "previewFixture" : "legacy-sampleContent",
|
||||
},
|
||||
},
|
||||
});
|
||||
return {
|
||||
template: {
|
||||
id: version.id,
|
||||
|
||||
Reference in new issue
Block a user