@@ -33,6 +33,7 @@ http.createServer(function (request, response) {
3333
3434 // if the file exists send it to the client
3535 // not really secure but this is a prototype
36+ // TODO: filter the file request
3637 fs . readFile ( 'client' + urlParts . pathname , function ( error , fileContent ) {
3738
3839 if ( ! error ) {
@@ -112,41 +113,35 @@ http.createServer(function (request, response) {
112113
113114 var queryObject = querystring . parse ( urlParts . query ) ;
114115
116+ console . log ( queryObject ) ;
117+
115118 if ( typeof queryObject !== 'undefined' && queryObject . trackId !== 'undefined' && queryObject . trackFormat !== 'undefined' ) {
116119
117120 var trackName = queryObject . trackId + '.' + queryObject . trackFormat ;
121+ var trackPath = 'downloaded_tracks/' + trackName ;
122+
123+ var fileStat = fs . statSync ( trackPath ) ;
118124
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- } ) ;
125+ var mimeType ;
126+
127+ switch ( queryObject . trackFormat ) {
128+ case 'ogg' :
129+ mimeType = 'audio/ogg' ;
130+ break ;
131+ case 'mp3' :
132+ mimeType = 'audio/mpeg' ;
133+ break ;
134+ }
135+
136+ response . writeHead ( 200 , { 'Content-Type' : mimeType , 'Content-Length' : fileStat . size } ) ;
137+
138+ var readStream = fs . createReadStream ( trackPath ) ;
139+
140+ readStream . pipe ( response ) ;
147141
148142 }
149143
144+ break ;
150145 default :
151146 response . writeHead ( 404 , { 'Content-Type' : 'text/html' } ) ;
152147 response . write ( 'page not found' ) ;
0 commit comments