- Removed the `fields` parameter from the version upload form, as it is now extracted from the uploaded .docx file using the `extractContentControls` function. - Updated related types and interfaces to reflect the new structure of version fields. - Modified the OpenAPI specification to document the changes in the versioning process. - Adjusted tests to ensure compliance with the new behavior, including validation for the absence of the `fields` parameter. - Implemented a new function `extractDocumentSnapshot` to handle the extraction of content controls and document properties from .docx files. - Updated metadata and versioning services to align with the new extraction logic.
62 lines
No EOL
2.3 KiB
TypeScript
62 lines
No EOL
2.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { extractDocumentSnapshot } from "@/lib/lib-tmpl";
|
|
// export const runtime = "nodejs";
|
|
|
|
const DOCX_CONTENT_TYPE =
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
|
|
// cap เดียวกับ N4 (app/actions/files.ts) — route นี้เปิดกว้างให้ภายนอกยิงได้
|
|
// ต้องเช็คขนาดก่อนอ่านเข้าหน่วยความจำ กัน DoS (bodySizeLimit ครอบเฉพาะ Server Actions)
|
|
const MAX_UPLOAD_BYTES = 100 * 1024 * 1024; // 100MB
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const formData = await request.formData();
|
|
const file = formData.get("file");
|
|
const TEST_SECRET = String(formData.get("secret") ?? "");
|
|
const TEST_PUBLIC_KEY_HEX = String(formData.get("public_key") ?? "");
|
|
|
|
if (!(file instanceof File) ||
|
|
(file.type !== DOCX_CONTENT_TYPE && !file.name.toLowerCase().endsWith(".docx") )
|
|
) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Field "file" must be a .docx file.',
|
|
},
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
if (file.size > MAX_UPLOAD_BYTES) {
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Field "file" exceeds the 100MB size limit.',
|
|
},
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
// File → Buffer
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
|
|
// ชุดแกะข้อมูล `{cc, bookmark, properties, verify}` จาก lib-tmpl.ts — ตัวเดียวกับที่
|
|
// ใช้เก็บเป็น fields ของ version ตอน POST /api/documents/versions (2026-10-30) ·
|
|
// secret/public_key เป็นของผู้เรียกเพื่อ "ทดสอบ verify" เฉพาะฝั่งนี้
|
|
const returnData = extractDocumentSnapshot(buffer, {
|
|
secret: TEST_SECRET || undefined,
|
|
publicKeyHex: TEST_PUBLIC_KEY_HEX || undefined,
|
|
});
|
|
return NextResponse.json(returnData);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Internal Server Error",
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
} |