feat: add audios table and ElevenLabs TTS endpoint

- New audios table with voice params, S3 link, alignment JSON
- POST /api/audios/generate calls ElevenLabs with-timestamps, uploads to S3
- GET/PATCH/DELETE /api/audios endpoints
- Requires ELEVENLABS_API_KEY env var

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 13:05:34 +02:00
parent 2f0e08e264
commit 75f05f45f2
4 changed files with 197 additions and 0 deletions

137
src/routes/audios.js Normal file
View File

@@ -0,0 +1,137 @@
const router = require('express').Router();
const { v4: uuidv4 } = require('uuid');
const { query } = require('../db');
const { uploadFile, deleteFile, keyFromUrl } = require('../s3');
const ELEVENLABS_BASE = 'https://api.elevenlabs.io/v1';
const ALLOWED_STATUSES = ['generated', 'published', 'blocked'];
// GET /api/audios
router.get('/', async (req, res, next) => {
try {
const { status, voice_id, limit = 50, offset = 0 } = req.query;
const params = [Math.min(parseInt(limit), 500), parseInt(offset)];
const conditions = [];
if (status) { conditions.push(`status = $${params.length + 1}`); params.push(status); }
if (voice_id) { conditions.push(`voice_id = $${params.length + 1}`); params.push(voice_id); }
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
const result = await query(
`SELECT * FROM audios ${where} ORDER BY created_at DESC LIMIT $1 OFFSET $2`,
params
);
res.json(result.rows);
} catch (err) { next(err); }
});
// GET /api/audios/:id
router.get('/:id', async (req, res, next) => {
try {
const result = await query('SELECT * FROM audios WHERE id = $1', [req.params.id]);
if (!result.rows.length) return res.status(404).json({ error: 'Not found' });
res.json(result.rows[0]);
} catch (err) { next(err); }
});
// POST /api/audios/generate — ElevenLabs TTS → S3 → DB
router.post('/generate', async (req, res, next) => {
try {
const {
text,
voice_id,
model_id = 'eleven_multilingual_v2',
speed = 1.0,
stability = 0.5,
similarity_boost = 0.75,
style = 0.0,
} = req.body;
if (!text) return res.status(400).json({ error: 'text is required' });
if (!voice_id) return res.status(400).json({ error: 'voice_id is required' });
const apiKey = process.env.ELEVENLABS_API_KEY;
if (!apiKey) return res.status(500).json({ error: 'ELEVENLABS_API_KEY not configured' });
const elevenRes = await fetch(
`${ELEVENLABS_BASE}/text-to-speech/${voice_id}/with-timestamps`,
{
method: 'POST',
headers: {
'xi-api-key': apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify({
text,
model_id,
speed,
voice_settings: { stability, similarity_boost, style, use_speaker_boost: true },
}),
}
);
if (!elevenRes.ok) {
const err = await elevenRes.text();
return res.status(elevenRes.status).json({ error: 'ElevenLabs error', detail: err });
}
const { audio_base64, alignment } = await elevenRes.json();
const buffer = Buffer.from(audio_base64, 'base64');
const id = uuidv4();
const key = `audios/${id}/${uuidv4()}.mp3`;
const audio_link = await uploadFile(key, buffer, 'audio/mpeg');
const result = await query(
`INSERT INTO audios
(id, text, audio_link, alignment, voice_id, model_id, speed, stability, similarity_boost, style)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *`,
[id, text, audio_link, JSON.stringify(alignment), voice_id, model_id,
speed, stability, similarity_boost, style]
);
res.status(201).json(result.rows[0]);
} catch (err) { next(err); }
});
// PATCH /api/audios/:id — Status ändern
router.patch('/:id', async (req, res, next) => {
try {
const allowed = ['status', 'text', 'audio_link', 'alignment', 'voice_id', 'model_id',
'speed', 'stability', 'similarity_boost', 'style', 'published_at', 'blocked_at'];
const fields = Object.keys(req.body).filter(k => allowed.includes(k));
if (!fields.length) return res.status(400).json({ error: 'No valid fields provided' });
if (req.body.status && !ALLOWED_STATUSES.includes(req.body.status))
return res.status(400).json({ error: `status must be one of: ${ALLOWED_STATUSES.join(', ')}` });
if (req.body.status === 'published' && !req.body.published_at)
fields.push('published_at'), req.body.published_at = new Date().toISOString();
if (req.body.status === 'blocked' && !req.body.blocked_at)
fields.push('blocked_at'), req.body.blocked_at = new Date().toISOString();
const setClauses = fields.map((f, i) => `${f} = $${i + 1}`).join(', ');
const values = [...fields.map(f => req.body[f]), req.params.id];
const result = await query(
`UPDATE audios SET ${setClauses} WHERE id = $${fields.length + 1} RETURNING *`,
values
);
if (!result.rows.length) return res.status(404).json({ error: 'Not found' });
res.json(result.rows[0]);
} catch (err) { next(err); }
});
// DELETE /api/audios/:id — DB-Row + S3-Datei löschen
router.delete('/:id', async (req, res, next) => {
try {
const existing = await query('SELECT audio_link FROM audios WHERE id = $1', [req.params.id]);
if (!existing.rows.length) return res.status(404).json({ error: 'Not found' });
const key = keyFromUrl(existing.rows[0].audio_link);
if (key) await deleteFile(key).catch(() => {});
await query('DELETE FROM audios WHERE id = $1', [req.params.id]);
res.status(204).end();
} catch (err) { next(err); }
});
module.exports = router;