Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Download Method #107

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions Tools/YamlCreate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -340,28 +340,19 @@ Function Get-InstallerFile {
[string] $PackageVersion

)

# Download and store the binary, but do not write to a file yet
$download = Invoke-WebRequest -Uri $URI -UserAgent 'winget/1.0' -DisableKeepAlive -TimeoutSec 30 -UseBasicParsing
# Attempt to get the file from the headers
try {
$contentDisposition = [System.Net.Mime.ContentDisposition]::new($download.Headers['Content-Disposition'])
$_Filename = $contentDisposition.FileName
} catch { Out-Null }
# Validate the headers reurned a valid file name
if (![string]::IsNullOrWhiteSpace($_Filename) -and $(Test-ValidFileName $_Filename)) {
$Filename = $_Filename
}
# If the headers did not return a valid file name, build our own file name
# Attempt to preserve the extension if it exists, otherwise, create our own
else {
$Filename = "$PackageIdentifier v$PackageVersion" + $(if ([System.IO.Path]::HasExtension($_Filename)) { [System.IO.Path]::GetExtension($_Filename) } elseif ([System.IO.Path]::HasExtension($InstallerUrl)) { [System.IO.Path]::GetExtension($URI) } else { '.winget-tmp' })
}
# Write File to disk
# Create a filename based on the Package Identifier and Version; Try to get the extension from the URL
# If the extension isn't found, use a custom one
$Filename = "$PackageIdentifier v$PackageVersion" + $(if([System.IO.Path]::HasExtension($URI)) { [System.IO.Path]::GetExtension($URI) } else { '.winget-tmp' })
$script:dest = Join-Path -Path $env:TEMP -ChildPath $Filename
$file = [System.IO.FileStream]::new($script:dest, [System.IO.FileMode]::Create)
$file.Write($download.Content, 0, $download.RawContentLength)
$file.Close()

# Create a new web client for downloading the file
$WebClient = [System.Net.WebClient]::new()
# If the system has a default proxy set, use it
$WebClient.Proxy = [System.Net.WebProxy]::GetDefaultProxy()
# Download the file
$WebClient.DownloadFile($URI, $dest)
# Dispose of the web client to release the resources it uses
$WebClient.Dispose()
}

# Prompts the user to enter installer values
Expand Down