full upgrade to dev

This commit is contained in:
2026-01-08 16:27:40 +01:00
parent 41f404c581
commit cd4d2367ab
20 changed files with 2687 additions and 96 deletions

View File

@@ -39,37 +39,46 @@ export async function POST(req: NextRequest) {
);
}
// Fetch project data first (needed for the new webhook format)
const projectResponse = await fetch(
`${process.env.NEXT_PUBLIC_API_URL || "http://localhost:3000"}/api/projects/${projectId}`,
{
method: "GET",
cache: "no-store",
},
);
if (!projectResponse.ok) {
return NextResponse.json(
{ error: "Project not found" },
{ status: 404 },
);
}
const project = await projectResponse.json();
// Optional: Check if project already has an image
if (!regenerate) {
const checkResponse = await fetch(
`${process.env.NEXT_PUBLIC_API_URL || "http://localhost:3000"}/api/projects/${projectId}`,
{
method: "GET",
cache: "no-store",
},
);
if (checkResponse.ok) {
const project = await checkResponse.json();
if (project.imageUrl && project.imageUrl !== "") {
return NextResponse.json(
{
success: true,
message:
"Project already has an image. Use regenerate=true to force regeneration.",
projectId: projectId,
existingImageUrl: project.imageUrl,
regenerated: false,
},
{ status: 200 },
);
}
if (project.imageUrl && project.imageUrl !== "") {
return NextResponse.json(
{
success: true,
message:
"Project already has an image. Use regenerate=true to force regeneration.",
projectId: projectId,
existingImageUrl: project.imageUrl,
regenerated: false,
},
{ status: 200 },
);
}
}
// Call n8n webhook to trigger AI image generation
// New webhook expects: body.projectData with title, category, description
// Webhook path: /webhook/image-gen (instead of /webhook/ai-image-generation)
const n8nResponse = await fetch(
`${n8nWebhookUrl}/webhook/ai-image-generation`,
`${n8nWebhookUrl}/webhook/image-gen`,
{
method: "POST",
headers: {
@@ -80,6 +89,11 @@ export async function POST(req: NextRequest) {
},
body: JSON.stringify({
projectId: projectId,
projectData: {
title: project.title || "Unknown Project",
category: project.category || "Technology",
description: project.description || "A clean minimalist visualization",
},
regenerate: regenerate,
triggeredBy: "api",
timestamp: new Date().toISOString(),
@@ -101,16 +115,97 @@ export async function POST(req: NextRequest) {
);
}
const result = await n8nResponse.json();
// The new webhook should return JSON with the pollinations.ai image URL
// The pollinations.ai URL format is: https://image.pollinations.ai/prompt/...
// This URL is stable and can be used directly
const contentType = n8nResponse.headers.get("content-type");
let imageUrl: string;
let generatedAt: string;
let fileSize: string | undefined;
if (contentType?.includes("application/json")) {
const result = await n8nResponse.json();
// Handle JSON response - webhook should return the pollinations.ai URL
// The URL from pollinations.ai is the direct image URL
imageUrl = result.imageUrl || result.url || result.generatedPrompt || "";
// If the webhook returns the pollinations.ai URL directly, use it
// Format: https://image.pollinations.ai/prompt/...
if (!imageUrl && typeof result === 'string' && result.includes('pollinations.ai')) {
imageUrl = result;
}
generatedAt = result.generatedAt || new Date().toISOString();
fileSize = result.fileSize;
} else if (contentType?.startsWith("image/")) {
// If webhook returns image binary, we need the URL from the workflow
// For pollinations.ai, the URL should be constructed from the prompt
// But ideally the webhook should return JSON with the URL
return NextResponse.json(
{
error: "Webhook returned image binary instead of URL",
message: "Please modify the n8n workflow to return JSON with the imageUrl field containing the pollinations.ai URL",
},
{ status: 500 },
);
} else {
// Try to parse as text/URL
const textResponse = await n8nResponse.text();
if (textResponse.includes('pollinations.ai') || textResponse.startsWith('http')) {
imageUrl = textResponse.trim();
generatedAt = new Date().toISOString();
} else {
return NextResponse.json(
{
error: "Unexpected response format from webhook",
message: "Webhook should return JSON with imageUrl field containing the pollinations.ai URL",
},
{ status: 500 },
);
}
}
if (!imageUrl) {
return NextResponse.json(
{
error: "No image URL returned from webhook",
message: "The n8n workflow should return the pollinations.ai image URL in the response",
},
{ status: 500 },
);
}
// If we got an image URL, we should update the project with it
if (imageUrl) {
// Update project with the new image URL
const updateResponse = await fetch(
`${process.env.NEXT_PUBLIC_API_URL || "http://localhost:3000"}/api/projects/${projectId}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
"x-admin-request": "true",
},
body: JSON.stringify({
imageUrl: imageUrl,
}),
},
);
if (!updateResponse.ok) {
console.warn("Failed to update project with image URL");
}
}
return NextResponse.json(
{
success: true,
message: "AI image generation started successfully",
message: "AI image generation completed successfully",
projectId: projectId,
imageUrl: result.imageUrl,
generatedAt: result.generatedAt,
fileSize: result.fileSize,
imageUrl: imageUrl,
generatedAt: generatedAt,
fileSize: fileSize,
regenerated: regenerate,
},
{ status: 200 },