Skip to content

Commit 6982ea7

Browse files
committed
💡 (ImagesToBlobStorage.ps1): enhance logging and progress tracking in image upload and link rewriting scripts
Improve the logging mechanism by replacing `Write-Debug` with more specific logging functions like `Write-InfoLog`, `Write-ErrorLog`, and `Write-DebugLog` to provide clearer and more meaningful log messages. Add progress tracking for the HTML link rewriting process, logging progress at 10% intervals to give better visibility into the script's execution status. These changes enhance the script's usability and maintainability by providing more informative feedback during execution.
1 parent f08e50b commit 6982ea7

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

‎.powershell/_includes/ImagesToBlobStorage.ps1

+28-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ function Upload-ImageFiles {
1212
[string]$AzureSASToken
1313
)
1414
try {
15-
Write-Debug "Uploading image files to Azure Blob Storage using azcopy..."
15+
Write-InfoLog "Uploading image files to Azure Blob Storage using azcopy..."
1616
azcopy sync $LocalPath "$BlobUrlBase`?$AzureSASToken" --recursive=true --include-pattern "*.jpg;*.jpeg;*.png;*.gif;*.webp;*.svg" --compare-hash=MD5
17-
Write-Debug "Upload complete."
17+
Write-InfoLog "Upload complete."
1818
}
1919
catch {
20-
Write-Debug "Error during upload: $_"
20+
Write-ErrorLog"Error during upload: $_"
2121
}
2222
}
2323

@@ -84,10 +84,18 @@ function Rewrite-ImageLinks {
8484
$HtmlFiles = Get-ChildItem -Path $LocalPath -Recurse -Include *.html
8585

8686
$totalLinks = 0;
87+
$totalFiles = $HtmlFiles.Count
88+
89+
if ($totalFiles -eq 0) {
90+
Write-InfoLog "No .html files found for processing."
91+
return
92+
}
93+
94+
$lastPercentage = 0 # Tracks when to log progress
95+
$progressInterval = 10 # Percentage interval for logging
8796

8897
foreach ($HtmlFile in $HtmlFiles) {
8998

90-
# $FileContent = Get-Content -Path (Resolve-Path $HtmlFile.FullName) -Raw
9199
$FileContent = Get-Content -LiteralPath $HtmlFile.FullName -Raw
92100
# Regex to match all src attributes with image paths
93101
$ImageRegex = "(?i)(src|content|href)\s*=\s*([""']?)(?<url>[^\s""'>]+\.(jpg|jpeg|png|gif|webp|svg))\2"
@@ -120,18 +128,18 @@ function Rewrite-ImageLinks {
120128
try {
121129
# Define the regex pattern
122130
$allowedPattern = '^(?:https?:\/\/)?(?:nkdagility\.com|preview\.nkdagility\.com|yellow-pond-042d21b03.*\.westeurope\.5\.azurestaticapps\.net)(\/.*)?$'
123-
if ($OriginalUrl -match $allowedPattern) {
131+
if ($OriginalPath -match $allowedPattern) {
124132
continue
125133
}
126134

127135
$pattern = '^(?:https?:\/\/)?[^\/]+(?<path>\/.*)$'
128-
if ($OriginalUrl -match $pattern) {
136+
if ($OriginalPath -match $pattern) {
129137
$path = $matches['path']
130138
$UpdatedPath = "$BlobUrl/" + $path -join '/'
131139
}
132140
}
133141
catch {
134-
Write-Debug " ERROR HTTP: $OriginalPath -> $UpdatedPath : $_"
142+
Write-DebugLog " ERROR HTTP: $OriginalPath -> $UpdatedPath : $_"
135143
}
136144
}
137145
elseif ($OriginalPath.StartsWith("/")) {
@@ -151,7 +159,7 @@ function Rewrite-ImageLinks {
151159
Write-DebugLog "Combined Path: $CombinedPath"
152160

153161
if (-not (Test-Path -Path $CombinedPath)) {
154-
Write-Debug " Path does not exist: $CombinedPath"
162+
Write-DebugLog " Path does not exist: $CombinedPath"
155163
continue;
156164
}
157165
# 3. Resolve the full path
@@ -187,9 +195,17 @@ function Rewrite-ImageLinks {
187195
# Save updated content back to the file
188196
Set-Content -LiteralPath $HtmlFile.FullName -Value $FileContent
189197
Write-DebugLog "Updated ($($Matches.count)): $($HtmlFile.FullName)"
198+
199+
# **Progress tracking**
200+
$index++
201+
$percentage = [math]::Round(($index / $totalFiles) * 100, 0)
202+
203+
# Log progress every 10%
204+
if ($percentage -ge $lastPercentage + $progressInterval) {
205+
Write-InfoLog "Progress: $percentage% ($index of $totalFiles HTML files processed with $totalLinks links updated)"
206+
$lastPercentage = $percentage
207+
}
190208

191209
}
192-
Write-InfoLog "HTML link rewriting complete of $totalLinks."
193-
194-
195-
}
210+
Write-InfoLog "HTML link rewriting complete: $totalLinks links updated across $totalFiles files."
211+
}

0 commit comments

Comments
 (0)