Skip to content

Commit 5c0ac1f

Browse files
committed
refactoring
1 parent 4ec8b4b commit 5c0ac1f

File tree

2 files changed

+275
-12
lines changed

2 files changed

+275
-12
lines changed

bootstrap.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var AudioDataAnalyzer = require('./library/audioDataAnalyzer').analyzer;
22

3-
var TrackDownloader = require('./library/trackDownloader').downloader;
3+
var FileDownloader = require('./library/fileDownloader').downloader;
44

55
var http = require('http');
66

@@ -86,7 +86,29 @@ http.createServer(function (request, response) {
8686

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

89-
getWaveData(queryObject.trackId, queryObject.trackFormat, queryObject.peaksAmount, function(error, peaks) {
89+
// track format
90+
switch(options.fileExtension) {
91+
case 'mp3':
92+
formatCode = 'mp31';
93+
break;
94+
case 'ogg':
95+
formatCode = 'ogg1';
96+
break;
97+
default:
98+
throw 'unsupported file format';
99+
}
100+
101+
var options = {
102+
trackId: queryObject.trackId,
103+
trackFormat: queryObject.trackFormat,
104+
peaksAmount: queryObject.peaksAmount,
105+
host: 'storage-new.newjamendo.com',
106+
port: 80,
107+
method: 'GET',
108+
serverDirecotry: './downloaded_tracks'
109+
};
110+
111+
getWaveData(options, function(error, peaks) {
90112

91113
if (!error) {
92114

@@ -158,30 +180,26 @@ console.log('server is listening, ip: ' + serverIp + ', port: ' + serverPort);
158180
*
159181
* get the wave data for a given trackId
160182
*
161-
* @param {type} trackId
162-
* @param {type} trackFormat
163-
* @param {type} peaksAmount
183+
* @param {type} options
164184
* @param {type} callback
165185
* @returns {undefined}
166186
*/
167-
var getWaveData = function getWaveDataFunction(trackId, trackFormat, peaksAmount, callback) {
187+
var getWaveData = function getWaveDataFunction(options, callback) {
168188

169189
// initialize the audioAnalyzer
170190
var audioDataAnalyzer = new AudioDataAnalyzer();
171191

172192
// initialize the track downloader
173-
var trackDownloader = new TrackDownloader();
174-
175-
var temporaryTracksDirecotry = './downloaded_tracks';
193+
var fileDownloader = new FileDownloader();
176194

177195
// download the track and write it on the disc of it does not already exist
178-
trackDownloader.writeTrackToDisc(trackId, function writeTrackCallback(error, trackPath) {
196+
fileDownloader.writeToDisc(options, function writeFileCallback(error, trackPath) {
179197

180198
// if there was no error downloading and writing the track
181199
if (!error) {
182200

183201
// analyze the track using ffmpeg
184-
audioDataAnalyzer.getPeaks(trackPath, peaksAmount, function getValuesCallback(error, peaks) {
202+
audioDataAnalyzer.getPeaks(trackPath, options, function getValuesCallback(error, peaks) {
185203

186204
// if there was no error analyzing the track
187205
if (!error) {
@@ -202,6 +220,6 @@ var getWaveData = function getWaveDataFunction(trackId, trackFormat, peaksAmount
202220

203221
}
204222

205-
}, temporaryTracksDirecotry, trackFormat);
223+
});
206224

207225
};

library/fileDownloader.js

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
// nodejs fs
2+
var fs = require('fs');
3+
4+
// nodejs http
5+
var http = require('http');
6+
7+
var DirectoryManager = require('./directoryManager').directoryManager;
8+
9+
var FileManager = require('./fileManager').fileManager;
10+
11+
12+
/**
13+
*
14+
* downloader
15+
*
16+
* @returns {downloaderConstructor}
17+
*/
18+
var downloader = function downloaderConstructor() {
19+
20+
};
21+
22+
/**
23+
*
24+
* fetches a file from a remote server and writes it into a folder on disc
25+
*
26+
* @param {type} options
27+
* @param {type} callback
28+
* @returns {undefined}
29+
*/
30+
downloader.prototype.writeToDisc = function(options, callback) {
31+
32+
if (options === undefined) {
33+
34+
options = {};
35+
36+
}
37+
38+
if (options.serverDirecotry === undefined) {
39+
40+
options.serverDirecotry = './downloads';
41+
42+
}
43+
44+
var directoryManager = new DirectoryManager();
45+
46+
var that = this;
47+
48+
// check if the temporary tracks directory already exists
49+
directoryManager.exists(options.serverDirecotry, function directoryExistsCallback(error, exists) {
50+
51+
// if there was no error checking if the directory exists
52+
if (!error) {
53+
54+
// if the directory does not exist
55+
if (!exists) {
56+
57+
// create a new directory
58+
directoryManager.create(options.serverDirecotry, createDirectoryCallback = function(error) {
59+
60+
// if there was no error creating the new directory
61+
if (!error) {
62+
63+
// download the the track and store it on disc
64+
that.downloadIfNotExists(options, callback);
65+
66+
} else {
67+
68+
callback(error);
69+
70+
}
71+
72+
});
73+
74+
} else {
75+
76+
// download the the track and store it on disc
77+
that.downloadIfNotExists(options, callback);
78+
79+
}
80+
81+
} else {
82+
83+
callback(error);
84+
85+
}
86+
87+
});
88+
89+
};
90+
91+
/**
92+
*
93+
* donwloads a track if does not already exist on disc
94+
*
95+
* @param {type} options
96+
* @param {type} callback
97+
* @returns {undefined}
98+
*/
99+
downloader.prototype.downloadIfNotExists = function downloadIfNotExists(options, callback) {
100+
101+
var fileManager = new FileManager();
102+
103+
var filePath = options.serverDirecotry + '/' + options.fileName;
104+
105+
var that = this;
106+
107+
// check if the file already exists
108+
fileManager.exists(filePath, function fileExistsCallback(error, exists) {
109+
110+
// if there was no error checking if the file exists
111+
if (!error) {
112+
113+
if (!exists) {
114+
115+
// download the file and store it in the temporary directory
116+
that.downloadFile(options, callback);
117+
118+
} else {
119+
120+
callback(false, filePath);
121+
122+
}
123+
124+
} else {
125+
126+
callback(error);
127+
128+
}
129+
130+
});
131+
132+
};
133+
134+
/**
135+
*
136+
* download a file
137+
*
138+
* @param {type} downloadOptions
139+
* @param {type} callback
140+
* @returns {undefined}
141+
*/
142+
downloader.prototype.downloadFile = function downloadFileFunction(downloadOptions, callback) {
143+
144+
console.log('downloadFile: ' + downloadOptions.path);
145+
146+
if (downloadOptions === undefined) {
147+
148+
callback('downloadOptions is undefined');
149+
150+
}
151+
152+
if (downloadOptions.method === undefined) {
153+
154+
downloadOptions.method = 'GET';
155+
156+
}
157+
158+
if (downloadOptions.remotePort === undefined) {
159+
160+
downloadOptions.port = 80;
161+
162+
}
163+
164+
if (downloadOptions.remoteHost === undefined) {
165+
166+
callback('download host is undefined');
167+
168+
}
169+
170+
if (downloadOptions.remotePath === undefined) {
171+
172+
callback('download path is undefined');
173+
174+
}
175+
176+
var writeStream = fs.createWriteStream(downloadOptions.filePath + downloadOptions.fileName);
177+
178+
// open a new write stream
179+
writeStream.on('open', function() {
180+
181+
var requestOptions = {
182+
hostname: downloadOptions.remoteHost,
183+
port: downloadOptions.remotePort,
184+
path: downloadOptions.remotePath,
185+
method: downloadOptions.method
186+
};
187+
188+
// request the file from remote server
189+
var httpRequest = http.request(requestOptions, function(httpResponse) {
190+
191+
console.log('writeTrackToDisc httpRequest STATUS: ' + httpResponse.statusCode);
192+
console.log('writeTrackToDisc httpRequest HEADERS: ' + JSON.stringify(httpResponse.headers));
193+
194+
// on successful request
195+
httpResponse.on('data', function(chunk) {
196+
197+
// write the file
198+
writeStream.write(chunk);
199+
200+
});
201+
202+
// the connection got closed
203+
httpResponse.on('end', function() {
204+
205+
console.log('remote file: ' + downloadOptions.fileName + ', got downloaded into: ' + downloadOptions.serverDirectory);
206+
207+
// close the write stream
208+
writeStream.end();
209+
210+
callback(false, downloadOptions.serverDirectory + downloadOptions.fileName);
211+
212+
});
213+
214+
});
215+
216+
// the request to the remote server failed
217+
httpRequest.on('error', function(error) {
218+
219+
console.log('writeToDisc, http request error: ' + error.message);
220+
221+
writeStream.end();
222+
223+
callback(error);
224+
225+
});
226+
227+
httpRequest.end();
228+
229+
});
230+
231+
// writing the file failed
232+
writeStream.on('error', function(error) {
233+
234+
console.log('writeToDisc writeStream, error: ' + error);
235+
236+
// close the stream
237+
writeStream.end();
238+
239+
callback(error);
240+
241+
});
242+
243+
};
244+
245+
module.exports.downloader = downloader;

0 commit comments

Comments
 (0)