-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
82 lines (70 loc) · 2.44 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
const express = require('express');
const redis = require('redis');
const fs = require('fs');
const bodyParser = require('body-parser');
const cors = require('cors');
var jsonParser = bodyParser.json()
var webmParser = bodyParser.raw({type: "audio/webm", limit: '50mb'})
var rawAudioParser = bodyParser.raw({type: "audio/*", limit: '50mb'})
const redisClient = redis.createClient();
redisClient.on('error', err => console.log('Redis Client Error', err));
const app = express()
const port = 3001
app.use(express.static(__dirname + '/public'));
app.use(cors({
origin: 'http://localhost:3000'
}));
app.get('/', async (req, res) => {
// const samples = await fs.promises.readdir(process.cwd() + '/../audiogen/samples');
res.sendFile('index.html', { root: __dirname });
})
app.get('/getsamplenames', async (req, res) => {
const samples = await fs.promises.readdir(process.cwd() + '/../audiogen/samples');
res.send(samples);
})
app.get('/getsample/:sample', async (req, res) => {
const sample = await fs.promises.readFile(process.cwd() + '/../audiogen/samples/' + req.params.sample);
res.send(sample);
})
app.post('/generateaudio', jsonParser, async (req, res) => {
await redisClient.connect();
const sample = req.body.sample;
if(!sample) {
res.status(400).send('No sample provided');
} else {
await redisClient.set(sample, "audiogen");
res.status(200).send('Enqueued task to generate audio')
}
await redisClient.disconnect();
})
app.post('/saverecording', webmParser, async (req, res) => {
const recording = req.body;
// console.log(recording);
filePath = process.cwd() + '/../audiogen/samples/recording.webm';
var i = 1;
while(fs.existsSync(filePath)) {
filePath = process.cwd() + '/../audiogen/samples/recording' + i.toString() + '.webm';
i++;
}
await fs.promises.writeFile(filePath, recording);
res.status(200).send('Recording saved')
});
app.post('/uploadsample', rawAudioParser, async (req, res) => {
const file = req.body;
console.log(file);
if(file.length > 0) {
filePath = process.cwd() + '/../audiogen/samples/sample.webm';
var i = 1;
while(fs.existsSync(filePath)) {
filePath = process.cwd() + '/../audiogen/samples/sample' + i.toString() + '.webm';
i++;
}
await fs.promises.writeFile(filePath, file);
res.status(200).send('Uploaded sample')
} else {
console.log('No file provided');
}
})
app.listen(port, () => {
console.log(`jamjam listening on port ${port}`)
})