Skip to content

50+ open-source generative AI apps you can clone, deploy, and monetize — image generators, video tools, virtual try-ons, AI SaaS templates, and platform integrations. One-click Vercel deploy on every template.

1.9 KB
Raw Blame
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth";
import config from "@/lib/config";

export async function POST(req) {
  try {
    const session = await getServerSession(authOptions);

    if (!session?.user) {
      return new NextResponse("Unauthorized", { status: 401 });
    }

    const formData = await req.formData();
    const file = formData.get("file");

    if (!file) {
      return new NextResponse("No file provided", { status: 400 });
    }

    const apiKey = config.ai.apiKey;
    console.log("ENV KEYS:", Object.keys(process.env).filter(k => k.includes("API") || k.includes("KEY") || k.includes("SECRET")));
    console.log("RESOLVED API KEY:", apiKey ? (apiKey.slice(0, 5) + '...') : 'undefined');
    if (!apiKey) {
      return new NextResponse("API Key not configured", { status: 500 });
    }

    console.log(`[UPLOAD_API] File details: name=${file.name}, size=${file.size}, type=${file.type}`);
    console.log(`[UPLOAD_API] Using API Key: ${apiKey ? (apiKey.slice(0, 5) + '...') : 'undefined'}`);

    // Prepare for MuAPI
    const muapiFormData = new FormData();
    muapiFormData.append("file", file);

    const response = await fetch(config.ai.uploadEndpoint, {
      method: "POST",
      headers: {
        "x-api-key": apiKey,
      },
      body: muapiFormData,
    });

    if (!response.ok) {
      const errorText = await response.text();
      console.error(`[UPLOAD_API] MuAPI returned error status ${response.status}: ${errorText}`);
      throw new Error(`MuAPI Upload Failed: ${response.status} ${errorText}`);
    }

    const data = await response.json();
    console.log(`[UPLOAD_API] MuAPI returned success data:`, data);
    return NextResponse.json(data);
  } catch (error) {
    console.error("[UPLOAD_ERROR_DETAILED]", error);
    return new NextResponse(error.message || "Internal Error", { status: 500 });
  }
}