full pipeline
This commit is contained in:
@@ -13,32 +13,31 @@ export async function POST(req: NextRequest) {
|
||||
return NextResponse.json({ error: "No file provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
// 1. Setup workspace
|
||||
const tempDir = path.join(os.tmpdir(), "xgrids-pipeline");
|
||||
await mkdir(tempDir, { recursive: true });
|
||||
|
||||
// FIX: Sanitize filename to avoid shell/path issues with spaces
|
||||
const safeName = file.name.replace(/[^a-z0-8.]/gi, "_").toLowerCase();
|
||||
const safeName = file.name.replace(/[^a-z0-9.]/gi, "_").toLowerCase();
|
||||
const timestamp = Date.now();
|
||||
const inputPath = path.join(tempDir, `${timestamp}_${safeName}`);
|
||||
const outputPath = inputPath.replace(/\.(lcc|lci|bin)$/i, ".ply");
|
||||
|
||||
// Ensure we replace the extension correctly for the output
|
||||
const outputPath = inputPath.replace(/\.(lcc|lci)$/i, ".ply");
|
||||
// DETERMINE WHICH SCRIPT TO RUN
|
||||
let scriptName = "convert_lci_to_ply.py";
|
||||
if (file.name.toLowerCase().includes("environment.bin")) {
|
||||
scriptName = "convert_env_to_ply.py";
|
||||
}
|
||||
|
||||
const scriptPath = path.join(
|
||||
process.cwd(),
|
||||
"scripts",
|
||||
"preprocess",
|
||||
"convert_lci_to_ply.py",
|
||||
scriptName,
|
||||
);
|
||||
|
||||
// 2. Write the file
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
await writeFile(inputPath, buffer);
|
||||
|
||||
// 3. Execute Python
|
||||
return new Promise<NextResponse>((resolve) => {
|
||||
// spawn handles arguments as an array, which is safer than exec for spaces
|
||||
const pythonProcess = spawn("python3", [
|
||||
scriptPath,
|
||||
inputPath,
|
||||
@@ -52,14 +51,10 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
pythonProcess.on("close", async (code) => {
|
||||
if (code !== 0) {
|
||||
console.error("Python Error:", errorOutput);
|
||||
// Cleanup input even on failure
|
||||
await unlink(inputPath).catch(() => {});
|
||||
return resolve(
|
||||
NextResponse.json(
|
||||
{
|
||||
error: `Python script failed with code ${code}. ${errorOutput}`,
|
||||
},
|
||||
{ error: `Python failed (${scriptName}): ${errorOutput}` },
|
||||
{ status: 500 },
|
||||
),
|
||||
);
|
||||
@@ -67,8 +62,6 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
try {
|
||||
const plyBuffer = await readFile(outputPath);
|
||||
|
||||
// Cleanup
|
||||
await Promise.all([
|
||||
unlink(inputPath).catch(() => {}),
|
||||
unlink(outputPath).catch(() => {}),
|
||||
@@ -86,7 +79,7 @@ export async function POST(req: NextRequest) {
|
||||
} catch (e) {
|
||||
resolve(
|
||||
NextResponse.json(
|
||||
{ error: "Failed to read generated PLY file" },
|
||||
{ error: "Failed to read output PLY" },
|
||||
{ status: 500 },
|
||||
),
|
||||
);
|
||||
@@ -94,7 +87,6 @@ export async function POST(req: NextRequest) {
|
||||
});
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("API Route Error:", error);
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user