Commit 0fe99509 authored by Yoann HOUPERT's avatar Yoann HOUPERT
Browse files

Add test

Clean code
parent e4d6d634
Pipeline #6396 passed with stage
in 27 seconds
......@@ -2,14 +2,14 @@
"api": 3000,
"orchestrator": {
"gstreamer": {
"host": "linsttpoc_kaldi_1",
"host": "linsttcontroller_kaldi_1",
"port": "80",
"api" : {
"recognize" : "client/dynamic/recognize"
}
},
"speechEnhancement": {
"host": "linsttpoc_speech-enhencement_1",
"host": "linsttcontroller_speech-enhencement_1",
"port": "5000",
"api" : {
"upload" : "upload"
......
......@@ -17,31 +17,32 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const request = require('request');
const enhancement = (wavFile, speechEnhancement, enhancementlCallback) => {
const url = 'http://' + speechEnhancement.host + ':' + speechEnhancement.port + '/' + speechEnhancement.api.upload;
module.exports = config => {
const request = require('request');
const options = {
url,
formData: {
wavFile: {
value: wavFile.data,
options: {
filename: 'wavFile',
contentType: 'audio/x-wav'
}
},
msg: '-n'
},
encoding: null
};
return {
enhancement: (wavFile, enhancementlCallback) => {
const url = 'http://' + config.host + ':' + config.port + '/' + config.api.upload;
request.post(options, enhancementlCallback);
const options = {
url,
formData: {
wavFile: {
value: wavFile.data,
options: {
filename: 'wavFile',
contentType: 'audio/x-wav'
}
},
msg: '-n'
},
encoding: null
};
return wavFile;
};
request.post(options, enhancementlCallback);
module.exports = {
enhancement
return wavFile;
}
};
};
......@@ -18,13 +18,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const request = require('request');
const transcript = (audio, gstreamer, sttCallback) => {
const url = 'http://' + gstreamer.host + ':' + gstreamer.port + '/' + gstreamer.api.recognize;
return request.post({url, body: audio}, sttCallback);
};
module.exports = {
transcript
module.exports = config => {
const request = require('request');
return {
transcript: (audio, sttCallback) => {
const url = 'http://' + config.host + ':' + config.port + '/' + config.api.recognize;
return request.post({url, body: audio}, sttCallback);
}
};
};
......@@ -18,33 +18,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const isFileValid = audio => {
if (audio.mimetype !== 'audio/wave') {
if (audio === undefined) {
return false;
}
return true;
};
const cleanFile = nameFile => {
fs.unlink(nameFile + '.wav', err => {
if (err) {
throw err;
}
});
};
const writeFile = (nameFile, data) => {
fs.writeFile('enhancementFile.wav', data, err => {
if (err) {
throw err;
}
});
if (audio.mimetype === 'audio/wave' || audio.mimetype === 'audio/x-wav') {
return true;
}
return false;
};
module.exports = {
isFileValid,
cleanFile,
writeFile
isFileValid
};
/*
* Copyright (c) 2017 Linagora.
*
* This file is part of linstt-controller
* (see https://ci.linagora.com/linagora/lgs/labs/linstt-controller).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
describe('checker should if file is valid', () => {
beforeEach(() => {
/* eslint-disable import/no-unassigned-import */
global.checker = require('./checker.js');
/* eslint-enable */
});
it('should define checker', () => {
expect(global.checker).toBeDefined();
});
it('should give false when undefined', () => {
expect(global.checker.isFileValid()).toBe(false);
});
it('should give false when not an audio', () => {
expect(global.checker.isFileValid('random stuff')).toBe(false);
});
it('should be false when the audio is not a wav', () => {
const audio = {
mimetype: 'audio/webm',
data: 'randomByte'
};
expect(global.checker.isFileValid(audio)).toBe(false);
});
it('should be true when the audio is wav', () => {
const audioWav = {
mimetype: 'audio/wave',
data: 'randomByte'
};
const audioXwav = {
mimetype: 'audio/x-wav',
data: 'randomByte'
};
expect(global.checker.isFileValid(audioWav)).toBe(true);
expect(global.checker.isFileValid(audioXwav)).toBe(true);
});
});
......@@ -20,41 +20,42 @@
const routesFactory = config => {
/* eslint new-cap: ["error", { "capIsNew": false }] */
config.speechEnhancement.host = '172.17.0.2';
config.gstreamer.host = '172.22.0.3';
const routes = require('express').Router();
const enhancer = require('../speech-enhancement');
const stt = require('../speech-to-text');
const util = require('../util');
const enhancer = require('../controller/speech-enhancement')(config.speechEnhancement);
const stt = require('../controller/speech-to-text')(config.gstreamer);
const checker = require('../util/checker');
routes.post('/api/transcript', (req, res) => {
if (!req.files) {
return res.status(400).send('No files were uploaded.');
}
if (!util.isFileValid(req.files.wav)) {
if (!checker.isFileValid(req.files.wav)) {
return res.status(400).send('File is not valid.');
}
enhancer.enhancement(req.files.wav, config.speechEnhancement,
(err, httpResponse, body) => {
enhancer.enhancement(req.files.wav, (err, httpResponse, body) => {
if (err) {
return res.status(500).send('Error while enhancing audio.' + err);
}
stt.transcript(body, (err, httpResponse, body) => {
if (err) {
return res.status(500).send('Error while enhancing audio.' + err);
return res.status(500).send('Error while transcript the audio.' + err);
} else if (body.indexOf('No workers available') !== -1) {
return res.status(500).send('No worker available for the moment.');
}
stt.transcript(body, config.gstreamer,
(err, httpResponse, body) => {
if (err) {
return res.status(500).send('Error while transcript the audio.' + err);
} else if (body.indexOf('No workers available') !== -1) {
return res.status(500).send('No worker available for the moment.');
}
const json = JSON.parse(body);
if (json.status === 5) {
return res.status(500).send('Error while transcript the audio.');
}
return res.status(200).json({message: 'transcript done', transcript: json});
}
);
const json = JSON.parse(body);
if (json.status === 5) {
return res.status(500).send('Error while transcript the audio.');
}
return res.status(200).json({message: 'transcript done', transcript: json});
}
);
}
);
});
return routes;
......
......@@ -22,7 +22,10 @@
"sinon": "^4.0.2"
},
"xo": {
"space": true
"space": true,
"envs": [
"jest"
]
},
"repository": {
"type": "git",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment