GoGma commited on
Commit
b059128
·
verified ·
1 Parent(s): d1ea4f6

Update src/app/api/generate/video/route.ts

Browse files
Files changed (1) hide show
  1. 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 VIDEO_CENSOR_RULES: Record<string, string> = {
6
- youtube: "family friendly, no violence, no nudity",
7
- tiktok: "age appropriate, no dangerous content",
8
- instagram: "community guidelines compliant",
9
- general: "safe for work"
10
- };
 
11
 
12
  export async function POST(request: NextRequest) {
13
  try {
14
  const body = await request.json();
15
- const { prompt, optimizedPrompt, platform = "general", style = "cinematic" } = body;
 
 
 
 
 
 
 
 
 
16
 
17
  if (!prompt && !optimizedPrompt) {
18
- return NextResponse.json({ success: false, error: "Se requiere un prompt" }, { status: 400 });
 
 
 
19
  }
20
 
21
- let finalPrompt = optimizedPrompt || prompt;
22
- const censorRule = VIDEO_CENSOR_RULES[platform] || VIDEO_CENSOR_RULES.general;
23
- finalPrompt = finalPrompt + ", " + censorRule + ", short clip";
24
 
25
- const contentRecord = await db.content.create({
26
- data: {
27
- type: "video",
28
- title: prompt.substring(0, 50),
29
- description: prompt,
30
- prompt: prompt,
31
- optimizedPrompt: finalPrompt,
32
- platform: platform,
33
- status: "processing"
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
- await db.content.update({
44
- where: { id: contentRecord.id },
45
- data: { status: "completed", filePath: "generated" }
46
  });
47
 
48
- await db.agentTask.create({
49
- data: { type: "generate_video", status: "completed", input: prompt, output: "Video generado", completedAt: new Date() }
50
- });
 
 
 
 
 
 
 
51
 
52
- return NextResponse.json({ success: true, video: { id: contentRecord.id, prompt: finalPrompt, platform, style } });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  } catch (genError) {
54
- await db.content.update({ where: { id: contentRecord.id }, data: { status: "failed" } });
55
- return NextResponse.json({ success: false, error: String(genError) }, { status: 500 });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  }
57
  } catch (error) {
58
- return NextResponse.json({ success: false, error: "Error interno" }, { status: 500 });
 
 
 
 
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
  }