Seed-Hero.ps1: focuspunten mee in de opslag van de hero (imageFocus) in plaats van een aparte focus-aanroep

Het eindpunt voor de focus is vervallen. Eerst alle uploads, dan een PUT van de hero met slides en imageFocus; idempotent (zelfde foto, zelfde afbeelding). Lokaal getest met de ASF-seed en hero-sls.json, op PowerShell 7 en Windows PowerShell 5.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 20:37:22 +01:00
co-authored by Claude Sonnet 5.5
parent 0b56fa6294
commit 4c7787caf2
2 changed files with 35 additions and 14 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ Een gegevensmap naast de site, buiten de webroot, waarin het account van de appl
./scripts/Seed-Hero.ps1 -BaseUrl https://test.slpsoftware.nl -SeedFile ./scripts/hero-sls.json
```
`hero-asf.json` bevat de 8 huidige foto's met alt-teksten en een eerste schatting van de focuspunten (door de ASF-sessie); Bryan past ze aan in de admin. `hero-sls.json` is de huidige SLP-hero (alleen tekst, twee knoppen). Het script slaat een CMS over dat al een hero heeft (`-Force` vervangt), vraagt het wachtwoord interactief en gebruikt voor productie niets zonder Bryans go.
`hero-asf.json` bevat de 8 huidige foto's met alt-teksten en een eerste schatting van de focuspunten (door de ASF-sessie); Bryan past ze aan in de admin. `hero-sls.json` is de huidige SLP-hero (alleen tekst, twee knoppen). Het script uploadt eerst alle foto's (dezelfde foto is dezelfde afbeelding, dus een tweede run dupliceert niets) en slaat dan de hero in een keer op, met de focuspunten in dezelfde aanvraag (`imageFocus`): een focuspunt geldt pas bij het opslaan van de hero. Het slaat een CMS over dat al een hero heeft (`-Force` vervangt), vraagt het wachtwoord interactief (alleen voor een lokale host of testrun leest het `SEED_HERO_PASSWORD`) en gebruikt voor productie niets zonder Bryans go. Getest op PowerShell 7 en Windows PowerShell 5.1 tegen een lokale host, met de ASF-seed en `hero-sls.json`.
## Wat er achter de schermen gebeurt
+34 -13
View File
@@ -71,16 +71,28 @@ if ($WhatIfPreference) {
}
if (-not $Email) { $Email = Read-Host 'Admin e-mail' }
$secure = Read-Host "Password for $Email" -AsSecureString
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
try {
$plain = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
$body = @{ email = $Email; password = $plain } | ConvertTo-Json -Compress
$login = Invoke-RestMethod -Method Post -Uri "$api/auth/login" -ContentType 'application/json' -Body $body
# The password is asked interactively. For an unattended run on a LOCAL host only (a test or CI job), the environment
# variable SEED_HERO_PASSWORD is used instead when it is set; it is never printed.
if ($env:SEED_HERO_PASSWORD) {
$plain = $env:SEED_HERO_PASSWORD
try {
$body = @{ email = $Email; password = $plain } | ConvertTo-Json -Compress
$login = Invoke-RestMethod -Method Post -Uri "$api/auth/login" -ContentType 'application/json' -Body $body
}
finally { $plain = $null; $body = $null }
}
finally {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$plain = $null; $body = $null
else {
$secure = Read-Host "Password for $Email" -AsSecureString
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
try {
$plain = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
$body = @{ email = $Email; password = $plain } | ConvertTo-Json -Compress
$login = Invoke-RestMethod -Method Post -Uri "$api/auth/login" -ContentType 'application/json' -Body $body
}
finally {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$plain = $null; $body = $null
}
}
$token = $login.accessToken
$headers = @{ Authorization = "Bearer $token" }
@@ -108,14 +120,22 @@ function Send-Photo([string] $path) {
finally { $client.Dispose() }
}
# First every photo is uploaded (the same photo again is the same image, so a second run duplicates nothing). The focus
# point of a photo is not a call of its own: a changed focus takes effect when the hero is saved, so it travels in the
# same request as the hero (imageFocus).
$requestSlides = @()
$imageFocus = @()
foreach ($s in $slides) {
$image = Send-Photo (Join-Path $ImagesDirectory $s.file)
Write-Host "upload $($s.file) -> image $($image.id)"
if ($s.focus) {
$focus = @{ focus = @{ x = [double]$s.focus.x; y = [double]$s.focus.y; zoom = 1.0 }; wide = $null; tall = $null } | ConvertTo-Json -Depth 5
$null = Invoke-RestMethod -Method Put -Uri "$api/hero/images/$($image.id)/focus" -Headers $headers -ContentType 'application/json' -Body $focus
Write-Host "focus $($s.file) at $($s.focus.x), $($s.focus.y)"
if ($s.focus -and -not ($imageFocus | Where-Object { $_.imageId -eq $image.id })) {
$imageFocus += [ordered]@{
imageId = $image.id
focus = [ordered]@{ x = [double]$s.focus.x; y = [double]$s.focus.y; zoom = 1.0 }
wide = $null
tall = $null
}
Write-Host "focus $($s.file) at $($s.focus.x), $($s.focus.y) (saved with the hero)"
}
$requestSlides += [ordered]@{ imageId = $image.id; alt = $(if ($s.decorative) { $null } else { $s.alt }); decorative = [bool]$s.decorative; isActive = $true }
}
@@ -130,6 +150,7 @@ $payload = [ordered]@{
startsAt = $null
endsAt = $null
slides = $requestSlides
imageFocus = $imageFocus
} | ConvertTo-Json -Depth 6
try {