forked from iLanguage/OPrime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
168 lines (148 loc) · 5.46 KB
/
server.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
var fs = require('fs'),
exec = require('child_process').exec,
sys = require('sys'),
path = require('path'),
http = require('http'),
formidable = require('./lib/formidable'),
paperboy = require('./lib/paperboy');
var PUBLIC = path.join(path.dirname(__filename), 'public');
var devmode = false;
var port = 8136;
if (devmode){
port = 8134;
}
var statuses = {};
var progresses = {};
var metadata = {};
var child;
function puts(error, stdout, stderr) { sys.puts(stdout) };
function logsAndFlagsFresh(error,uuid,stdout,stderr){
sys.puts(stdout);
var setFresh = function(){
statuses[uuid]="transcription fresh";
sys.print("\nProcessed uuid: "+uuid+" set to: "+statuses[uuid]+"\n\n");
}
return setFresh;
};
http.createServer(function(req, res) {
/*TODO check for API key and non banned install id in this regex */
regex = new RegExp('/upload/(.+)');
match = regex.exec(req.url);
if (match && req.method.toLowerCase() == 'post') {
var uuid = match[1];
uuid = uuid.replace(/.3gp/,"");
uuid = uuid.replace(/.mp3/,"");
uuid = uuid.replace(/.srt/,"");
uuid = uuid.replace(/_client/,"");
uuid = uuid.replace(/_server/,"");
sys.print("Receiving transcription request: "+uuid+'\n');
var form = new formidable.IncomingForm();
form.uploadDir = './data';
form.keepExtensions = true;
// keep track of progress.
form.addListener('progress', function(recvd, expected) {
progress = (recvd / expected * 100).toFixed(2);
progresses[uuid] = progress;
});
form.parse(req, function(error, fields, files) {
var path = files['file']['path'],
filename = files['file']['filename'],
mime = files['file']['mime'];
sys.print('Users file: '+filename + ':filename\nIs server file: ' + path + ':path\n');
/*
* Rename to original name (sanitize, although it shoudl already be sanitized by the android client.)
*/
var safeFilename=filename.replace(/[^\w\.]/g,"_");
safeFilename=safeFilename.replace(/[;:|@&*/\\]/g,"_");
//safeFilename=safeFilename.replace(/_client\./,".");
safeFilename=safeFilename.replace(/\.mp3/,".amr");
var tempdir = "../backup/";
fs.renameSync(path,tempdir+safeFilename);
safeFilenameServer = safeFilename.replace(/_client/,"_server");
var videoregex = new RegExp('(.+).3gp');
var matchvideo = videoregex.exec(filename);
res.writeHead(200, {'content-type': 'text/html'});
if(matchvideo){
res.write("Server is Processing.\n");
res.write(filename + ':filename\n' + path + ':path\n');
//TODO run versioning on all uploaded files
exec("bash audio2text.sh "+ safeFilename.replace(/\.3gp/,""),puts);
}else{
res.write("File uploaded.");
}
res.end();
exec("date",puts);
sys.print("\tFinished upload processing."+'\n');
});
return;
}
// (update) metadata
regex = new RegExp('/update/(.+)');
match = regex.exec(req.url);
if (match && req.method.toLowerCase() == 'post') {
uuid = match[1];
var form = new formidable.IncomingForm();
form.addListener('field', function(name, value) {
sys.print("fresh metadata for "+uuid+": "+name+" => "+value+"\n")
metadata[name] = value;
});
form.parse(req);
}
// respond to status queries
regex = new RegExp('/extract/(.+)');
match = regex.exec(req.url);
if (match) {
uuid = match[1];
uuid = uuid.replace(/.mp3/,"");
res.writeHead(200, {'content-type': 'text/html'});
res.write("<html>");
regex = new RegExp('/extract/touch');
match = regex.exec(req.url);
if(match){
res.write("Extracting touch results from the subtitles....<p> </p>The data is now integrated in the <a href='/touch_response_visualizer.html'>touch response visualizer</a>");
exec("bash ../backup/srt2touchdatadir.sh ",puts);
}else{
res.write("Extracting textgrids using Praat. This may take a while....");
exec("bash praatfiles/audio2textGrid.sh ",puts);
}
res.write("<p>The raw results are in the <a href='file:///Applications/OPrimeAdministrator.app/Contents/Resources/oprime-server/results'>/Applications/OPrimeAdministrator.app/Contents/Resources/oprime-server/results</a> ");
res.end();
}
// respond to status queries
regex = new RegExp('/status/(.+)');
match = regex.exec(req.url);
if (match) {
uuid = match[1];
uuid = uuid.replace(/.mp3/,"");
uuid = uuid.replace(/.srt/,"");
uuid = uuid.replace(/_client/,"");
uuid = uuid.replace(/_server/,"");
res.writeHead(200, {'content-type': 'application/json'});
res.write(JSON.stringify({'status': statuses[uuid]}));
res.end();
exec("date",puts);
sys.print(uuid+"\nReplied to status request: "+JSON.stringify({'status': statuses[uuid]}));
sys.print("\n\n");
}
// respond to progress queries.
regex = new RegExp('/progress/(.+)');
match = regex.exec(req.url);
if (match) {
uuid = match[1];
res.writeHead(200, {'content-type': 'application/json'});
res.write(JSON.stringify({'progress': progresses[uuid]}));
res.end();
}
// let paperboy handle any static content.
paperboy
.deliver(PUBLIC, req, res)
.after(function(statCode) {
sys.log('Served Request: ' + statCode + ' ' + req.url)
})
.otherwise(function() {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('Not Found');
res.end();
});
}).listen(port);
sys.log('ready at http://localhost:'+port+'/')