Skip to content

add cbbtc and clanker (#39) #50

add cbbtc and clanker (#39)

add cbbtc and clanker (#39) #50

Workflow file for this run

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)"
}