Skip to content

Commit

Permalink
Update TTS API for better client support (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Dec 1, 2023
1 parent 96e158f commit 16f285a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion core/api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports.load = function Routes(app, io, controllers, middlewares) {
middlewares.ttsRateLimit,
asyncMiddleware(controllers.ttsController.getTemporaryToken),
);
app.get('/tts/generate', asyncMiddleware(controllers.ttsController.generate));
app.get('/tts/:tts_token/generate.mp3', asyncMiddleware(controllers.ttsController.generate));

// user
app.post('/users/signup', middlewares.rateLimiter, asyncMiddleware(controllers.userController.signup));
Expand Down
18 changes: 6 additions & 12 deletions core/api/tts/tts.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@ const TTS_TOKEN_PREFIX = 'tts-token:';

module.exports = function TTSController(redisClient) {
/**
* @api {get} /tts/generate Generate a mp3 file from a text
* @api {get} /tts/:tts_token/generate.mp3 Return a mp3 file
* @apiName generate
* @apiGroup TTS
*
*
* @apiQuery {String} text The text to generate
* @apiQuery {String} token Temporary token to have access to
*
* @apiSuccessExample {binary} Success-Response:
* HTTP/1.1 200 OK
*/
async function generate(req, res, next) {
const instanceId = await redisClient.get(`${TTS_TOKEN_PREFIX}:${req.query.token}`);
if (!instanceId) {
const text = await redisClient.get(`${TTS_TOKEN_PREFIX}:${req.params.tts_token}`);
if (!text) {
throw new UnauthorizedError('Invalid TTS token.');
}
// Streaming response to client
const { data, headers } = await axios({
url: process.env.TEXT_TO_SPEECH_URL,
method: 'POST',
data: {
text: req.query.text,
text,
},
headers: {
authorization: `Bearer ${process.env.TEXT_TO_SPEECH_API_KEY}`,
Expand Down Expand Up @@ -57,12 +53,10 @@ module.exports = function TTSController(redisClient) {
*/
async function getTemporaryToken(req, res, next) {
const token = uuid.v4();
await redisClient.set(`${TTS_TOKEN_PREFIX}:${token}`, req.instance.id, {
await redisClient.set(`${TTS_TOKEN_PREFIX}:${token}`, req.body.text, {
EX: 5 * 60, // 5 minutes in seconds
});
const url = `${process.env.GLADYS_PLUS_BACKEND_URL}/tts/generate?token=${token}&text=${encodeURIComponent(
req.body.text,
)}`;
const url = `${process.env.GLADYS_PLUS_BACKEND_URL}/tts/${token}/generate.mp3`;
res.json({ token, url });
}

Expand Down
11 changes: 4 additions & 7 deletions test/core/api/tts/tts.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ describe('TTS API', () => {
.post('/tts/token')
.set('Accept', 'application/json')
.set('Authorization', configTest.jwtAccessTokenInstance)
.send({ text: 'Bonjour, je suis Gladys' })
.send({ text: 'bonjour' })
.expect('Content-Type', /json/)
.expect(200);
expect(response.body).to.have.property('token');
expect(response.body).to.have.property(
'url',
`http://test-api.com/tts/generate?token=${response.body.token}&text=Bonjour%2C%20je%20suis%20Gladys`,
);
expect(response.body).to.have.property('url', `http://test-api.com/tts/${response.body.token}/generate.mp3`);
const responseMp3File = await request(TEST_BACKEND_APP)
.get(`/tts/generate?token=${response.body.token}&text=bonjour`)
.get(`/tts/${response.body.token}/generate.mp3`)
.set('Accept', 'application/json')
.set('Authorization', configTest.jwtAccessTokenInstance)
.send()
Expand All @@ -58,7 +55,7 @@ describe('TTS API', () => {
});
it('should return 401', async () => {
const response = await request(TEST_BACKEND_APP)
.get(`/tts/generate?token=toto&text=bonjour`)
.get(`/tts/toto/generate.mp3`)
.set('Accept', 'application/json')
.set('Authorization', configTest.jwtAccessTokenInstance)
.send()
Expand Down

0 comments on commit 16f285a

Please sign in to comment.