Skip to content

Commit 289f1c2

Browse files
committed
✨🔧 (CleanupDataFiles.ps1, ImagesToBlobStorage.ps1): enhance logging and file handling
Enhance the scripts to provide more informative logging and handle cases where no files are found. The changes include: - Adding checks for the presence of files before attempting deletion, logging a message if no files are found. - Calculating and logging the total size of files to be deleted, providing better insight into the operation. - Replacing `Write-Debug` with `Write-DebugLog` and `Write-ErrorLog` for consistency and clarity in logging. These improvements aim to make the scripts more robust and user-friendly by providing detailed feedback during execution.
1 parent a925457 commit 289f1c2

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

.powershell/_includes/CleanupDataFiles.ps1

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ function Delete-LocalDataFiles {
88
$count = 0
99
try {
1010
Write-InfoLog "Deleting all data file files locally from '$LocalPath'..."
11-
Get-ChildItem -Path $env:LOCAL_IMAGE_PATH -Recurse -Include data.captions.*.srt, data.captions.json, data.json, data.index.classifications.json | ForEach-Object {
11+
$files = Get-ChildItem -Path $env:LOCAL_IMAGE_PATH -Recurse -Include data.captions.*.srt, data.captions.json, data.json, data.index.classifications.json
12+
if ($files.Count -eq 0) {
13+
Write-InfoLog "No files found."
14+
return 0;
15+
}
16+
$size = ($files | Measure-Object -Property Length -Sum).Sum
17+
$sizeString = "{0:N2} MB" -f ($size / 1MB)
18+
Write-InfoLog "Found ($($files.Count)) files totalling $sizeString."
19+
$files | ForEach-Object {
1220
try {
1321
$count++
1422
Remove-Item -Path $_.FullName -Force

.powershell/_includes/ImagesToBlobStorage.ps1

+18-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ function Delete-LocalImageFiles {
2929
$count = 0
3030
try {
3131
Write-InfoLog "Deleting all image files locally..."
32-
Get-ChildItem -Path $LocalPath -Recurse -Include *.jpg, *.jpeg, *.png, *.gif, *.webp, *.svg | ForEach-Object {
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+
$size = ($images | Measure-Object -Property Length -Sum).Sum
38+
$sizeString = "{0:N2} MB" -f ($size / 1MB)
39+
Write-InfoLog "Found ($($images.Count)) image files of $sizeString."
40+
$images | ForEach-Object {
3341
try {
3442
$count++
3543
Remove-Item -Path $_.FullName -Force
@@ -119,49 +127,49 @@ function Rewrite-ImageLinks {
119127
# Relative paths - Ensure consistency by converting to root-relative
120128
# 1. Get the parent directory of the HTML file
121129
$ParentDirectory = Split-Path -Path $HtmlFile.FullName -Parent
122-
Write-Debug "Parent Directory: $ParentDirectory"
130+
Write-DebugLog "Parent Directory: $ParentDirectory"
123131

124132
# 2. Combine the parent directory with the original path
125133
$CombinedPath = Join-Path -Path $ParentDirectory -ChildPath $OriginalPath
126-
Write-Debug "Combined Path: $CombinedPath"
134+
Write-DebugLog "Combined Path: $CombinedPath"
127135

128136
if (-not (Test-Path -Path $CombinedPath)) {
129137
Write-Debug " Path does not exist: $CombinedPath"
130138
continue;
131139
}
132140
# 3. Resolve the full path
133141
$ResolvedPath = Resolve-Path -Path $CombinedPath
134-
Write-Debug "Resolved Path: $ResolvedPath"
142+
Write-DebugLog "Resolved Path: $ResolvedPath"
135143

136144
# 4. Get the root-relative path
137145
$LocalImagesFullPath = (Get-Item $LocalPath).FullName
138-
Write-Debug "Local Images Full Path: $LocalImagesFullPath"
146+
Write-DebugLog "Local Images Full Path: $LocalImagesFullPath"
139147

140148
$RootRelativePath = $ResolvedPath.Path.Replace($LocalImagesFullPath, "").Replace("\", "/")
141-
Write-Debug "Root Relative Path: $RootRelativePath"
149+
Write-DebugLog "Root Relative Path: $RootRelativePath"
142150

143151
# 5. Construct the updated path
144152
$UpdatedPath = "$BlobUrl/$RootRelativePath"
145-
Write-Debug " Updated Path: $UpdatedPath"
153+
Write-DebugLog " Updated Path: $UpdatedPath"
146154
}
147155
catch {
148-
Write-Debug " Error resolving path: $_"
156+
Write-ErrorLog " Error resolving path: $_"
149157
continue;
150158
}
151159
}
152160

153161
# Replace the original path in the content
154162
if ($OriginalPath -ne $UpdatedPath) {
155163
$FileContent = $FileContent -replace [regex]::Escape($OriginalPath), $UpdatedPath
156-
Write-Debug " Replaced: $OriginalPath -> $UpdatedPath"
164+
Write-DebugLog " Replaced: $OriginalPath -> $UpdatedPath"
157165
$totalLinks += 1;
158166
}
159167

160168
}
161169

162170
# Save updated content back to the file
163171
Set-Content -LiteralPath $HtmlFile.FullName -Value $FileContent
164-
Write-InfoLog "Updated ($($Matches.count)): $($HtmlFile.FullName)"
172+
Write-DebugLog "Updated ($($Matches.count)): $($HtmlFile.FullName)"
165173

166174
}
167175
Write-InfoLog "HTML link rewriting complete of $totalLinks."

0 commit comments

Comments
 (0)