forked from TER-M1/wam-openstudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (72 loc) · 2.28 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const PORT = 5002;
const TRACK_PATH = './public/song/multitrack';
const LOCAL_TRACK_PATH = './song/multitrack';
let tracks = {};
const app = express();
app.use(express.static('public'));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
next();
});
app.listen(PORT, () => console.log(`Server is started and listening on port ${PORT}.`));
/*
ROUTING
*/
app.get('/track', async (req, res) => {
console.log('get track');
res.writeHead(200, {"Content-Type": "application/json"});
exploreMultiTracks();
res.write(JSON.stringify(tracks));
res.end();
});
app.get('/track/:id', async (req, res) => {
console.log('get track by id');
const id = req.params.id;
res.writeHead(200, {"Content-Type": "application/json"});
res.write(JSON.stringify(getMutliTrackById(id)));
res.end();
} )
const endsWith = (str, suffix) => str.indexOf(suffix, str.length - suffix.length) !== -1;
function isASoundFile(fileName) {
if (endsWith(fileName, ".mp3")) return true;
if (endsWith(fileName, ".ogg")) return true;
if (endsWith(fileName, ".wav")) return true;
return endsWith(fileName, ".m4a");
}
function exploreMultiTracks() {
tracks = {"tracks": []};
const directoryPath = TRACK_PATH;
//passsing directoryPath and callback function
var files = fs.readdirSync(directoryPath)
let i = 0;
files.forEach((file) => {
var soundList = fs.readdirSync(`${directoryPath}/${file}`)
const track = {
id: i,
trackname: file,
path: `${LOCAL_TRACK_PATH}/${file}`,
soundList: soundList
.filter(sound => isASoundFile(sound))
.map(sound => ({
name: sound
}))
}
tracks.tracks.push(track);
i++;
})
}
function getMutliTrackById(id) {
let res = {};
tracks.tracks.forEach(track => {
if (String(track.id) === id) {
res = track;
}
})
return res;
}
exploreMultiTracks();