@@ -8,28 +8,38 @@ function writeToFile(filename, response, callback) {
88}
99
1010function download ( fileUrl , filename , callback ) {
11- https . get ( fileUrl , function ( response ) {
12- if (
13- response . statusCode > 300 &&
14- response . statusCode < 400 &&
15- response . headers . location
16- ) {
17- if ( url . parse ( response . headers . location ) . hostname ) {
18- https . get ( response . headers . location , function ( res ) {
19- writeToFile ( filename , res , callback ) ;
20- } ) ;
21- } else {
22- https . get (
23- url . resolve ( url . parse ( fileUrl ) . hostname , response . headers . location ) ,
24- function ( res ) {
11+ https
12+ . get ( fileUrl , function ( response ) {
13+ if (
14+ response . statusCode > 300 &&
15+ response . statusCode < 400 &&
16+ response . headers . location
17+ ) {
18+ if ( url . parse ( response . headers . location ) . hostname ) {
19+ https . get ( response . headers . location , function ( res ) {
2520 writeToFile ( filename , res , callback ) ;
26- }
21+ } ) ;
22+ } else {
23+ https . get (
24+ url . resolve ( url . parse ( fileUrl ) . hostname , response . headers . location ) ,
25+ function ( res ) {
26+ writeToFile ( filename , res , callback ) ;
27+ }
28+ ) ;
29+ }
30+ } else {
31+ writeToFile ( filename , response , callback ) ;
32+ }
33+ } )
34+ . on ( "error" , function ( err ) {
35+ console . error ( err ) ;
36+ if ( err . code === "ECONNRESET" ) {
37+ console . error ( "\n** Your connection was reset. **" ) ;
38+ console . error (
39+ "\n** Are you behind a proxy or a firewall that is preventing a connection? **"
2740 ) ;
2841 }
29- } else {
30- writeToFile ( filename , response , callback ) ;
31- }
32- } ) ;
42+ } ) ;
3343}
3444
3545module . exports = {
0 commit comments