@@ -94,119 +94,141 @@ param(
94
94
}
95
95
}
96
96
97
- $res = $req.GetResponse ();
97
+ try {
98
+ [System.Net.HttpWebResponse ]$res = $req.GetResponse ();
98
99
99
- try {
100
- $headers = @ {}
101
- foreach ($key in $res.Headers ) {
102
- $value = $res.Headers [$key ];
103
- if ($value ) {
104
- $headers.Add (" $key " , " $value " )
100
+ try {
101
+ $headers = @ {}
102
+ foreach ($key in $res.Headers ) {
103
+ $value = $res.Headers [$key ];
104
+ if ($value ) {
105
+ $headers.Add (" $key " , " $value " )
106
+ }
105
107
}
106
- }
107
108
108
- if ($headers.ContainsKey (" Content-Type" )) {
109
- $contentType = $headers [' Content-Type' ]
110
- if ($contentType -ne $null ) {
111
- if ($contentType.ToLower ().Contains(" text/html" ) -or $contentType.ToLower ().Contains(" text/plain" )) {
112
- Write-Warning " $fileName is of content type $contentType "
113
- Set-Content - Path " $fileName .istext" - Value " $fileName has content type $contentType " - Encoding UTF8 - Force
109
+ if ($headers.ContainsKey (" Content-Type" )) {
110
+ $contentType = $headers [' Content-Type' ]
111
+ if ($contentType -ne $null ) {
112
+ if ($contentType.ToLower ().Contains(" text/html" ) -or $contentType.ToLower ().Contains(" text/plain" )) {
113
+ Write-Warning " $fileName is of content type $contentType "
114
+ Set-Content - Path " $fileName .istext" - Value " $fileName has content type $contentType " - Encoding UTF8 - Force
115
+ }
114
116
}
117
+ }
118
+ } catch {
119
+ # not able to get content-type header
120
+ Write-Debug " Error getting content type - $ ( $_.Exception.Message ) "
121
+ }
122
+
123
+ if ($fileName -and ! (Split-Path $fileName )) {
124
+ $fileName = Join-Path (Get-Location - PSProvider " FileSystem" ) $fileName
125
+ }
126
+ elseif ((! $Passthru -and ($fileName -eq $null )) -or (($fileName -ne $null ) -and (Test-Path - PathType " Container" $fileName )))
127
+ {
128
+ [string ]$fileName = ([regex ]' (?i)filename=(.*)$' ).Match( $res.Headers [" Content-Disposition" ] ).Groups[1 ].Value
129
+ $fileName = $fileName.trim (" \/"" '" )
130
+ if (! $fileName ) {
131
+ $fileName = $res.ResponseUri.Segments [-1 ]
132
+ $fileName = $fileName.trim (" \/" )
133
+ if (! $fileName ) {
134
+ $fileName = Read-Host " Please provide a file name"
135
+ }
136
+ $fileName = $fileName.trim (" \/" )
137
+ if (! ([IO.FileInfo ]$fileName ).Extension) {
138
+ $fileName = $fileName + " ." + $res.ContentType.Split (" ;" )[0 ].Split(" /" )[1 ]
139
+ }
115
140
}
116
- }
117
- } catch {
118
- # not able to get content-type header
119
- Write-Debug " Error getting content type - $ ( $_.Exception.Message ) "
120
- }
141
+ $fileName = Join-Path (Get-Location - PSProvider " FileSystem" ) $fileName
142
+ }
143
+ if ($Passthru ) {
144
+ $encoding = [System.Text.Encoding ]::GetEncoding( $res.CharacterSet )
145
+ [string ]$output = " "
146
+ }
121
147
122
- if ($fileName -and ! (Split-Path $fileName )) {
123
- $fileName = Join-Path (Get-Location - PSProvider " FileSystem" ) $fileName
124
- }
125
- elseif ((! $Passthru -and ($fileName -eq $null )) -or (($fileName -ne $null ) -and (Test-Path - PathType " Container" $fileName )))
126
- {
127
- [string ]$fileName = ([regex ]' (?i)filename=(.*)$' ).Match( $res.Headers [" Content-Disposition" ] ).Groups[1 ].Value
128
- $fileName = $fileName.trim (" \/"" '" )
129
- if (! $fileName ) {
130
- $fileName = $res.ResponseUri.Segments [-1 ]
131
- $fileName = $fileName.trim (" \/" )
132
- if (! $fileName ) {
133
- $fileName = Read-Host " Please provide a file name"
134
- }
135
- $fileName = $fileName.trim (" \/" )
136
- if (! ([IO.FileInfo ]$fileName ).Extension) {
137
- $fileName = $fileName + " ." + $res.ContentType.Split (" ;" )[0 ].Split(" /" )[1 ]
138
- }
148
+ if ($res.StatusCode -eq 401 -or $res.StatusCode -eq 403 -or $res.StatusCode -eq 404 ) {
149
+ $env: ChocolateyExitCode = $res.StatusCode
150
+ throw " Remote file either doesn't exist, is unauthorized, or is forbidden for '$url '."
139
151
}
140
- $fileName = Join-Path (Get-Location - PSProvider " FileSystem" ) $fileName
141
- }
142
- if ($Passthru ) {
143
- $encoding = [System.Text.Encoding ]::GetEncoding( $res.CharacterSet )
144
- [string ]$output = " "
145
- }
146
152
147
- if ($res.StatusCode -eq 200 ) {
148
- [long ]$goal = $res.ContentLength
149
- $goalFormatted = Format-FileSize $goal
150
- $reader = $res.GetResponseStream ()
153
+ if ($res.StatusCode -eq 200 ) {
154
+ [long ]$goal = $res.ContentLength
155
+ $goalFormatted = Format-FileSize $goal
156
+ $reader = $res.GetResponseStream ()
151
157
152
- if ($fileName ) {
153
- $fileDirectory = $ ([System.IO.Path ]::GetDirectoryName($fileName ))
154
- if (! (Test-Path ($fileDirectory ))) {
155
- [System.IO.Directory ]::CreateDirectory($fileDirectory ) | Out-Null
156
- }
158
+ if ($fileName ) {
159
+ $fileDirectory = $ ([System.IO.Path ]::GetDirectoryName($fileName ))
160
+ if (! (Test-Path ($fileDirectory ))) {
161
+ [System.IO.Directory ]::CreateDirectory($fileDirectory ) | Out-Null
162
+ }
157
163
158
- try {
159
- $writer = new-object System.IO.FileStream $fileName , " Create"
160
- } catch {
161
- throw $_.Exception
164
+ try {
165
+ $writer = new-object System.IO.FileStream $fileName , " Create"
166
+ } catch {
167
+ throw $_.Exception
168
+ }
162
169
}
163
- }
164
170
165
- [byte []]$buffer = new-object byte[] 1048576
166
- [long ]$total = [long ]$count = [long ]$iterLoop = 0
171
+ [byte []]$buffer = new-object byte[] 1048576
172
+ [long ]$total = [long ]$count = [long ]$iterLoop = 0
167
173
168
- $originalEAP = $ErrorActionPreference
169
- $ErrorActionPreference = ' Stop'
170
- try {
171
- do
172
- {
173
- $count = $reader.Read ($buffer , 0 , $buffer.Length );
174
- if ($fileName ) {
175
- $writer.Write ($buffer , 0 , $count );
176
- }
177
-
178
- if ($Passthru ){
179
- $output += $encoding.GetString ($buffer , 0 , $count )
180
- } elseif (! $quiet ) {
181
- $total += $count
182
- $totalFormatted = Format-FileSize $total
183
- if ($goal -gt 0 -and ++ $iterLoop % 10 -eq 0 ) {
184
- Write-Progress " Downloading $url to $fileName " " Saving $totalFormatted of $goalFormatted ($total /$goal )" - id 0 - percentComplete (($total / $goal )* 100 )
174
+ $originalEAP = $ErrorActionPreference
175
+ $ErrorActionPreference = ' Stop'
176
+ try {
177
+ do
178
+ {
179
+ $count = $reader.Read ($buffer , 0 , $buffer.Length );
180
+ if ($fileName ) {
181
+ $writer.Write ($buffer , 0 , $count );
185
182
}
183
+
184
+ if ($Passthru ){
185
+ $output += $encoding.GetString ($buffer , 0 , $count )
186
+ } elseif (! $quiet ) {
187
+ $total += $count
188
+ $totalFormatted = Format-FileSize $total
189
+ if ($goal -gt 0 -and ++ $iterLoop % 10 -eq 0 ) {
190
+ Write-Progress " Downloading $url to $fileName " " Saving $totalFormatted of $goalFormatted ($total /$goal )" - id 0 - percentComplete (($total / $goal )* 100 )
191
+ }
186
192
187
- if ($total -eq $goal ) {
188
- Write-Progress " Completed download of $url ." " Completed download of $fileName ($goalFormatted )." - id 0 - Completed
193
+ if ($total -eq $goal ) {
194
+ Write-Progress " Completed download of $url ." " Completed download of $fileName ($goalFormatted )." - id 0 - Completed
195
+ }
189
196
}
190
- }
191
- } while ($count -gt 0 )
192
- Write-Host " "
193
- Write-Host " Download of $ ( [System.IO.Path ]::GetFileName($fileName )) ($goalFormatted ) completed."
194
- } catch {
195
- throw $_.Exception
196
- } finally {
197
- $ErrorActionPreference = $originalEAP
198
- }
197
+ } while ($count -gt 0 )
198
+ Write-Host " "
199
+ Write-Host " Download of $ ( [System.IO.Path ]::GetFileName($fileName )) ($goalFormatted ) completed."
200
+ } catch {
201
+ throw $_.Exception
202
+ } finally {
203
+ $ErrorActionPreference = $originalEAP
204
+ }
199
205
200
- $reader.Close ()
201
- if ($fileName ) {
202
- $writer.Flush ()
203
- $writer.Close ()
206
+ $reader.Close ()
207
+ if ($fileName ) {
208
+ $writer.Flush ()
209
+ $writer.Close ()
210
+ }
211
+ if ($Passthru ){
212
+ $output
213
+ }
214
+ }
215
+ } catch {
216
+ if ($req -ne $null ) {
217
+ $req.ServicePoint.MaxIdleTime = 0
218
+ $req.Abort ();
219
+ # ruthlessly remove $req to ensure it isn't reused
220
+ Remove-Variable req
221
+ Start-Sleep 1
222
+ [GC ]::Collect()
204
223
}
205
- if ($Passthru ){
206
- $output
224
+
225
+ Set-PowerShellExitCode 404
226
+ throw " The remote file either doesn't exist, is unauthorized, or is forbidden for url '$url '. $ ( $_.Exception.Message ) "
227
+ } finally {
228
+ if ($res -ne $null ) {
229
+ $res.Close ()
207
230
}
208
231
}
209
- $res.Close ();
210
232
}
211
233
212
234
# this could be cleaned up with http://learn-powershell.net/2013/02/08/powershell-and-events-object-events/
0 commit comments