Refactor detection of url file name

This commit is contained in:
Manuel Thalmann 2023-07-16 01:46:57 +02:00
parent a5bcee666e
commit 35feef4445

View file

@ -6,16 +6,17 @@ function Install-SoftwarePackage([Context] $context, [string] $location, [string
[string]$tempDir = $null; [string]$tempDir = $null;
if (-not ($local.IsPresent)) { if (-not ($local.IsPresent)) {
Write-Information "Determining the file name of $location";
$tempDir = $context.GetTempDirectory(); $tempDir = $context.GetTempDirectory();
$tempFile = Join-Path $tempDir "download.tmp"; $request = [System.Net.WebRequest]::Create($url);
$request.AllowAutoRedirect = $false;
$response = $request.GetResponse();
$fileName = [System.IO.Path]::GetFileName($response.GetResponseHeader("Location"));
Write-Information "$fileName";
$filePath = Join-Path $tempDir $fileName;
Write-Information "Downloading setup file from $location"; Write-Information "Downloading setup file from $location";
$response = Invoke-WebRequest $location -OutFile $tempFile; Invoke-WebRequest $location -OutFile $filePath;
$fileName = [System.IO.Path]::GetFileName($response.Headers["Location"]);
Write-Information "Renaming setup file to $fileName";
$filePath = Join-Path $tempDir $fileName;
Move-Item $tempFile $filePath;
} else { } else {
$filePath = $location; $filePath = $location;
} }