-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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`'"; | ||
|
||
|
@@ -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; | ||
|
@@ -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 { | ||
|
@@ -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
100
src/chocolatey.resources/helpers/functions/Get-FileName.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters