-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
271 lines (210 loc) · 7.66 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const express = require('express');
const app = express();
var bodyParser = require('body-parser');
const watson = require('watson-developer-cloud');
var fs = require('fs');
var url = require('url');
var path = require('path');
var wavFileInfo = require('wav-file-info');
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false });
// app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Bootstrap application settings
require('./config/express')(app);
const stt = new watson.SpeechToTextV1({
// if left undefined, username and password to fall back to the SPEECH_TO_TEXT_USERNAME and
// SPEECH_TO_TEXT_PASSWORD environment properties, and then to VCAP_SERVICES (on Bluemix)
//username: '',
//password: ''
});
const authService = new watson.AuthorizationV1(stt.getCredentials());
var PythonShell = require('python-shell');
var FILENAME = "";
app.get('/', function(req, res) {
res.render('index', {
BLUEMIX_ANALYTICS: process.env.BLUEMIX_ANALYTICS,
});
});
app.get('/api/translation', function (req, res) {
console.warn("File name received: "+req.headers.filename);
//var tempPath = req.headers.preview;
//var tempPath = URL.createObjectURL(req.headers.preview);
//console.warn("File received: "+req.headers.files);
var options = {
// mode: 'text',
// pythonPath: 'path/to/python',
// pythonOptions: ['-u'],
// scriptPath: 'path/to/my/scripts',
args: [req.headers.filename]
//args: [tempPath]
};
PythonShell.run('./cloud-client/quickstart.py',options, function (err, transcript) {
if (err) throw err;
var temp = transcript[1].slice(12)
console.warn("Google Cloud: "+ temp);
var google = temp;
var options = {
// mode: 'text',
// pythonPath: 'path/to/python',
// pythonOptions: ['-u'],
// scriptPath: 'path/to/my/scripts',
args: ["9fd2a189-3d57-4c02-8a55-5f0159bff2cf","e50b56df-95b7-4fa1-9061-83a7a9bea372",req.headers.filename]
//args: ["9fd2a189-3d57-4c02-8a55-5f0159bff2cf","e50b56df-95b7-4fa1-9061-83a7a9bea372",tempPath]
};
//PythonShell.run('./examples/audio_client.py', options, function (err, transcript) {
PythonShell.run('./pullstring-python/examples/audio_client.py', options, function (err, transcript) {
if (err) throw err;
console.warn("pullstring: "+ transcript[1]);
var pullstring = transcript[1];
var data = {
google : google,
pullstring: pullstring
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data));
});
});
});
// Get token using your credentials
app.get('/api/token', function(req, res, next) {
authService.getToken(function(err, token) {
if (err) {
next(err);
} else {
res.send(token);
}
});
});
function superFunction(a) {
return FILENAME;
}
app.post('/uploadFile', function(request,response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
var isWin = !!process.platform.match(/^win/);
if (filename && filename.toString().indexOf(isWin ? '\\uploadFile' : '/uploadFile') != -1 && request.method.toLowerCase() == 'post') {
//var a = uploadFileFunction(request, response);
//console.warn("name: "+ );
//return uploadFileFunction(request, response);
return superFunction(uploadFileFunction(request, response));
}
fs.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('404 Not Found: ' + filename + '\n');
response.end();
return;
}
if (filename.indexOf('favicon.ico') !== -1) {
return;
}
if (fs.statSync(filename).isDirectory() && !isWin) {
filename += '/index.html';
} else if (fs.statSync(filename).isDirectory() && !!isWin) {
filename += '\\index.html';
}
fs.readFile(filename, 'binary', function(err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.write(err + '\n');
response.end();
return;
}
var contentType;
if (filename.indexOf('.html') !== -1) {
contentType = 'text/html';
}
if (filename.indexOf('.js') !== -1) {
contentType = 'application/javascript';
}
if (contentType) {
response.writeHead(200, {
'Content-Type': contentType
});
} else response.writeHead(200);
response.write(file, 'binary');
response.end();
});
});
})
function uploadFileFunction(request, response) {
// parse a file upload
var mime = require('mime');
var formidable = require('formidable');
var util = require('util');
var form = new formidable.IncomingForm();
var dir = !!process.platform.match(/^win/) ? '\\public\\audio\\' : '/public/audio/';
form.uploadDir = __dirname + dir;
form.keepExtensions = true;
form.maxFieldsSize = 10 * 1024 * 1024;
form.maxFields = 1000;
form.multiples = false;
return form.parse(request, function(err, fields, files) {
var file = util.inspect(files);
response.writeHead(200, getHeaders('Content-Type', 'application/json'));
var fileName = file.split('path:')[1].split('\',')[0].split(dir)[1].toString().replace(/\\/g, '').replace(/\//g, '');
FILENAME = fileName;
var port = process.env.PORT || process.env.VCAP_APP_PORT || 3000;
appAddress = process.env.IP || "0.0.0.0";
if (appAddress == '0.0.0.0') {
appAddress = 'localhost';
}
var fileURL = 'http://' + appAddress + ':' + port + '/public/audio/' + fileName;
console.log('fileURL: ', fileURL);
var path = './public/audio/' + fileName;
wavFileInfo.infoByFilename(path, function(err, info){
if (err) {
response.write(JSON.stringify({
fileURL: fileURL,
fileName: fileName
}));
response.end();
} else {
response.write(JSON.stringify({
fileURL: fileURL,
fileName: fileName,
info: info
}));
response.end();
}
});
});
}
function getHeaders(opt, val) {
try {
var headers = {};
headers["Access-Control-Allow-Origin"] = "https://secure.seedocnow.com";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = true;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
if (opt) {
headers[opt] = val;
}
return headers;
} catch (e) {
return {};
}
}
module.exports = app;