Spaces:
Build error
Build error
File size: 3,061 Bytes
333c51a b059128 333c51a b059128 333c51a b059128 333c51a b059128 333c51a b059128 acbcb20 b059128 333c51a b059128 333c51a b059128 333c51a b059128 333c51a b059128 333c51a b059128 333c51a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import { NextRequest, NextResponse } from "next/server";
import ZAI from "z-ai-web-dev-sdk";
import { db } from "@/lib/db";
import fs from "fs/promises";
import path from "path";
const DOWNLOAD_DIR = path.join(process.cwd(), "download", "videos");
async function ensureDir() {
try {
await fs.mkdir(DOWNLOAD_DIR, { recursive: true });
} catch {}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const {
prompt,
optimizedPrompt,
platform = "general",
style = "cinematic",
duration = "short",
aspectRatio = "16:9",
saveToDb = true,
projectId,
} = body;
if (!prompt && !optimizedPrompt) {
return NextResponse.json(
{ success: false, error: "Se requiere un prompt" },
{ status: 400 }
);
}
await ensureDir();
const finalPrompt = optimizedPrompt || prompt;
// ✅ DB record
let contentRecord: any = null;
if (saveToDb) {
contentRecord = await db.content.create({
data: {
type: "video",
title: (prompt || "").substring(0, 50),
description: prompt,
prompt: prompt,
optimizedPrompt: finalPrompt,
platform,
status: "processing",
projectId: projectId || null,
metadata: JSON.stringify({ style, duration, aspectRatio }),
},
});
}
try {
const zai = await ZAI.create();
// ✅ SDK correcto (sin la S)
const response: any = await zai.video.generations.create({
prompt: finalPrompt,
});
// ✅ Extraer URL de forma tolerante
const videoUrl: string | null =
response?.video_url ||
response?.url ||
response?.data?.[0]?.url ||
null;
if (!videoUrl) {
throw new Error("No se recibió video del generador");
}
// ✅ actualizar DB
if (contentRecord) {
await db.content.update({
where: { id: contentRecord.id },
data: {
status: "completed",
filePath: videoUrl,
},
});
}
return NextResponse.json({
success: true,
video: {
id: contentRecord?.id,
url: videoUrl,
prompt: finalPrompt,
platform,
style,
duration,
aspectRatio,
},
});
} catch (genError) {
console.error("Error generando video:", genError);
if (contentRecord) {
await db.content.update({
where: { id: contentRecord.id },
data: {
status: "failed",
metadata: JSON.stringify({ error: String(genError) }),
},
});
}
return NextResponse.json(
{ success: false, error: "Error al generar video" },
{ status: 500 }
);
}
} catch (error) {
console.error("Error en generate video:", error);
return NextResponse.json(
{ success: false, error: "Error interno" },
{ status: 500 }
);
}
}
|