Files
portfolio/app/api/n8n/generate-image/route.ts
2026-01-08 16:27:40 +01:00

275 lines
8.1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
/**
* POST /api/n8n/generate-image
*
* Triggers AI image generation for a project via n8n workflow
*
* Body:
* {
* projectId: number;
* regenerate?: boolean; // Force regenerate even if image exists
* }
*/
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const { projectId, regenerate = false } = body;
// Validate input
if (!projectId) {
return NextResponse.json(
{ error: "projectId is required" },
{ status: 400 },
);
}
// Check environment variables
const n8nWebhookUrl = process.env.N8N_WEBHOOK_URL;
const n8nSecretToken = process.env.N8N_SECRET_TOKEN;
if (!n8nWebhookUrl) {
return NextResponse.json(
{
error: "N8N_WEBHOOK_URL not configured",
message:
"AI image generation is not set up. Please configure n8n webhooks.",
},
{ status: 503 },
);
}
// 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) {
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/image-gen`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
...(n8nSecretToken && {
Authorization: `Bearer ${n8nSecretToken}`,
}),
},
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(),
}),
},
);
if (!n8nResponse.ok) {
const errorText = await n8nResponse.text();
console.error("n8n webhook error:", errorText);
return NextResponse.json(
{
error: "Failed to trigger image generation",
message: "n8n workflow failed to execute",
details: errorText,
},
{ status: 500 },
);
}
// 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 completed successfully",
projectId: projectId,
imageUrl: imageUrl,
generatedAt: generatedAt,
fileSize: fileSize,
regenerated: regenerate,
},
{ status: 200 },
);
} catch (error) {
console.error("Error in generate-image API:", error);
return NextResponse.json(
{
error: "Internal server error",
message: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 },
);
}
}
/**
* GET /api/n8n/generate-image?projectId=123
*
* Check the status of image generation for a project
*/
export async function GET(req: NextRequest) {
try {
const searchParams = req.nextUrl.searchParams;
const projectId = searchParams.get("projectId");
if (!projectId) {
return NextResponse.json(
{ error: "projectId query parameter is required" },
{ status: 400 },
);
}
// Fetch project to check image status
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();
return NextResponse.json({
projectId: parseInt(projectId),
title: project.title,
hasImage: !!project.imageUrl,
imageUrl: project.imageUrl || null,
updatedAt: project.updatedAt,
});
} catch (error) {
console.error("Error checking image status:", error);
return NextResponse.json(
{
error: "Internal server error",
message: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 },
);
}
}