-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
171 lines (152 loc) · 4.56 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
164
165
166
167
168
169
170
171
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + '.' + file.originalname.split('.').pop())
}
})
const upload = multer({ storage: storage })
const PORT = process.env.PORT || 4000;
const { processAudio, speedAudio } = require('./processAudio');
const utils = require('./utils');
const babbbellabs = require('./babblelabs');
// Listen on rabbitmq
// require('./worker').init();
// Create necessary file dirs
const APP_DIRS = ['./tmp'];
APP_DIRS.forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
})
app.all('/*', (req, res, next) => {
// CORS headers - Set custom headers for CORS
res.header('Access-Control-Allow-Origin', '*'); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS,PATCH');
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token, X-Vw-Anonymous-Id, X-Key, Cache-Control, X-Requested-With');
if (req.method === 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
app.use(bodyParser())
app.use(morgan('dev'));
const amqp = require("amqplib/callback_api");
amqp.connect(process.env.RABBITMQ_SERVER, (err, conn) => {
if (err) {
console.log("error is", err);
}
conn.createChannel((err, ch) => {
if (err) {
console.log("Error creating conection ", err);
return process.exit(1);
}
console.log("rabbitmq connection created");
channel = ch;
channel.on("error", (err) => {
console.log("RABBITMQ ERROR", err);
process.exit(1);
});
channel.on("close", () => {
console.log("RABBITMQ CLOSE");
process.exit(1);
});
require('./rabbitmqHandler').init(channel)
});
});
app.get('/health', (req, res) => {
babbbellabs.isTokenValid()
.then(() => {
return res.status(200).send('OK')
})
.catch(err => {
console.log(err);
return res.status(503).send('BABBELLABS DOWN')
})
})
app.get('/', (req, res) => {
return res.status(200).send('Audio Processor api root');
})
app.post('/process_audio', upload.any(), (req, res) => {
const { url, outputFormat } = req.body;
const { files } = req;
let fetchFile;
console.log(files);
if (url) {
fetchFile = utils.getRemoteFile(url);
} else if (files) {
const file = files[0];
fetchFile = new Promise((resolve) => {
resolve(file.path);
})
} else {
return res.status(400).send('Please upload a file or provide a url');
}
fetchFile.then(filePath => {
processAudio({ filePath, outputFormat }, (err, outputPath) => {
if (err) {
console.log('Error processing audio', err);
return res.status(400).send('Something went wrong');
}
return res.sendFile(path.join(__dirname, outputPath), (err) => {
if (err) {
console.log('error sending back file', err);
}
fs.unlink(filePath, () => { });
fs.unlink(outputPath, () => { });
});
})
})
.catch(err => {
console.log(err);
return res.status(400).send('Something went wrong');
})
})
app.post('/audioSpeed', upload.any(), (req, res) => {
const { url, speed } = req.body;
const { files } = req;
let fetchFile;
console.log(files);
if (url) {
fetchFile = utils.getRemoteFile(url);
} else if (files) {
const file = files[0];
fetchFile = new Promise((resolve) => {
resolve(file.path);
})
} else {
return res.status(400).send('Please upload a file or provide a url');
}
fetchFile.then(filePath => {
speedAudio(filePath, speed, (err, outputPath) => {
if (err) {
console.log('Error speeding audio audio', err);
return res.status(400).send('Something went wrong');
}
return res.sendFile(path.join(__dirname, outputPath), (err) => {
if (err) {
console.log('error sending back file', err);
}
fs.unlink(filePath, () => { });
fs.unlink(outputPath, () => { });
});
})
})
.catch(err => {
console.log(err);
return res.status(400).send('Something went wrong');
})
})
app.listen(PORT)
console.log(`Magic happens on port ${PORT}`) // shoutout to the user
console.log(`==== Running in ${process.env.NODE_ENV} mode ===`)
module.exports = app // expose app