mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Improves driver downloads by using cached links
Reduces unnecessary Download Center requests by preferring cached file details when available Falls back to downloading/parsing the page only on cache miss, then backfills the cache for future runs Adds error handling and logging around cache load/update to avoid download failures from cache issues
This commit is contained in:
@@ -185,83 +185,123 @@ function Save-MicrosoftDriversTask {
|
||||
### GET THE DOWNLOAD LINK
|
||||
$status = "Getting download link..."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
||||
WriteLog "Getting download page content for $modelName from $modelLink"
|
||||
$OriginalVerbosePreference = $VerbosePreference
|
||||
$VerbosePreference = 'SilentlyContinue'
|
||||
# Use passed-in UserAgent and Headers
|
||||
$downloadPageContent = Invoke-WebRequest -Uri $modelLink -UseBasicParsing -Headers $Headers -UserAgent $UserAgent
|
||||
$VerbosePreference = $OriginalVerbosePreference
|
||||
WriteLog "Complete"
|
||||
|
||||
$status = "Parsing download page..."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
||||
WriteLog "Parsing download page for file"
|
||||
$scriptPattern = '<script>window.__DLCDetails__={(.*?)}<\/script>'
|
||||
$scriptMatch = [regex]::Match($downloadPageContent.Content, $scriptPattern)
|
||||
# Initialize Win10/Win11 link variables
|
||||
$win10Link = $null
|
||||
$win10FileName = $null
|
||||
$win11Link = $null
|
||||
$win11FileName = $null
|
||||
|
||||
if ($scriptMatch.Success) {
|
||||
$scriptContent = $scriptMatch.Groups[1].Value
|
||||
# $downloadFilePattern = '"name":"(.*?)",.*?"url":"(.*?)"'
|
||||
$downloadFilePattern = '"name":"([^"]+\.(?:msi|zip))",[^}]*?"url":"(.*?)"'
|
||||
$downloadFileMatches = [regex]::Matches($scriptContent, $downloadFilePattern, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
|
||||
# Prefer cached Download Center details (Source C) to avoid unnecessary internet calls and cache rewrites
|
||||
$useCachedDownloadCenterDetails = $false
|
||||
try {
|
||||
$cache = Import-SurfaceDriverIndexCache -DriversFolder $DriversFolder
|
||||
$cachedDetails = @($cache.DownloadCenterDetails | Where-Object { $_.Link -eq $modelLink } | Select-Object -First 1)
|
||||
if ($cachedDetails.Count -gt 0 -and $cachedDetails[0].Files -and $cachedDetails[0].Files.Count -gt 0) {
|
||||
$useCachedDownloadCenterDetails = $true
|
||||
WriteLog "Surface cache: Using cached Download Center details for $modelName from $modelLink"
|
||||
|
||||
foreach ($downloadFile in @($cachedDetails[0].Files)) {
|
||||
if ($null -eq $downloadFile) { continue }
|
||||
$currentFileName = $downloadFile.Name
|
||||
$fileUrl = $downloadFile.Url
|
||||
if ([string]::IsNullOrWhiteSpace($currentFileName) -or [string]::IsNullOrWhiteSpace($fileUrl)) { continue }
|
||||
|
||||
$win10Link = $null
|
||||
$win10FileName = $null
|
||||
$win11Link = $null
|
||||
$win11FileName = $null
|
||||
|
||||
# Iterate through all matches to find potential Win10 and Win11 links
|
||||
foreach ($downloadFile in $downloadFileMatches) {
|
||||
$currentFileName = $downloadFile.Groups[1].Value
|
||||
$fileUrl = $downloadFile.Groups[2].Value
|
||||
|
||||
if ($currentFileName -match "Win10") {
|
||||
$win10Link = $fileUrl
|
||||
$win10FileName = $currentFileName
|
||||
WriteLog "Found Win10 link: $win10FileName"
|
||||
}
|
||||
elseif ($currentFileName -match "Win11") {
|
||||
$win11Link = $fileUrl
|
||||
$win11FileName = $currentFileName
|
||||
WriteLog "Found Win11 link: $win11FileName"
|
||||
if ($currentFileName -match "Win10") {
|
||||
$win10Link = $fileUrl
|
||||
$win10FileName = $currentFileName
|
||||
WriteLog "Found Win10 link (cached): $win10FileName"
|
||||
}
|
||||
elseif ($currentFileName -match "Win11") {
|
||||
$win11Link = $fileUrl
|
||||
$win11FileName = $currentFileName
|
||||
WriteLog "Found Win11 link (cached): $win11FileName"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
WriteLog "Surface cache: Failed loading cached Download Center details for '$modelName'. Error: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
# Update local cache with Download Center file details (Source C) for this model.
|
||||
# This runs during download (not during Get Models) so it won't slow the listview population.
|
||||
try {
|
||||
$filesForCache = [System.Collections.Generic.List[pscustomobject]]::new()
|
||||
if ($win10Link -and $win10FileName) {
|
||||
$filesForCache.Add([pscustomobject]@{ Name = $win10FileName; Url = $win10Link })
|
||||
}
|
||||
if ($win11Link -and $win11FileName) {
|
||||
$filesForCache.Add([pscustomobject]@{ Name = $win11FileName; Url = $win11Link })
|
||||
# Cache miss: download and parse the model's Download Center page (Source C), then backfill the cache
|
||||
if (-not $useCachedDownloadCenterDetails) {
|
||||
WriteLog "Getting download page content for $modelName from $modelLink"
|
||||
$OriginalVerbosePreference = $VerbosePreference
|
||||
$VerbosePreference = 'SilentlyContinue'
|
||||
# Use passed-in UserAgent and Headers
|
||||
$downloadPageContent = Invoke-WebRequest -Uri $modelLink -UseBasicParsing -Headers $Headers -UserAgent $UserAgent
|
||||
$VerbosePreference = $OriginalVerbosePreference
|
||||
WriteLog "Complete"
|
||||
|
||||
$status = "Parsing download page..."
|
||||
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
|
||||
WriteLog "Parsing download page for file"
|
||||
$scriptPattern = '<script>window.__DLCDetails__={(.*?)}<\/script>'
|
||||
$scriptMatch = [regex]::Match($downloadPageContent.Content, $scriptPattern)
|
||||
|
||||
if ($scriptMatch.Success) {
|
||||
$scriptContent = $scriptMatch.Groups[1].Value
|
||||
# $downloadFilePattern = '"name":"(.*?)",.*?"url":"(.*?)"'
|
||||
$downloadFilePattern = '"name":"([^"]+\.(?:msi|zip))",[^}]*?"url":"(.*?)"'
|
||||
$downloadFileMatches = [regex]::Matches($scriptContent, $downloadFilePattern, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
|
||||
|
||||
# Iterate through all matches to find potential Win10 and Win11 links
|
||||
foreach ($downloadFile in $downloadFileMatches) {
|
||||
$currentFileName = $downloadFile.Groups[1].Value
|
||||
$fileUrl = $downloadFile.Groups[2].Value
|
||||
|
||||
if ($currentFileName -match "Win10") {
|
||||
$win10Link = $fileUrl
|
||||
$win10FileName = $currentFileName
|
||||
WriteLog "Found Win10 link: $win10FileName"
|
||||
}
|
||||
elseif ($currentFileName -match "Win11") {
|
||||
$win11Link = $fileUrl
|
||||
$win11FileName = $currentFileName
|
||||
WriteLog "Found Win11 link: $win11FileName"
|
||||
}
|
||||
}
|
||||
|
||||
if ($filesForCache.Count -gt 0) {
|
||||
$cache = Import-SurfaceDriverIndexCache -DriversFolder $DriversFolder
|
||||
$detailsEntry = [pscustomobject][ordered]@{
|
||||
Model = $modelName
|
||||
Link = $modelLink
|
||||
Files = @($filesForCache)
|
||||
# Update local cache with Download Center file details (Source C) for this model.
|
||||
# This runs during download (not during Get Models) so it won't slow the listview population.
|
||||
try {
|
||||
$filesForCache = [System.Collections.Generic.List[pscustomobject]]::new()
|
||||
if ($win10Link -and $win10FileName) {
|
||||
$filesForCache.Add([pscustomobject]@{ Name = $win10FileName; Url = $win10Link })
|
||||
}
|
||||
if ($win11Link -and $win11FileName) {
|
||||
$filesForCache.Add([pscustomobject]@{ Name = $win11FileName; Url = $win11Link })
|
||||
}
|
||||
|
||||
$newDetails = [System.Collections.Generic.List[pscustomobject]]::new()
|
||||
foreach ($item in @($cache.DownloadCenterDetails)) {
|
||||
if ($null -ne $item -and $item.PSObject.Properties['Link'] -and $item.Link -ne $modelLink) {
|
||||
$newDetails.Add($item)
|
||||
if ($filesForCache.Count -gt 0) {
|
||||
$cache = Import-SurfaceDriverIndexCache -DriversFolder $DriversFolder
|
||||
$detailsEntry = [pscustomobject][ordered]@{
|
||||
Model = $modelName
|
||||
Link = $modelLink
|
||||
Files = @($filesForCache)
|
||||
}
|
||||
}
|
||||
$newDetails.Add($detailsEntry)
|
||||
$cache.DownloadCenterDetails = @($newDetails)
|
||||
Save-SurfaceDriverIndexCache -Cache $cache -DriversFolder $DriversFolder
|
||||
}
|
||||
}
|
||||
catch {
|
||||
WriteLog "Surface cache: Failed updating Download Center details cache for '$modelName'. Error: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
$newDetails = [System.Collections.Generic.List[pscustomobject]]::new()
|
||||
foreach ($item in @($cache.DownloadCenterDetails)) {
|
||||
if ($null -ne $item -and $item.PSObject.Properties['Link'] -and $item.Link -ne $modelLink) {
|
||||
$newDetails.Add($item)
|
||||
}
|
||||
}
|
||||
$newDetails.Add($detailsEntry)
|
||||
$cache.DownloadCenterDetails = @($newDetails)
|
||||
Save-SurfaceDriverIndexCache -Cache $cache -DriversFolder $DriversFolder
|
||||
}
|
||||
}
|
||||
catch {
|
||||
WriteLog "Surface cache: Failed updating Download Center details cache for '$modelName'. Error: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
$useCachedDownloadCenterDetails = $true
|
||||
}
|
||||
}
|
||||
|
||||
if ($useCachedDownloadCenterDetails) {
|
||||
# Decision logic to select the appropriate download link
|
||||
$downloadLink = $null
|
||||
$fileName = $null
|
||||
|
||||
Reference in New Issue
Block a user