|
import { NextRequest, NextResponse } from "next/server"; |
|
import prisma from "@/lib/prisma"; |
|
|
|
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY; |
|
const ELEVENLABS_API_URL = "https://api.elevenlabs.io/v1"; |
|
|
|
|
|
export async function GET() { |
|
try { |
|
console.log("Fetching voices from ElevenLabs..."); |
|
const response = await fetch(`${ELEVENLABS_API_URL}/voices`, { |
|
headers: { |
|
"xi-api-key": ELEVENLABS_API_KEY!, |
|
"Content-Type": "application/json", |
|
}, |
|
}); |
|
|
|
if (!response.ok) { |
|
console.error("ElevenLabs API error:", await response.text()); |
|
throw new Error("Erreur lors de la récupération des voix"); |
|
} |
|
|
|
const data = await response.json(); |
|
console.log("ElevenLabs response:", data); |
|
|
|
|
|
const clonedVoices = data.voices.filter( |
|
(voice: any) => voice.category === "cloned" |
|
); |
|
|
|
return NextResponse.json({ voices: clonedVoices }); |
|
} catch (error) { |
|
console.error("Erreur complète:", error); |
|
return NextResponse.json( |
|
{ error: "Erreur lors de la récupération des voix" }, |
|
{ status: 500 } |
|
); |
|
} |
|
} |
|
|
|
|
|
export async function DELETE(request: NextRequest) { |
|
try { |
|
const { voiceId } = await request.json(); |
|
|
|
if (!voiceId) { |
|
return NextResponse.json( |
|
{ error: "ID de voix manquant" }, |
|
{ status: 400 } |
|
); |
|
} |
|
|
|
console.log("Deleting voice:", voiceId); |
|
const response = await fetch(`${ELEVENLABS_API_URL}/voices/${voiceId}`, { |
|
method: "DELETE", |
|
headers: { |
|
"xi-api-key": ELEVENLABS_API_KEY!, |
|
"Content-Type": "application/json", |
|
}, |
|
}); |
|
|
|
if (!response.ok) { |
|
console.error("ElevenLabs delete error:", await response.text()); |
|
throw new Error("Erreur lors de la suppression de la voix"); |
|
} |
|
|
|
|
|
await prisma.user.updateMany({ |
|
where: { voiceId }, |
|
data: { voiceId: null }, |
|
}); |
|
|
|
return NextResponse.json({ success: true }); |
|
} catch (error) { |
|
console.error("Erreur complète:", error); |
|
return NextResponse.json( |
|
{ error: "Erreur lors de la suppression de la voix" }, |
|
{ status: 500 } |
|
); |
|
} |
|
} |
|
|