Skip to content

Commit

Permalink
(GH-649) Write Small File When Download Type Text
Browse files Browse the repository at this point in the history
Write a file that can be used later when a file is not of the proper
type that is expected when downloading a file.
  • Loading branch information
ferventcoder committed Mar 2, 2016
1 parent dd115be commit 74aa4e6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ param(

$res = $req.GetResponse();

try {
$headers = @{}
foreach ($key in $res.Headers) {
$value = $res.Headers[$key];
if ($value) {
$headers.Add("$key","$value")
}
}

if ($headers.ContainsKey("Content-Type")) {
$contentType = $headers['Content-Type']
if ($contentType -ne $null) {
if ($contentType.ToLower().Contains("text/html") -or $contentType.ToLower().Contains("text/plain")) {
Write-Warning "$fileName is of content type $contentType"
Set-Content -Path "$fileName.istext" -Value "$fileName has content type $contentType" -Encoding UTF8 -Force
}
}
}
} catch {
# not able to get content-type header
Write-Debug "Error getting content type - $($_.Exception.Message)"
}

if($fileName -and !(Split-Path $fileName)) {
$fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
}
Expand Down

0 comments on commit 74aa4e6

Please sign in to comment.