-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsanitize.ps1
87 lines (77 loc) · 2.92 KB
/
sanitize.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Read the Packages file and separate it using `r as a delimeter
$Packages = (Get-Content Packages -Delimiter "`r")
$preware_feed = ($Packages | foreach-Object -ThrottleLimit 8 -Parallel {
# Broken URL checker
function Test-BrokenUrl ($url) {
# Use Invoke-WebRequest with -Method Head to send a HEAD request and get only the headers
$response = Invoke-WebRequest -Uri $url -Method Head -SkipHttpErrorCheck
if ($response.StatusCode -eq 404) {
# Return true if 404
return $true
} else {
# Return false for any other status code
return $false
}
# Stop execution if any 500 code is received
if ($response.StatusCode -match '^5') {
Write-Error "Server internal issue while accessing $url"
Pause
exit 1
}
}
# Define a function to remove special characters
function Remove-SpecialChars {
param (
[string]$InputString
)
# Use regular expression to remove special characters
$OutputString = $InputString -replace "'", "``" -replace '[^\w .,:/;`-]', ' '
# Return the output string
return $OutputString
}
$package = $_
foreach ($line in $package -split "`n") {
if ($line -match "^Source:") {
$json = $line.Replace("Source: ", "")
$source = ($json | ConvertFrom-Json)
# Remove special characters from the description
$source.FullDescription = Remove-SpecialChars -InputString $source.FullDescription
# Check if the file is available, update the title if not
if($source.Location -like "*//*") {
$urlbroken = Test-BrokenUrl $source.Location
} Else {
$urlbroken = $true
}
if ($urlbroken) { $source.Title = $source.Title + " - Missing IPK"}
$newjson = ($source | ConvertTo-Json -Compress)
Write-Output "Source: $newjson"
} else {
$line
}
}
Write-Progress -Activity " Processing:" -Status $source.Title
})
# Get the current date and time in ISO format
$now = Get-Date -Format "yyyy-MM-ddTHHmmss"
# Create a backup file name with the date and time
$backup = "Packages.bak-$now"
# Copy the original file to the backup file
Copy-Item "Packages" $backup
# Write the Preware feed to the Packages file
$preware_feed | Out-File Packages
# Check if gzip exists as an executable
$gzip = Get-Command -Name gzip -ErrorAction SilentlyContinue
# If gzip exists, launch it
if ($gzip) {
gzip -kf Packages
} else {
Write-Warning "gzip not found, you may need to run gzip -k Packages manually"
}
Write-Host "Missing IPKs:"
$missing = ($preware_feed | Select-String "Missing IPK")
foreach($line in $missing){
$json = $line -replace ".*Source: "
$json = $json.Trim()
$source = ($json | convertfrom-json)
Write-Host $source.Title + $source.Location
}