Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-435) Use Original Download File Name
  (GH-332) Fixup example
  • Loading branch information
ferventcoder committed Jan 28, 2016
2 parents 3d9dc83 + 9c2a554 commit 03a177c
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 33 deletions.
3 changes: 3 additions & 0 deletions src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<ItemGroup>
<EmbeddedResource Include="helpers\chocolateyScriptRunner.ps1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="helpers\functions\Get-FileName.ps1" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,22 @@ OPTIONAL - Specify custom headers
Example:
--------
$options =
@{
Headers = @{
Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
'Accept-Charset' = 'ISO-8859-1,utf-8;q=0.7,*;q=0.3';
'Accept-Language' = 'en-GB,en-US;q=0.8,en;q=0.6';
Cookie = '[email protected]';
Referer = 'http://submain.com/download/ghostdoc/';
}
}
Get-ChocolateyWebFile 'ghostdoc' 'http://submain.com/download/GhostDoc_v4.0.zip' -options $options
$options =
@{
Headers = @{
Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
'Accept-Charset' = 'ISO-8859-1,utf-8;q=0.7,*;q=0.3';
'Accept-Language' = 'en-GB,en-US;q=0.8,en;q=0.6';
Cookie = 'requiredinfo=info';
Referer = 'https://somelocation.com/';
}
}
Get-ChocolateyWebFile 'package' 'https://somelocation.com/thefile.exe' -options $options
.PARAMETER GetOriginalFileName
OPTIONAL switch to allow Chocolatey to determine the original file name from the url
.EXAMPLE
Get-ChocolateyWebFile '__NAME__' 'C:\somepath\somename.exe' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT'
Expand All @@ -83,7 +86,8 @@ param(
[string] $checksumType = '',
[string] $checksum64 = '',
[string] $checksumType64 = $checksumType,
[hashtable] $options = @{Headers=@{}}
[hashtable] $options = @{Headers=@{}},
[switch]$getOriginalFileName
)
Write-Debug "Running 'Get-ChocolateyWebFile' for $packageName with url:`'$url`', fileFullPath:`'$fileFullPath`', url64bit:`'$url64bit`', checksum: `'$checksum`', checksumType: `'$checksumType`', checksum64: `'$checksum64`', checksumType64: `'$checksumType64`'";

Expand All @@ -104,19 +108,10 @@ param(
# only set if urls are different
if ($url32bit -ne $url64bit) {
$checksum = $checksum64
if ($checkSumType64 -ne '') {
if ($checkSumType64 -ne '') {
$checksumType = $checksumType64
}
}
}

try {
$fileDirectory = $([System.IO.Path]::GetDirectoryName($fileFullPath))
if (!(Test-Path($fileDirectory))) {
[System.IO.Directory]::CreateDirectory($fileDirectory) | Out-Null
}
}
} catch {
Write-Host "Attempt to create directory failed for '$fileFullPath'."
}

$forceX86 = $env:chocolateyForceX86;
Expand All @@ -128,6 +123,27 @@ param(
$checksumType = $checksumType32
}

if ($getOriginalFileName) {
try {
$fileDirectory = [System.IO.Path]::GetDirectoryName($fileFullPath)
$originalFileName = [System.IO.Path]::GetFileName($fileFullPath)
$fileFullPath = Get-FileName -url $url -defaultName $originalFileName
$fileFullPath = Join-Path $fileDirectory $fileFullPath
$fileFullPath = [System.IO.Path]::GetFullPath($fileFullPath)
} catch {
Write-Host "Attempt to use original download file name failed for '$url'."
}
}

try {
$fileDirectory = $([System.IO.Path]::GetDirectoryName($fileFullPath))
if (!(Test-Path($fileDirectory))) {
[System.IO.Directory]::CreateDirectory($fileDirectory) | Out-Null
}
} catch {
Write-Host "Attempt to create directory failed for '$fileFullPath'."
}

