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