Files
portfolio/app/api/n8n/generate-image/route.ts
denshooter 0349c686fa feat(auth): implement session token creation and verification for enhanced security
feat(api): require session authentication for admin routes and improve error handling

fix(api): streamline project image generation by fetching data directly from the database

fix(api): optimize project import/export functionality with session validation and improved error handling

fix(api): enhance analytics dashboard and email manager with session token for admin requests

fix(components): improve loading states and dynamic imports for better user experience

chore(security): update Content Security Policy to avoid unsafe-eval in production

chore(deps): update package.json scripts for consistent environment handling in linting and testing
2026-01-12 00:27:03 +01:00

272 lines
8.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
/**
* 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 {
// Rate limiting for n8n endpoints
const ip = req.headers.get('x-forwarded-for') || req.headers.get('x-real-ip') || 'unknown';
const { checkRateLimit } = await import('@/lib/auth');
if (!checkRateLimit(ip, 10, 60000)) { // 10 requests per minute
return NextResponse.json(
{ error: 'Rate limit exceeded. Please try again later.' },
{ status: 429 }
);
}
// Require admin authentication for n8n endpoints
const { requireAdminAuth } = await import('@/lib/auth');
const authError = requireAdminAuth(req);
if (authError) {
return authError;
}
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 },
);
}
const projectIdNum = typeof projectId === "string" ? parseInt(projectId, 10) : Number(projectId);
if (!Number.isFinite(projectIdNum)) {
return NextResponse.json({ error: "projectId must be a number" }, { status: 400 });
}
// Fetch project data directly (avoid HTTP self-calls)
const project = await prisma.project.findUnique({ where: { id: projectIdNum } });
if (!project) {
return NextResponse.json({ error: "Project not found" }, { status: 404 });
}
// 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: projectIdNum,
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: projectIdNum,
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) {
try {
await prisma.project.update({
where: { id: projectIdNum },
data: { imageUrl, updatedAt: new Date() },
});
} catch {
// Non-fatal: image URL can still be returned to caller
console.warn("Failed to update project with image URL");
}
}
return NextResponse.json(
{
success: true,
message: "AI image generation completed successfully",
projectId: projectIdNum,
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 },
);
}
const projectIdNum = parseInt(projectId, 10);
if (!Number.isFinite(projectIdNum)) {
return NextResponse.json({ error: "projectId must be a number" }, { status: 400 });
}
const project = await prisma.project.findUnique({ where: { id: projectIdNum } });
if (!project) {
return NextResponse.json({ error: "Project not found" }, { status: 404 });
}
return NextResponse.json({
projectId: projectIdNum,
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 },
);
}
}