Skip to content

Commit

Permalink
Fix: Download Method (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trenly authored Oct 25, 2021
1 parent 474b766 commit fce081a
Showing 1 changed file with 12 additions and 21 deletions.
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

0 comments on commit fce081a

Please sign in to comment.