Skip to content

Commit d3b2fc3

Browse files
committed
added format type
1 parent 09cc9f5 commit d3b2fc3

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

bootstrap.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ http.createServer(function (request, response) {
8383

8484
var queryObject = querystring.parse(urlParts.query);
8585

86-
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.peaksAmount !== 'undefined') {
86+
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.trackFormat !== 'undefined' && queryObject.peaksAmount !== 'undefined') {
8787

88-
getWaveData(queryObject.trackId, queryObject.peaksAmount, function(error, peaks) {
88+
getWaveData(queryObject.trackId, queryObject.trackFormat, queryObject.peaksAmount, function(error, peaks) {
8989

9090
if (!error) {
9191

@@ -108,6 +108,45 @@ http.createServer(function (request, response) {
108108
}
109109

110110
break;
111+
case '/getTrack':
112+
113+
var queryObject = querystring.parse(urlParts.query);
114+
115+
if (typeof queryObject !== 'undefined' && queryObject.trackId !== 'undefined' && queryObject.trackFormat !== 'undefined') {
116+
117+
var trackName = queryObject.trackId + '.' + queryObject.trackFormat;
118+
119+
fs.readFile('downloaded_tracks/' + trackName, function(error, track) {
120+
121+
var mimeType;
122+
123+
switch(queryObject.trackFormat) {
124+
case 'ogg':
125+
mimeType = 'audio/ogg';
126+
break;
127+
case 'mp3':
128+
mimeType = 'audio/mpeg';
129+
break;
130+
}
131+
132+
if (!error) {
133+
134+
response.writeHead(200, { 'Content-Type': mimeType });
135+
response.write(track);
136+
response.end();
137+
138+
} else {
139+
140+
response.writeHead(404, { 'Content-Type': 'text/html' });
141+
response.write('page not found');
142+
response.end();
143+
144+
}
145+
146+
});
147+
148+
}
149+
111150
default:
112151
response.writeHead(404, { 'Content-Type': 'text/html' });
113152
response.write('page not found');
@@ -125,11 +164,12 @@ console.log('server is listening, ip: ' + serverIp + ', port: ' + serverPort);
125164
* get the wave data for a given trackId
126165
*
127166
* @param {type} trackId
167+
* @param {type} trackFormat
128168
* @param {type} peaksAmount
129169
* @param {type} callback
130170
* @returns {undefined}
131171
*/
132-
var getWaveData = function getWaveDataFunction(trackId, peaksAmount, callback) {
172+
var getWaveData = function getWaveDataFunction(trackId, trackFormat, peaksAmount, callback) {
133173

134174
// initialize the audioAnalyzer
135175
var audioDataAnalyzer = new AudioDataAnalyzer();
@@ -138,7 +178,6 @@ var getWaveData = function getWaveDataFunction(trackId, peaksAmount, callback) {
138178
var trackDownloader = new TrackDownloader();
139179

140180
var temporaryTracksDirecotry = './downloaded_tracks';
141-
var format = 'ogg';
142181

143182
// download the track and write it on the disc of it does not already exist
144183
trackDownloader.writeTrackToDisc(trackId, function writeTrackCallback(error, trackPath) {
@@ -168,6 +207,6 @@ var getWaveData = function getWaveDataFunction(trackId, peaksAmount, callback) {
168207

169208
}
170209

171-
}, temporaryTracksDirecotry, format);
210+
}, temporaryTracksDirecotry, trackFormat);
172211

173212
};

0 commit comments

Comments
 (0)