-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
163 lines (124 loc) · 4.72 KB
/
index.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
const express = require('express')
const path = require('path')
const handlebars = require('express-handlebars')
const request = require('request')
const bodyParser = require('body-parser')
const fs = require('fs')
const uuid = require('uuid/v4')
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json({limit: "100mb"}))
app.use(bodyParser.raw({limit: "100mb"}))
//setup endpoints
app.engine('.hbs', handlebars({
defaultLayout: 'main',
extname: '.hbs',
layoutsDir: path.join(__dirname, 'views/layouts')
}))
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname, 'views'))
var queued_tasks = []
var completed_tasks = []
var rawtexts = {}
var waiting_for_server = false
//***********************
//client-facing endpoints
//***********************
//main HTML of the site
app.get('/', (request, response) => {
response.render('home', {
url: request.url
})
})
//uploads an audio file though body["audio-base64"]. response["task-id"] is an identifier for the queued task.
//if body["raw-text"] exists, the base64 audio is ignored.
app.post('/uploadBlob', (request, response) => {
let task_id = uuid()
//use the raw text if it exists -- otherwise check for the recording
if (request.body["raw-text"] != undefined) {
rawtexts[task_id] = request.body["raw-text"]
console.log("received " + request.body["raw-text"])
} else {
let file_path = "recordings/" + task_id + ".wav"
var audio_base64 = request.body["audio-base64"]
fs.writeFile(file_path, audio_base64, 'base64', function(error) { console.log(error) })
}
queued_tasks.push(task_id)
console.log(queued_tasks)
response.send({'status': 'success', 'task-id': task_id})
})
//body["task-id"] should be an identifier from /uploadBlob.
//response["code"] is either unknown-task, waiting-for-server, or response-ready.
//if response-ready, then response["response"] will be something when i get to it
app.post('/pollForSiriResponse', (request, response) => {
var taskId = request.body["task-id"]
if (!completed_tasks.includes(taskId)) {
if (!queued_tasks.includes(taskId)) {
response.send({'status': 'failure', 'code': 'unknown-task'})
} else {
response.send({'status': 'failure', 'code': 'waiting-for-server'})
}
return
}
response.send({'status': 'success', 'code': 'response-ready'})
})
//server-facing endpoints
app.get('/reset', (request, response) => {
waiting_for_server = false
response.send({status: 'success'})
})
app.get('/recordingAvailable', (request, response) => {
console.log("recordings avaiable? "+ queued_tasks)
if (waiting_for_server) {
response.send("false")
return
}
if (queued_tasks.length == 0) {
response.send("false")
} else {
response.send(queued_tasks[0]) //send the first task id
waiting_for_server = true //don't send another recording until the server sends back a response
}
})
//if the next query has a rawtext, return that rawtext. Otherwise, "false".
app.get('/rawtext', (request, response) => {
if (queued_tasks.length == 0) {
response.send("false")
} else {
task_id = queued_tasks[0]
rawtext = rawtexts[task_id]
if (rawtext != undefined) {
response.send(rawtext)
} else {
response.send("false")
}
}
})
//request.body is {"task-id": ..., "siri-response": {"image": ..., "audio" ...}}
app.post('/deliverSiriResponse', (request, response) => {
body = request.body
task_id = body["task-id"]
siri_response = body["siri-response"]
if (task_id == undefined
|| siri_response == undefined
|| siri_response["image"] == undefined
|| siri_response["audio"] == undefined) {
response.send({status: 'failure'})
return
}
queued_tasks.shift() //remove the current task
//array.splice(queued_tasks, queued_tasks.indexOf(task_id)) //remove the task
waiting_for_server = false //allow the server to receive more recordings
//write files to disk
fs.writeFile("siri-responses/" + task_id + ".png", siri_response["image"], 'base64', function(err) {})
fs.writeFile("siri-responses/" + task_id + ".mp4", siri_response["audio"], 'base64', function(err) {})
completed_tasks.push(task_id)
response.send({status: 'success'})
})
//publicize server
function makeDirectoryPublic(name) {
app.use(express.static(__dirname + name));
app.use(name, express.static(__dirname + name));
}
['/assets', '/scripts', '/css', '/siri-responses', '/recordings'].forEach(makeDirectoryPublic)
app.listen(8081)