From 4e3b7e4683390b4046b94a84ff13f7200f42e133 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Thu, 12 Oct 2023 22:00:22 -0400 Subject: [PATCH] few fixes and new func IsURL --- Tools.ps1 | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index 7cbe4f8..bbf43f1 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -169,15 +169,15 @@ Function Get-Version { # Determines whether a URL is valid Function IsURLValid { param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL) - Try { - $HTTPRequest = [System.Net.WebRequest]::Create($URL) - $HTTPResponse = $HTTPRequest.GetResponse() - $HTTPStatus = [int]$HTTPResponse.StatusCode - $HTTPResponse.Close() - } - Catch { } - If ($HTTPStatus -ne 404) { Return $true } - Else { Return $false } + Try { $status = [System.Net.WebRequest]::Create($URL).GetResponse().StatusCode } + Catch { Write-Error $_.Exception.Message ; Return $false } + If ( $status -eq 404 ) { Return $false } + Return $true +} + +Function IsURL { + param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$Value) + Return $value -match "https?:\/\/" } # Downloads a file from a URL @@ -186,21 +186,19 @@ Function Download-File { [Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$URL, [Parameter(Mandatory=$false,ValueFromPipeline=$false)][string]$File ) - If ( !($File) ) { $File = '{0}{1}' -f (Get-TempPath), (Split-Path $URL -Leaf) } - $URL = Get-AbsoluteURI -URL $URL - If ( IsURLValid $URL ) { - If ( Test-Path $File ) { - Write-Output "Deleting existing file: ""${File}""" - Try { Remove-Item $File -Force } - Catch { Write-Error $_.Exception.Message } - } - Write-Output "Downloading ""${URL}"" to ""${File}""" - Try { (New-Object System.Net.WebClient).DownloadFile($URL,$File) } - Catch { Write-Error $_.Exception.Message } - If ( Test-Path "${File}" ) { Return "${File}" } - Else { Write-Output "Downloaded file does not exist at ""${File}""" ; Return $false } + $URI = Get-AbsoluteURI -URL $URL + If ( $null -eq $URI ) { Return } + If ( [string]::IsNullOrEmpty($File) ) { $File = '{0}{1}' -f (Get-TempPath), (Split-Path $URL -Leaf) } + If ( Test-Path $File ) { + Write-Output "Deleting existing file: ""${File}""" + Try { Remove-Item $File -Force } + Catch { Write-Error $_.Exception.Message ; Return } } - Else { Write-Output "URL is not valid or file cannot be reached: ${URL}" } + Write-Output "Downloading ""${URI}"" to ""${File}""" + Try { (New-Object System.Net.WebClient).DownloadFile($URI,$File) } + Catch { Write-Error $_.Exception.Message ; Return } + If ( Test-Path "${File}" ) { Return "${File}" } + Else { Write-Output "Downloaded file does not exist at ""${File}""" ; Return } } # Install a program from a provided path @@ -822,6 +820,7 @@ Function ZeroPad-Decimal { # Function to follow a URL and return the absolute URI of the result (whether redirected or not) Function Get-AbsoluteURI { param([string]$URL) + If ( !(IsURLValid $URL) ) { Return } Return [System.Net.HttpWebRequest]::Create($URL).GetResponse().ResponseUri.AbsoluteUri }