@@ -13,8 +13,6 @@ function download (inputUri, outputFilePath, callback) {
13
13
var uri = url . parse ( inputUri ) ;
14
14
15
15
if ( inputUri . indexOf ( 'http://' ) !== 0 && inputUri . indexOf ( 'https://' ) !== 0 ) {
16
- // this is to detect scenarios like localhost:8080 where localhost is
17
- // treated as protocol even if it's not.
18
16
if ( inputUri . indexOf ( uri . protocol + '//' ) !== 0 ) {
19
17
inputUri = 'http://' + inputUri ;
20
18
uri = url . parse ( inputUri ) ;
@@ -55,33 +53,28 @@ function download (inputUri, outputFilePath, callback) {
55
53
var deferred = Q . defer ( ) ;
56
54
protocol . get ( options , function ( res ) {
57
55
58
- // If Moved Permanently or Found, redirect to new URL
59
56
if ( [ 301 , 302 ] . indexOf ( res . statusCode ) > - 1 ) {
60
57
return download ( res . headers . location , outputFilePath )
61
58
. then ( function ( result ) { deferred . resolve ( result ) ; } )
62
59
. catch ( function ( err ) { deferred . reject ( err ) ; } ) ;
63
60
}
64
61
65
- // If not OK or Not Modified, throw error
66
62
if ( [ 200 , 304 ] . indexOf ( res . statusCode ) === - 1 ) {
67
63
var err = new Error ( 'Error downloading \'' + inputUri + '\'. Response was \'' + res . statusCode + ' - ' + res . statusMessage + '\'.' ) ;
68
64
err . statusCode = res . statusCode ;
69
65
err . statusMessage = res . statusMessage ;
70
66
return deferred . reject ( err ) ;
71
67
}
72
68
73
- // If Not Modified, ignore
74
69
if ( res . statusCode === 304 ) {
75
70
return deferred . resolve ( { 'path' : outputFilePath , 'statusCode' : res . statusCode , 'statusMessage' : res . statusMessage , 'contentType' : res . headers [ 'content-type' ] } ) ;
76
71
}
77
72
78
- // Else save
79
73
res . pipe ( fs . createWriteStream ( outputFilePath ) )
80
74
. on ( 'close' , function ( ) {
81
75
var lastAccessed = new Date ( ) ;
82
76
var lastModified = res . headers [ 'last-modified' ] ? new Date ( res . headers [ 'last-modified' ] ) : lastAccessed ;
83
77
84
- // update the last modified time of the file to match the response header
85
78
fs . utimes ( outputFilePath , lastAccessed , lastModified , function ( err ) {
86
79
if ( err ) {
87
80
return deferred . reject ( err ) ;
0 commit comments