$headers = @{}
if ($url.StartsWith('http')) {
try {
Expand Down Expand Up @@ -194,4 +210,6 @@ param(
# $url is already set properly to the used location.
#Write-Debug "Verifying downloaded file is not known to contain viruses. FilePath: `'$fileFullPath`'."
#Get-VirusCheckValid -location $url -file $fileFullPath

return $fileFullPath
}
100 changes: 100 additions & 0 deletions src/chocolatey.resources/helpers/functions/Get-FileName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2011 - Present RealDimensions Software, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Based on http://stackoverflow.com/a/13571471/18475
function Get-FileName {
param(
[string]$url = '',
[string]$defaultName,
$userAgent = 'chocolatey command line'
)

Write-Debug "Running 'Get-FileName' to determine name with url:'$url', defaultName:'$defaultName'";

$originalFileName = $defaultName
$fileName = $null

$request = [System.Net.HttpWebRequest]::Create($url)
if ($request -eq $null) {
$request.Close()
return $originalFileName
}

$request.Method = "GET"
$request.Accept = '*/*'
$request.AllowAutoRedirect = $true
$request.MaximumAutomaticRedirections = 20
#$request.KeepAlive = $true
$request.Timeout = 20000

#http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-httpw
$request.CookieContainer = New-Object System.Net.CookieContainer
$request.UserAgent = $userAgent

try
{
[HttpWebResponse]$response = $request.GetResponse()
if ($response -eq $null) {
$response.Close()
return $originalFileName
}

[string]$header = $response.Headers['Content-Disposition']
[string]$headerLocation = $response.Headers['Location']

# start with content-disposition header
if ($header -ne '') {
$fileHeaderName = 'filename='
$index = $header.LastIndexOf($fileHeaderName, [StringComparison]::OrdinalIgnoreCase)
if ($index -gt -1) {
$fileName = $header.Substring($index + $fileHeaderName.Length).Replace('"', '')
}
}

# If empty, check location header next
if ($fileName -eq $null -or $fileName -eq '') {
if ($headerLocation -ne '') {
$fileName = [System.IO.Path]::GetFileName($headerLocation)
}
}

# Next comes using the response url value
if ($fileName -eq $null -or $fileName -eq '') {
$containsQuery = [System.IO.Path]::GetFileName($url).Contains('?')
$containsEquals = [System.IO.Path]::GetFileName($url).Contains('=')
$fileName = [System.IO.Path]::GetFileName($response.ResponseUri.ToString())
}

$response.Close()
$response.Dispose()

[System.Text.RegularExpressions.Regex]$containsABadCharacter = New-Object Regex("[" + [System.Text.RegularExpressions.Regex]::Escape([System.IO.Path]::GetInvalidFileNameChars()) + "]");

# when all else fails, default the name
if ($fileName -eq $null -or $fileName -eq '' -or $containsABadCharacter.IsMatch($fileName)) {
$fileName = $originalFileName
}

Write-Debug "File name determined from url is '$fileName'"

return $fileName
} catch
{
$request.ServicePoint.MaxIdleTime = 0
$request.Abort();
Write-Debug "Url request/response failed - file name will be '$originalFileName'"

return $originalFileName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ Example:
Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
'Accept-Charset' = 'ISO-8859-1,utf-8;q=0.7,*;q=0.3';
'Accept-Language' = 'en-GB,en-US;q=0.8,en;q=0.6';
Cookie = '[email protected]';
Referer = 'http://submain.com/download/ghostdoc/';
Cookie = 'requiredinfo=info';
Referer = 'https://somelocation.com/';
}
}
Get-ChocolateyWebFile 'ghostdoc' 'http://submain.com/download/GhostDoc_v4.0.zip' -options $options
Get-ChocolateyWebFile 'package' 'https://somelocation.com/thefile.exe' -options $options
.EXAMPLE
Install-ChocolateyPackage '__NAME__' 'EXE_OR_MSI' 'SILENT_ARGS' 'URL' '64BIT_URL_DELETE_IF_NO_64BIT'
Expand Down Expand Up @@ -108,6 +108,6 @@ param(
if (![System.IO.Directory]::Exists($tempDir)) { [System.IO.Directory]::CreateDirectory($tempDir) | Out-Null }
$file = Join-Path $tempDir "$($packageName)Install.$fileType"

Get-ChocolateyWebFile $packageName $file $url $url64bit -checksum $checksum -checksumType $checksumType -checksum64 $checksum64 -checksumType64 $checksumType64 -options $options
Install-ChocolateyInstallPackage $packageName $fileType $silentArgs $file -validExitCodes $validExitCodes
$filePath = Get-ChocolateyWebFile $packageName $file $url $url64bit -checksum $checksum -checksumType $checksumType -checksum64 $checksum64 -checksumType64 $checksumType64 -options $options -getOriginalFileName
Install-ChocolateyInstallPackage $packageName $fileType $silentArgs $filePath -validExitCodes $validExitCodes
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ param(
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir) | Out-Null}
$file = Join-Path $tempDir "$($packageName)Install.$fileType"

Get-ChocolateyWebFile $packageName $file $url $url64bit -checkSum $checkSum -checksumType $checksumType -checkSum64 $checkSum64 -checksumType64 $checksumType64 -options $options
Get-ChocolateyUnzip "$file" $unzipLocation $specificFolder $packageName
$filePath = Get-ChocolateyWebFile $packageName $file $url $url64bit -checkSum $checkSum -checksumType $checksumType -checkSum64 $checkSum64 -checksumType64 $checksumType64 -options $options -getOriginalFileName
Get-ChocolateyUnzip "$filePath" $unzipLocation $specificFolder $packageName
}

0 comments on commit 03a177c

Please sign in to comment.