@@ -12,12 +12,12 @@ function Upload-ImageFiles {
12
12
[string ]$AzureSASToken
13
13
)
14
14
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..."
16
16
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."
18
18
}
19
19
catch {
20
- Write-Debug " Error during upload: $_ "
20
+ Write-ErrorLog " Error during upload: $_ "
21
21
}
22
22
}
23
23
@@ -26,23 +26,52 @@ function Delete-LocalImageFiles {
26
26
param (
27
27
[string ]$LocalPath
28
28
)
29
+ $count = 0
29
30
try {
30
- Write-Debug " Deleting all image files locally..."
31
- Get-ChildItem - Path $LocalPath - Recurse - Include * .jpg, * .jpeg, * .png, * .gif, * .webp, * .svg | ForEach-Object {
31
+ Write-InfoLog " Deleting all image files locally..."
32
+ $images = Get-ChildItem - Path $LocalPath - Recurse - Include * .jpg, * .jpeg, * .png, * .gif, * .webp, * .svg
33
+ if ($images.Count -eq 0 ) {
34
+ Write-InfoLog " No image files found."
35
+ return 0 ;
36
+ }
37
+
38
+ $totalFiles = $images.Count
39
+ $size = ($images | Measure-Object - Property Length - Sum).Sum
40
+ $sizeString = " {0:N2} MB" -f ($size / 1 MB )
41
+ Write-InfoLog " Found ($totalFiles ) image files totalling $sizeString ."
42
+
43
+ $lastPercentage = 0 # Tracks when to log progress
44
+ $progressInterval = 10 # Percentage interval for logging
45
+
46
+ $images | ForEach-Object - Begin { $index = 0 } - Process {
32
47
try {
33
48
Remove-Item - Path $_.FullName - Force
34
- Write-Debug " Deleted: $ ( $_.FullName ) "
49
+ Write-DebugLog " Deleted: $ ( $_.FullName ) "
50
+ $count ++
51
+ $index ++
52
+
53
+ # Calculate percentage progress
54
+ $percentage = [math ]::Round(($index / $totalFiles ) * 100 , 0 )
55
+
56
+ # Log progress at defined intervals
57
+ if ($percentage -ge $lastPercentage + $progressInterval ) {
58
+ Write-InfoLog " Progress: $percentage % ($index of $totalFiles image files deleted)"
59
+ $lastPercentage = $percentage
60
+ }
35
61
}
36
62
catch {
37
- Write-Debug " Error deleting file $ ( $_.FullName ) : $_ "
63
+ Write-ErrorLog " Error deleting file $ ( $_.FullName ) : $_ "
38
64
}
39
65
}
40
66
}
41
67
catch {
42
- Write-Debug " Error during file deletion: $_ "
68
+ Write-ErrorLog " Error during image file deletion: $_ "
43
69
}
70
+ Write-InfoLog " Completed: Deleted $count image files."
71
+ return $count ;
44
72
}
45
73
74
+
46
75
# Method 3: Rewrite image links in .html files using regex
47
76
function Rewrite-ImageLinks {
48
77
param (
@@ -55,10 +84,18 @@ function Rewrite-ImageLinks {
55
84
$HtmlFiles = Get-ChildItem - Path $LocalPath - Recurse - Include * .html
56
85
57
86
$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
58
96
59
97
foreach ($HtmlFile in $HtmlFiles ) {
60
98
61
- # $FileContent = Get-Content -Path (Resolve-Path $HtmlFile.FullName) -Raw
62
99
$FileContent = Get-Content - LiteralPath $HtmlFile.FullName - Raw
63
100
# Regex to match all src attributes with image paths
64
101
$ImageRegex = " (?i)(src|content|href)\s*=\s*(["" ']?)(?<url>[^\s"" '>]+\.(jpg|jpeg|png|gif|webp|svg))\2"
@@ -91,18 +128,18 @@ function Rewrite-ImageLinks {
91
128
try {
92
129
# Define the regex pattern
93
130
$allowedPattern = ' ^(?:https?:\/\/)?(?:nkdagility\.com|preview\.nkdagility\.com|yellow-pond-042d21b03.*\.westeurope\.5\.azurestaticapps\.net)(\/.*)?$'
94
- if ($OriginalUrl -match $allowedPattern ) {
131
+ if ($OriginalPath -match $allowedPattern ) {
95
132
continue
96
133
}
97
134
98
135
$pattern = ' ^(?:https?:\/\/)?[^\/]+(?<path>\/.*)$'
99
- if ($OriginalUrl -match $pattern ) {
136
+ if ($OriginalPath -match $pattern ) {
100
137
$path = $matches [' path' ]
101
138
$UpdatedPath = " $BlobUrl /" + $path -join ' /'
102
139
}
103
140
}
104
141
catch {
105
- Write-Debug " ERROR HTTP: $OriginalPath -> $UpdatedPath : $_ "
142
+ Write-DebugLog " ERROR HTTP: $OriginalPath -> $UpdatedPath : $_ "
106
143
}
107
144
}
108
145
elseif ($OriginalPath.StartsWith (" /" )) {
@@ -115,52 +152,60 @@ function Rewrite-ImageLinks {
115
152
# Relative paths - Ensure consistency by converting to root-relative
116
153
# 1. Get the parent directory of the HTML file
117
154
$ParentDirectory = Split-Path - Path $HtmlFile.FullName - Parent
118
- Write-Debug " Parent Directory: $ParentDirectory "
155
+ Write-DebugLog " Parent Directory: $ParentDirectory "
119
156
120
157
# 2. Combine the parent directory with the original path
121
158
$CombinedPath = Join-Path - Path $ParentDirectory - ChildPath $OriginalPath
122
- Write-Debug " Combined Path: $CombinedPath "
159
+ Write-DebugLog " Combined Path: $CombinedPath "
123
160
124
161
if (-not (Test-Path - Path $CombinedPath )) {
125
- Write-Debug " Path does not exist: $CombinedPath "
162
+ Write-DebugLog " Path does not exist: $CombinedPath "
126
163
continue ;
127
164
}
128
165
# 3. Resolve the full path
129
166
$ResolvedPath = Resolve-Path - Path $CombinedPath
130
- Write-Debug " Resolved Path: $ResolvedPath "
167
+ Write-DebugLog " Resolved Path: $ResolvedPath "
131
168
132
169
# 4. Get the root-relative path
133
170
$LocalImagesFullPath = (Get-Item $LocalPath ).FullName
134
- Write-Debug " Local Images Full Path: $LocalImagesFullPath "
171
+ Write-DebugLog " Local Images Full Path: $LocalImagesFullPath "
135
172
136
173
$RootRelativePath = $ResolvedPath.Path.Replace ($LocalImagesFullPath , " " ).Replace(" \" , " /" )
137
- Write-Debug " Root Relative Path: $RootRelativePath "
174
+ Write-DebugLog " Root Relative Path: $RootRelativePath "
138
175
139
176
# 5. Construct the updated path
140
177
$UpdatedPath = " $BlobUrl /$RootRelativePath "
141
- Write-Debug " Updated Path: $UpdatedPath "
178
+ Write-DebugLog " Updated Path: $UpdatedPath "
142
179
}
143
180
catch {
144
- Write-Debug " Error resolving path: $_ "
181
+ Write-ErrorLog " Error resolving path: $_ "
145
182
continue ;
146
183
}
147
184
}
148
185
149
186
# Replace the original path in the content
150
187
if ($OriginalPath -ne $UpdatedPath ) {
151
188
$FileContent = $FileContent -replace [regex ]::Escape($OriginalPath ), $UpdatedPath
152
- Write-Debug " Replaced: $OriginalPath -> $UpdatedPath "
189
+ Write-DebugLog " Replaced: $OriginalPath -> $UpdatedPath "
153
190
$totalLinks += 1 ;
154
191
}
155
192
156
193
}
157
194
158
195
# Save updated content back to the file
159
196
Set-Content - LiteralPath $HtmlFile.FullName - Value $FileContent
160
- Write-InfoLog " Updated ($ ( $Matches.count ) ): $ ( $HtmlFile.FullName ) "
197
+ 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
+ }
161
208
162
209
}
163
- Write-InfoLog " HTML link rewriting complete of $totalLinks ."
164
-
165
-
166
- }
210
+ Write-InfoLog " HTML link rewriting complete: $totalLinks links updated across $totalFiles files."
211
+ }
0 commit comments