From 74aa4e6b9e9403abcd473ab2bed8cb9f678af4a6 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Wed, 2 Mar 2016 10:17:14 -0600 Subject: [PATCH] (GH-649) Write Small File When Download Type Text Write a file that can be used later when a file is not of the proper type that is expected when downloading a file. --- .../helpers/functions/Get-WebFile.ps1 | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 index e3c64e025b..743c9b7fdb 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 @@ -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 }