@@ -10,6 +10,8 @@ var FileManager = require('./fileManager').fileManager;
1010
1111
1212/**
13+ *
14+ * track downloader
1315 *
1416 * @returns {downloaderConstructor }
1517 */
@@ -40,16 +42,22 @@ downloader.prototype.writeTrackToDisc = function(trackId, callback, temporaryTra
4042
4143 var that = this ;
4244
45+ // check if the temporary tracks directory already exists
4346 directoryManager . exists ( temporaryTracksDirecotry , function directoryExistsCallback ( error , exists ) {
4447
48+ // if there was no error checking if the directory exists
4549 if ( ! error ) {
4650
51+ // if the directory does not exist
4752 if ( ! exists ) {
4853
54+ // create a new directory
4955 directoryManager . create ( temporaryTracksDirecotry , createDirectoryCallback = function ( error ) {
5056
57+ // if there was no error creating the new directory
5158 if ( ! error ) {
5259
60+ // download the the track and store it on disc
5361 that . downloadIfNotExists ( trackId , callback , temporaryTracksDirecotry , format ) ;
5462
5563 } else {
@@ -62,6 +70,7 @@ downloader.prototype.writeTrackToDisc = function(trackId, callback, temporaryTra
6270
6371 } else {
6472
73+ // download the the track and store it on disc
6574 that . downloadIfNotExists ( trackId , callback , temporaryTracksDirecotry , format ) ;
6675
6776 }
@@ -77,6 +86,8 @@ downloader.prototype.writeTrackToDisc = function(trackId, callback, temporaryTra
7786} ;
7887
7988/**
89+ *
90+ * donwloads a track if does not already exist on disc
8091 *
8192 * @param {type } trackId
8293 * @param {type } callback
@@ -94,12 +105,15 @@ downloader.prototype.downloadIfNotExists = function downloadIfNotExists(trackId,
94105
95106 var that = this ;
96107
108+ // check if the file already exists
97109 fileManager . exists ( filePath , function fileExistsCallback ( error , exists ) {
98110
111+ // if there was no error checking if the file exists
99112 if ( ! error ) {
100113
101114 if ( ! exists ) {
102115
116+ // download the file and store it in the temporary directory
103117 that . downloadFile ( trackId , callback , filePath , format ) ;
104118
105119 } else {
@@ -117,7 +131,17 @@ downloader.prototype.downloadIfNotExists = function downloadIfNotExists(trackId,
117131 } ) ;
118132
119133} ;
120-
134+
135+ /**
136+ *
137+ * download a file
138+ *
139+ * @param {type } trackId
140+ * @param {type } callback
141+ * @param {type } trackPath
142+ * @param {type } format
143+ * @returns {undefined }
144+ */
121145downloader . prototype . downloadFile = function downloadFileFunction ( trackId , callback , trackPath , format ) {
122146
123147 console . log ( 'downloadFile: ' + trackId ) ;
@@ -130,6 +154,7 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
130154
131155 var formatCode ;
132156
157+ // track format
133158 switch ( format ) {
134159 case 'mp3' :
135160 formatCode = 'mp31' ;
@@ -151,23 +176,29 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
151176
152177 var writeStream = fs . createWriteStream ( trackPath ) ;
153178
179+ // open a new write stream
154180 writeStream . on ( 'open' , function ( ) {
155181
182+ // request the file from remote server
156183 var httpRequest = http . request ( options , function ( httpResponse ) {
157184
158185 console . log ( 'writeTrackToDisc httpRequest STATUS: ' + httpResponse . statusCode ) ;
159186 console . log ( 'writeTrackToDisc httpRequest HEADERS: ' + JSON . stringify ( httpResponse . headers ) ) ;
160187
188+ // on successful request
161189 httpResponse . on ( 'data' , function ( chunk ) {
162190
191+ // write the file
163192 writeStream . write ( chunk ) ;
164193
165194 } ) ;
166195
196+ // the connection got closed
167197 httpResponse . on ( 'end' , function ( ) {
168198
169199 console . log ( 'file ' + trackPath + ' got downloaded into ' + trackPath ) ;
170200
201+ // close the write stream
171202 writeStream . end ( ) ;
172203
173204 callback ( false , trackPath ) ;
@@ -176,6 +207,7 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
176207
177208 } ) ;
178209
210+ // the request to the remote server failed
179211 httpRequest . on ( 'error' , function ( error ) {
180212
181213 console . log ( 'writeTrackToDisc, http request error: ' + error . message ) ;
@@ -190,10 +222,12 @@ downloader.prototype.downloadFile = function downloadFileFunction(trackId, callb
190222
191223 } ) ;
192224
225+ // writing the file failed
193226 writeStream . on ( 'error' , function ( error ) {
194227
195228 console . log ( 'writeTrackToDisc writeStream, error: ' + error ) ;
196229
230+ // close the stream
197231 writeStream . end ( ) ;
198232
199233 callback ( error ) ;
0 commit comments