forked from trustwallet/assets
-
Notifications
You must be signed in to change notification settings - Fork 4
186 lines (159 loc) · 7.02 KB
/
cf_upload.yml
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
name: Upload to Cloudflare Images
on:
push:
branches:
- master
paths:
- "blockchains/**.png"
- "dapps/**.png"
- "blockchains/**.jpg"
- "dapps/**.jpg"
- "blockchains/**.webp"
- "dapps/**.webp"
- "blockchains/**.svg"
- "dapps/**.svg"
workflow_dispatch:
jobs:
upload-cf:
runs-on: ubuntu-latest
if: github.repository_owner == 'DimensionDev'
steps:
- uses: actions/checkout@v2
- name: Upload images
shell: pwsh
env:
CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
ID_PREFIX: "Assets/"
run: |
$imageList = @()
$pageIndex = 1
$retry = 0
while ($true) {
$resp = try {
Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/accounts/$($env:CF_ACCOUNT_ID)/images/v1?page=$($pageIndex)&per_page=100" `
-SkipHeaderValidation -Headers @{ "Authorization" = "Bearer " + $env:CF_API_TOKEN; }
}
catch {
[PSCustomObject]@{
success = $false;
messages = $_.Exception.Response.StatusCode, $_.Exception.Message;
}
}
if ($resp.success) {
if ($resp.result.images.Length -eq 0) {
break
}
$imageList = ($resp.result.images | Select-Object -ExpandProperty id | ForEach-Object { $_.TrimStart($env:ID_PREFIX).Trim() }) + $imageList
$pageIndex++
$retry = 0
}
else {
$retry++
if ($retry -ge 3) {
throw "Error: Fetching failed on Page $pageIndex. $($resp.messages[0]). $($resp.messages[1])"
}
}
}
$imageList = $imageList | Sort-Object | Get-Unique
Write-Host "Success: Fetching remote completed, Total: $($imageList.Length), First: $($imageList | Select-Object -First 1)"
$localList = Get-ChildItem -File -Recurse -Path "./blockchains/", "./dapps/" -Include "*.png", "*.jpg", "*.webp" , "*.svg" `
| Resolve-Path -Relative `
| ForEach-Object { ($_ -Replace "\\", "/").TrimStart("./").Trim() } `
| Sort-Object `
| Get-Unique
Write-Host "Success: Listing local completed, Total: $($localList.Length), First: $($localList | Select-Object -First 1)"
$diffList = Compare-Object -CaseSensitive -ReferenceObject $localList -DifferenceObject $imageList `
| Where-Object { $_.SideIndicator -eq "<=" } `
| Select-Object -ExpandProperty InputObject `
| Sort-Object `
| Get-Unique
Write-Host "Success: Listing difference completed, Total: $($diffList.Length), First: $($diffList | Select-Object -First 1)"
$uploaded = 0
$unprocessed = @()
$sw = [System.Diagnostics.Stopwatch]::new()
foreach ($imagePath in $diffList) {
$sw.Restart()
$resp = try {
Invoke-RestMethod -Method "POST" -Uri "https://api.cloudflare.com/client/v4/accounts/$($env:CF_ACCOUNT_ID)/images/v1" `
-SkipHeaderValidation -Headers @{ "Authorization" = "Bearer " + $env:CF_API_TOKEN; } `
-Form @{
"file" = Get-Item -Path $imagePath;
"id" = "$env:ID_PREFIX" + "$imagePath";
}
} catch {
[PSCustomObject]@{
success = $false;
messages = $_.Exception.Response.StatusCode, $_.Exception.Message;
}
}
if ($resp.success) {
Write-Host "Success: Uploaded, ID: $($env:ID_PREFIX)$imagePath"
$uploaded++
Start-Sleep -Seconds 0.25
}
elseif ($resp.messages[0] -eq 409) {
Write-Host "Warning: Conflict images detected, ID: $($env:ID_PREFIX)$imagePath"
}
elseif ($resp.messages[0] -eq 422) {
$unprocessed += $env:ID_PREFIX + $imagePath
}
else {
Write-Error "Error: Uploading failed, ID: $($env:ID_PREFIX)$imagePath. $($resp.messages[0]). $($resp.messages[1])"
}
$sl = 250 - $sw.ElapsedMilliseconds
if ($sl -gt 0) {
Start-Sleep -Milliseconds $sl
}
}
Write-Host "Success: Images uploaded, Total: $uploaded"
if ($unprocessed.Length -gt 0) {
foreach ($entity in $unprocessed) {
Write-Host "Error: Unprocessable entity, ID: $entity"
}
throw "Error: Unprocessable entities, Total: $($unprocessed.Length)"
}
$obsoList = Compare-Object -CaseSensitive -ReferenceObject $localList -DifferenceObject $imageList `
| Where-Object { $_.SideIndicator -eq "=>" } `
| Select-Object -ExpandProperty InputObject `
| Sort-Object `
| Get-Unique
Write-Host "Success: Listing obsolescence completed, Total: $($obsoList.Length), First: $($obsoList | Select-Object -First 1)"
$deleted = 0
foreach ($imagePath in $obsoList) {
$sw.Restart()
$resp = try {
Invoke-RestMethod -Method "DELETE" -Uri "https://api.cloudflare.com/client/v4/accounts/$($env:CF_ACCOUNT_ID)/images/v1/$($env:ID_PREFIX)$imagePath" `
-SkipHeaderValidation -Headers @{ "Authorization" = "Bearer " + $env:CF_API_TOKEN; }
} catch {
[PSCustomObject]@{
success = $false;
messages = $_.Exception.Response.StatusCode, $_.Exception.Message;
}
}
if ($resp.success) {
Write-Host "Success: Deleted, ID: $($env:ID_PREFIX)$imagePath"
$deleted++
Start-Sleep -Seconds 0.25
}
elseif ($resp.messages[0] -eq 404) {
Write-Host "Warning: Image not found, ID: $($env:ID_PREFIX)$imagePath"
}
elseif ($resp.messages[0] -eq 422) {
$unprocessed += $env:ID_PREFIX + $imagePath
}
else {
Write-Error "Error: Deleting failed, ID: $($env:ID_PREFIX)$imagePath. $($resp.messages[0]). $($resp.messages[1])"
}
$sl = 250 - $sw.ElapsedMilliseconds
if ($sl -gt 0) {
Start-Sleep -Milliseconds $sl
}
}
Write-Host "Success: Images deleted, Total: $deleted"
if ($unprocessed.Length -gt 0) {
foreach ($entity in $unprocessed) {
Write-Host "Error: Unprocessable entity, ID: $entity"
}
throw "Error: Unprocessable entities, Total: $($unprocessed.Length)"
}