@@ -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,21 @@ 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 ) {
95
- continue
131
+ if ($OriginalPath -match $allowedPattern ) {
132
+ $pattern = ' ^(?:https?:\/\/)?[^\/]+(?<path>\/.*)$'
133
+ if ($OriginalPath -match $pattern ) {
134
+ $path = $matches [' path' ]
135
+ $UpdatedPath = " $BlobUrl /" + $path -join ' /'
136
+ }
137
+
96
138
}
97
-
98
- $pattern = ' ^(?:https?:\/\/)?[^\/]+(?<path>\/.*)$'
99
- if ($OriginalUrl -match $pattern ) {
100
- $path = $matches [' path' ]
101
- $UpdatedPath = " $BlobUrl /" + $path -join ' /'
102
- }
139
+ else {
140
+ Write-DebugLog " Skipping : $OriginalPath "
141
+ }
142
+
103
143
}
104
144
catch {
105
- Write-Debug " ERROR HTTP: $OriginalPath -> $UpdatedPath : $_ "
145
+ Write-DebugLog " ERROR HTTP: $OriginalPath -> $UpdatedPath : $_ "
106
146
}
107
147
}
108
148
elseif ($OriginalPath.StartsWith (" /" )) {
@@ -115,52 +155,60 @@ function Rewrite-ImageLinks {
115
155
# Relative paths - Ensure consistency by converting to root-relative
116
156
# 1. Get the parent directory of the HTML file
117
157
$ParentDirectory = Split-Path - Path $HtmlFile.FullName - Parent
118
- Write-Debug " Parent Directory: $ParentDirectory "
158
+ Write-DebugLog " Parent Directory: $ParentDirectory "
119
159
120
160
# 2. Combine the parent directory with the original path
121
161
$CombinedPath = Join-Path - Path $ParentDirectory - ChildPath $OriginalPath
122
- Write-Debug " Combined Path: $CombinedPath "
162
+ Write-DebugLog " Combined Path: $CombinedPath "
123
163
124
164
if (-not (Test-Path - Path $CombinedPath )) {
125
- Write-Debug " Path does not exist: $CombinedPath "
165
+ Write-DebugLog " Path does not exist: $CombinedPath "
126
166
continue ;
127
167
}
128
168
# 3. Resolve the full path
129
169
$ResolvedPath = Resolve-Path - Path $CombinedPath
130
- Write-Debug " Resolved Path: $ResolvedPath "
170
+ Write-DebugLog " Resolved Path: $ResolvedPath "
131
171
132
172
# 4. Get the root-relative path
133
173
$LocalImagesFullPath = (Get-Item $LocalPath ).FullName
134
- Write-Debug " Local Images Full Path: $LocalImagesFullPath "
174
+ Write-DebugLog " Local Images Full Path: $LocalImagesFullPath "
135
175
136
176
$RootRelativePath = $ResolvedPath.Path.Replace ($LocalImagesFullPath , " " ).Replace(" \" , " /" )
137
- Write-Debug " Root Relative Path: $RootRelativePath "
177
+ Write-DebugLog " Root Relative Path: $RootRelativePath "
138
178
139
179
# 5. Construct the updated path
140
180
$UpdatedPath = " $BlobUrl /$RootRelativePath "
141
- Write-Debug " Updated Path: $UpdatedPath "
181
+ Write-DebugLog " Updated Path: $UpdatedPath "
142
182
}
143
183
catch {
144
- Write-Debug " Error resolving path: $_ "
184
+ Write-ErrorLog " Error resolving path: $_ "
145
185
continue ;
146
186
}
147
187
}
148
188
149
189
# Replace the original path in the content
150
190
if ($OriginalPath -ne $UpdatedPath ) {
151
191
$FileContent = $FileContent -replace [regex ]::Escape($OriginalPath ), $UpdatedPath
152
- Write-Debug " Replaced: $OriginalPath -> $UpdatedPath "
192
+ Write-DebugLog " Replaced: $OriginalPath -> $UpdatedPath "
153
193
$totalLinks += 1 ;
154
194
}
155
195
156
196
}
157
197
158
198
# Save updated content back to the file
159
199
Set-Content - LiteralPath $HtmlFile.FullName - Value $FileContent
160
- Write-InfoLog " Updated ($ ( $Matches.count ) ): $ ( $HtmlFile.FullName ) "
200
+ Write-DebugLog " Updated ($ ( $Matches.count ) ): $ ( $HtmlFile.FullName ) "
201
+
202
+ # **Progress tracking**
203
+ $index ++
204
+ $percentage = [math ]::Round(($index / $totalFiles ) * 100 , 0 )
205
+
206
+ # Log progress every 10%
207
+ if ($percentage -ge $lastPercentage + $progressInterval ) {
208
+ Write-InfoLog " Progress: $percentage % ($index of $totalFiles HTML files processed with $totalLinks links updated)"
209
+ $lastPercentage = $percentage
210
+ }
161
211
162
212
}
163
- Write-InfoLog " HTML link rewriting complete of $totalLinks ."
164
-
165
-
166
- }
213
+ Write-InfoLog " HTML link rewriting complete: $totalLinks links updated across $totalFiles files."
214
+ }
0 commit comments