|
|
|
@ -35,7 +35,7 @@ export async function POST(request: NextRequest) { |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
// Validate directory exists
|
|
|
|
// Ensure project directory exists (supports saving into new subfolders)
|
|
|
|
try { |
|
|
|
const stats = await fs.stat(directoryPath); |
|
|
|
if (!stats.isDirectory()) { |
|
|
|
@ -48,13 +48,35 @@ export async function POST(request: NextRequest) { |
|
|
|
); |
|
|
|
} |
|
|
|
} catch (dirError) { |
|
|
|
logger.warn('file.error', 'Workflow save failed: directory does not exist', { |
|
|
|
directoryPath, |
|
|
|
}); |
|
|
|
return NextResponse.json( |
|
|
|
{ success: false, error: "Directory does not exist" }, |
|
|
|
{ status: 400 } |
|
|
|
); |
|
|
|
const err = dirError as NodeJS.ErrnoException; |
|
|
|
const isNotFound = |
|
|
|
err?.code === "ENOENT" || |
|
|
|
(typeof err?.message === "string" && |
|
|
|
(err.message.includes("ENOENT") || err.message.includes("no such file or directory"))); |
|
|
|
|
|
|
|
if (!isNotFound) { |
|
|
|
logger.warn('file.error', 'Workflow save failed: directory validation error', { |
|
|
|
directoryPath, |
|
|
|
error: dirError instanceof Error ? dirError.message : 'Unknown error', |
|
|
|
}); |
|
|
|
return NextResponse.json( |
|
|
|
{ success: false, error: "Directory validation failed" }, |
|
|
|
{ status: 400 } |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
await fs.mkdir(directoryPath, { recursive: true }); |
|
|
|
} catch (mkdirError) { |
|
|
|
logger.warn('file.error', 'Workflow save failed: could not create directory', { |
|
|
|
directoryPath, |
|
|
|
error: mkdirError instanceof Error ? mkdirError.message : 'Unknown error', |
|
|
|
}); |
|
|
|
return NextResponse.json( |
|
|
|
{ success: false, error: "Could not create directory" }, |
|
|
|
{ status: 400 } |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Auto-create subfolders for inputs and generations
|
|
|
|
|