@@ -48,38 +48,25 @@ process.on('message', function(task) {
4848 function downloadFile ( cb ) {
4949 if ( task . file_path ) {
5050 // Download the file
51- var stream = fs . createWriteStream ( path ) ;
52-
53- // Store error if statusCode !== 200
54- var err ;
55- stream . on ( "finish" , function ( ) {
56- cb ( err ) ;
57- } ) ;
58-
5951 var apiUrl = url . parse ( task . file_path , false , true ) ;
60- var req = request ( apiUrl . protocol + "//" + apiUrl . host )
61- . get ( apiUrl . path ) ;
62-
63- // Warning, Black magic.
64- // Streaming and checking for status code is no easy task...
65- req . end ( function ( ) { } ) . req . once ( 'response' , function ( res ) {
66- if ( res . statusCode !== 200 ) {
67- if ( res . statusCode === 410 ) {
68- err = new Error ( '410 Gone' ) ;
69- err . skip = true ;
52+ request ( apiUrl . protocol + "//" + apiUrl . host )
53+ . get ( apiUrl . path )
54+ . expect ( 200 )
55+ . end ( function ( err , res ) {
56+ if ( err ) {
57+ if ( err . toString ( ) . match ( / 4 1 0 / ) ) {
58+ err . skip = true ;
59+ }
60+ else {
61+ err = new Error ( 'Error downloading file: ' + err . toString ( ) ) ;
62+ }
63+ return cb ( err ) ;
7064 }
71- else {
72- err = new Error ( 'Error downloading file, got status ' + res . statusCode ) ;
73- }
74-
75- stream . end ( ) ;
76- this . abort ( ) ;
77- }
78- } ) ;
79- req . pipe ( stream ) ;
65+ fs . writeFile ( path , res . text , { encoding : 'binary' } , cb ) ;
66+ } ) ;
8067 }
8168 else {
82- cb ( null ) ;
69+ cb ( ) ;
8370 }
8471 } ,
8572 function startHydration ( cb ) {
0 commit comments