feat: 收口模板升级与阶段七持续回归

This commit is contained in:
que01 committed 2026-10-17 21:33:57 +08:00
1 parent ec16fff70e
commit 71c528de34
29 files changed
+1962 -304

No files matched your search

@@ -14,6 +14,7 @@ import { promises as fs } from "node:fs";
import path from "node:path";
import { promisify } from "node:util";
import { PrismaService } from "../../prisma.service.js";
import { recordTemplateOperationFailure } from "./template-operation-failure.js";
const execFileAsync = promisify(execFile);
const MAX_ARCHIVE_BYTES = 50 * 1024 * 1024;
@@ -181,13 +182,15 @@ export class AdminTemplatePackageService {
};
}
async importArchive(request: FastifyRequest, actorId: string) {
async importArchive(request: FastifyRequest, actorId: string, requestId?: string) {
const part = await request.file({ limits: { files: 1, fileSize: MAX_ARCHIVE_BYTES } });
if (!part) throw new BadRequestException("请上传模板 archive 文件");
if (part.fieldname !== "archive") throw new BadRequestException("上传字段必须是 archive");
const chunks: Buffer[] = [];
let bytes = 0;
let storedArtifactPath: string | null = null;
let failurePackageKey: string | undefined;
let failureVersion: string | undefined;
try {
for await (const chunk of part.file) {
const buffer = Buffer.from(chunk as Uint8Array);
@@ -242,6 +245,8 @@ export class AdminTemplatePackageService {
path.join(packageRoot, "template.json"),
"template.json",
)) as Manifest;
failurePackageKey = typeof manifest.packageKey === "string" ? manifest.packageKey : undefined;
failureVersion = typeof manifest.version === "string" ? manifest.version : undefined;
const packageJson = jsonObject(
await this.readJson(path.join(packageRoot, "package.json"), "package.json"),
"package.json",
@@ -304,6 +309,7 @@ export class AdminTemplatePackageService {
errors: [],
fileCount: files.length,
extractedBytes: files.reduce((total, file) => total + file.bytes, 0),
files: checksums.files.map((file) => ({ ...file })),
},
contentChecksum: checksums.contentChecksum,
archiveChecksum,
@@ -328,6 +334,14 @@ export class AdminTemplatePackageService {
return { imported: true, idempotent: false, version: this.publicVersion(version) };
} catch (error) {
if (storedArtifactPath) await fs.rm(storedArtifactPath, { force: true });
await recordTemplateOperationFailure(this.prisma, {
action: "import",
error,
...(requestId ? { requestId } : {}),
actorId,
...(failurePackageKey ? { packageKey: failurePackageKey } : {}),
...(failureVersion ? { version: failureVersion } : {}),
});
throw error;
} finally {
await fs.rm(temporaryRoot, { recursive: true, force: true });