|
| 1 | +Function Get-FileFromUrlOrCache |
| 2 | +{ |
| 3 | + <# |
| 4 | + .Synopsis |
| 5 | + Gets a file from the download cache or (if unavailable) download and cache it (if the cache is enabled) |
| 6 | +
|
| 7 | + .Parameter Url |
| 8 | + The URL to be downloaded from. |
| 9 | +
|
| 10 | + .Parameter CachedFileName |
| 11 | + The name of the file to be used to store the downloaded resource in the cache. |
| 12 | +
|
| 13 | + .Example |
| 14 | + Get-FileFromUrlOrCache 'http://www.example.com/test.zip' |
| 15 | +
|
| 16 | + .Example |
| 17 | + Get-FileFromUrlOrCache 'http://www.example.com/test.zip' 'cached-file-name.zip' |
| 18 | +
|
| 19 | + .Outputs |
| 20 | + [System.Array] |
| 21 | + #> |
| 22 | + Param ( |
| 23 | + [Parameter(Mandatory = $True, Position = 0)] |
| 24 | + [ValidateNotNull()] |
| 25 | + [ValidateLength(1, [int]::MaxValue)] |
| 26 | + [string] $Url, |
| 27 | + [Parameter(Mandatory = $False, Position = 1)] |
| 28 | + [string] $CachedFileName |
| 29 | + ) |
| 30 | + Begin { |
| 31 | + $localFile = $null |
| 32 | + $fromCache = $null |
| 33 | + } |
| 34 | + Process { |
| 35 | + $extension = '' |
| 36 | + If ($null -ne $CachedFileName -and $CachedFileName -ne '') { |
| 37 | + $match = $CachedFileName | Select-String -Pattern '.(\.[A-Za-z0-9_\-]+)$' |
| 38 | + If ($match) { |
| 39 | + $extension = $match.Matches.Groups[1].Value |
| 40 | + } |
| 41 | + } Else { |
| 42 | + $CachedFileName = '' |
| 43 | + $match = $Url | Select-String -Pattern '^[^:]+:/+[^?#/]+/(?:[^?#/]*/)*([^?#/]+)(?:$|^|#)' |
| 44 | + If ($match) { |
| 45 | + $nameFromUrl = $match.Matches.Groups[1].Value |
| 46 | + $match = $nameFromUrl | Select-String -Pattern '.(\.[A-Za-z0-9_\-]+)$' |
| 47 | + If ($match) { |
| 48 | + $extension = $match.Matches.Groups[1].Value |
| 49 | + } |
| 50 | + If ($nameFromUrl -imatch '^[a-z0-9_\-][a-z0-9_\-\.]*[a-z0-9_\-]$') { |
| 51 | + $CachedFileName = $nameFromUrl; |
| 52 | + } |
| 53 | + } |
| 54 | + If ($CachedFileName -eq '') { |
| 55 | + $stream = New-Object System.IO.MemoryStream |
| 56 | + Try { |
| 57 | + $streamWriter = New-Object -TypeName System.IO.BinaryWriter -ArgumentList @($stream) |
| 58 | + Try { |
| 59 | + $streamWriter.Write([System.Text.Encoding]::UTF8.GetBytes($Url)) |
| 60 | + $streamWriter.Flush() |
| 61 | + $stream.Position = 0 |
| 62 | + $hash = Get-FileHash -InputStream $stream -Algorithm SHA1 |
| 63 | + $CachedFileName = $hash.Hash |
| 64 | + } |
| 65 | + Finally { |
| 66 | + $streamWriter.Dispose() |
| 67 | + } |
| 68 | + } |
| 69 | + Finally { |
| 70 | + $stream.Dispose() |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + If ($Script:DOWNLOADCACHE_PATH -eq '') { |
| 75 | + $fullCachePath = '' |
| 76 | + } Else { |
| 77 | + If (-Not(Test-Path -LiteralPath $Script:DOWNLOADCACHE_PATH -PathType Container)) { |
| 78 | + New-Item -Path $Script:DOWNLOADCACHE_PATH -ItemType Directory | Out-Null |
| 79 | + } |
| 80 | + $fullCachePath = Join-Path -Path $Script:DOWNLOADCACHE_PATH -ChildPath $CachedFileName |
| 81 | + } |
| 82 | + If ($fullCachePath -ne '' -and (Test-Path -LiteralPath $fullCachePath -PathType Leaf)) { |
| 83 | + $localFile = $fullCachePath |
| 84 | + $fromCache = $true |
| 85 | + } Else { |
| 86 | + $temporaryFile = Get-TemporaryFileWithExtension -Extension $extension |
| 87 | + Try { |
| 88 | + Try { |
| 89 | + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + [Net.SecurityProtocolType]::Tls11 + [Net.SecurityProtocolType]::Tls |
| 90 | + } |
| 91 | + Catch { |
| 92 | + Write-Debug '[Net.ServicePointManager] or [Net.SecurityProtocolType] not found in current environment' |
| 93 | + } |
| 94 | + Write-Debug "Downloading from $Url" |
| 95 | + Invoke-WebRequest -UseBasicParsing $Url -OutFile $temporaryFile |
| 96 | + If ($fullCachePath -ne '') { |
| 97 | + Move-Item -LiteralPath $temporaryFile -Destination $fullCachePath |
| 98 | + $localFile = $fullCachePath |
| 99 | + $fromCache = $true |
| 100 | + } Else { |
| 101 | + $localFile = $fullCachePath |
| 102 | + $fromCache = $false |
| 103 | + } |
| 104 | + } Catch { |
| 105 | + Try { |
| 106 | + Remove-Item -LiteralPath $temporaryFile |
| 107 | + } Catch { |
| 108 | + Write-Debug 'Failed to remove a temporary file' |
| 109 | + } |
| 110 | + Throw |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + End { |
| 115 | + $localFile |
| 116 | + $fromCache |
| 117 | + } |
| 118 | +} |
0 commit comments