From 1b542dee986ec5cbb7c3e65068545ffd5acb393c Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Mon, 16 Nov 2026 22:23:15 +0100 Subject: [PATCH] Hero: grote foto's in de achtergrond robuust en sneller, voortgang en opnieuw proberen; admin zonder inline script - Een decode voor de hele set, per breedte eenmaal geschaald voor WebP en AVIF (6000x4000: 21,1 s -> 6,4 s per set) - Eigen limiet per set (Media:RenderSetTimeoutSeconds 600), harde limiet rond een foto, de renderer stopt nooit door een uitzondering, een mislukte foto wacht tot de volgende ronde en houdt de rij niet op - Logging per foto (start, per variant, einde, fout); voortgang per variant in de admin; nieuwe uitsnede na focuswijziging zichtbaar (oude afbeelding blijft, shimmer); knop Opnieuw proberen - Thema-script als /theme-init.js (CSP script-src 'self'), test en smoke-controle op inline scripts Co-Authored-By: Claude Sonnet 5.5 --- .../assumptions-and-open-questions.md | 4 +- docs/hero-module.md | 3 +- frontend/index.html | 9 +- frontend/public/theme-init.js | 12 ++ frontend/src/csp.test.ts | 28 ++++ .../hero/components/ProcessingStatus.tsx | 51 +++++++- frontend/src/features/hero/mocks/handlers.ts | 13 +- .../src/features/hero/pages/HeroPage.test.tsx | 73 ++++++++++- frontend/src/features/hero/pages/HeroPage.tsx | 6 +- frontend/src/features/hero/services/types.ts | 3 + frontend/src/i18n/locales/en/translation.json | 8 +- frontend/src/i18n/locales/nl/translation.json | 8 +- openapi/slpsoftware.v1.json | 18 +++ scripts/smoke-site.sh | 7 + .../MagickImageProcessor.cs | 120 +++++++++++++++--- .../Media/ImageProcessing.cs | 16 +++ .../HeroBigPhotoTimingTests.cs | 84 ++++++++++++ .../HeroFocusSaveTests.cs | 62 +++++++++ .../Models/HeroModels.cs | 4 +- .../Services/HeroImageService.cs | 34 ++++- .../Services/HeroRenderService.cs | 44 ++++++- .../Services/HeroService.cs | 14 +- 22 files changed, 567 insertions(+), 54 deletions(-) create mode 100644 frontend/public/theme-init.js create mode 100644 frontend/src/csp.test.ts create mode 100644 src/SlpModularCms.Modules.Hero.Tests/HeroBigPhotoTimingTests.cs diff --git a/aidlc-docs/features/hero-module/assumptions-and-open-questions.md b/aidlc-docs/features/hero-module/assumptions-and-open-questions.md index fbd00fc..aa94600 100644 --- a/aidlc-docs/features/hero-module/assumptions-and-open-questions.md +++ b/aidlc-docs/features/hero-module/assumptions-and-open-questions.md @@ -15,7 +15,7 @@ Kept while Bryan was away (2026-11-16): no questions to him, a likely assumption | 9 | Image quality and widths. | WebP 80, AVIF 50, JPEG 82; wide 640 to 2560 and tall 480 to 1280, never upscaled (approved defaults). Bryan's ASF photos are only 1600 or 2048 wide, so larger variants do not exist. | Other numbers or ladders. | `VariantPlanner` | | 10 | Is **AVIF** used on the page? | Only for slides 2 and up on the client; the first image and the preload are WebP only (ASF: a preload cannot say AVIF if supported). | Add `` before the WebP source and a second preload per format (costs a double download in some browsers). | `HeroPictureRenderer` | | 11 | Who may use the Hero screen? | Owner and Administrator, like Offerings. | Owner only: change the route and nav roles. | `router.tsx`, `Sidebar.tsx`, `[Authorize(Policy = "AdminOnly")]` | -| 12 | Upload limits. | 10 MB per file, 64 megapixels, at most 20 uploads per user per hour, 2 images processed at once, 2 GB in total. The proxy allows 15 MB (Bryan). | Other limits via `Media__*` and `RateLimiting__HeroUpload`; a higher file limit also needs the proxy limit. | `MediaOptions`, `ServiceCollectionExtensions` (policy `hero-upload`) | +| 12 | Upload limits. | 10 MB per file, 64 megapixels, at most 100 uploads per user per hour (was 20; raised 2026-11-16 so a batch of 8-10 photos does not use it up), 2 images processed at once, 2 GB in total. The proxy allows 15 MB (Bryan). | Other limits via `Media__*` and `RateLimiting__HeroUpload`; a higher file limit also needs the proxy limit. | `MediaOptions`, `ServiceCollectionExtensions` (policy `hero-upload`) | | 13 | Can the admin **delete an image** itself? | **Decided: no button**; replacing the image of a slide or removing the slide is enough; unused photos go after 7 days. The API (`DELETE /hero/images/{id}`, 409 while in use) exists. | Add a media library screen with delete. | `HeroImagesController.Delete`, `HeroPage.tsx` | | 14 | Wording of the **messages** (Dutch), for example the focus dialog and the validation texts. | My wording; the admin has nl and en. | Edit the texts. | `frontend/src/i18n/locales/*/translation.json` (`hero.*`), `SlpModularCms.Modules.Hero/Texts/*.json` | | 15 | The **button target** rules. | The CMS accepts only `#anchor` (letters, digits, `-`, `_`, starting with a letter), `/path` (no `//`, no spaces) or `https://host/...`; the page renderer is a little wider (ASF rules) because it also copes with data it did not check. | Allow `mailto:` or `tel:` (add to both validators and the renderer, and agree it with the sites). | `ButtonTargetValidator`, `schemas/hero.ts`, `HeroPictureRenderer.IsButtonTarget` | @@ -29,3 +29,5 @@ Kept while Bryan was away (2026-11-16): no questions to him, a likely assumption | 23 | What does the host write for a hero **without any slide** (SLP Software)? | A text-only region in the first HTML (so a CMS change is visible without JavaScript, LCP is the text): `h1.cms-hero-title` with an optional `span.cms-hero-tagline` and `span.cms-hero-title-lines` (title lines separated by `
`, no brand prefix), then `p.cms-hero-lead` (lead, line breaks as `
`) and `p.cms-hero-buttons` with `a.cms-hero-button`. Nothing to preload, and the preload region is left as built. A hero WITH slides but none usable stays empty (ASF unchanged). This is a per-site switch, `Hero:TextOnlyRegion`: on in the committed settings of the SLP site, off by default (so ASF keeps its baked hero when its CMS hero has no slide, as ASF asked). | A separate marker for the text region instead of a switch. | `HeroOptions.TextOnlyRegion`, `HeroPictureRenderer.Render` and `TextOnlyRegion`, `HeroHtmlTransform.ApplyAsync`, `Api.SlpSoftware/appsettings.json`, `HeroTextOnlyTests` | | 24 | Does the data block carry the hero when it has no slides? | Yes (intended): the site reads the hero from the data block, the baked block is `{}`. | n/a | `HeroPageModel`, `HeroDataContributor` | | 25 | **Variants are cut in the background**, not inside the upload or save request (2026-11-16, found on the ASF test host: saving 8 photos took 60 s+ on the Pi and the proxy cut it with a 504). | Upload and save return at once; `HeroRenderService` cuts one image at a time and switches an image to its new set when it is complete (old set stays live meanwhile); an image without a set is not published; a failing image is tried 3 times, then left until the next restart. Measured on a 20-core PC: upload 3.5-8.8 s and save of 8 changed focus points 39.6 s before, 0.1 s and 0.1 s after (the 37.5 s of cutting moved to the background, 4.7 s per photo). On the Pi: about 25-30 s per photo, 4 minutes for 8. | A longer proxy time-out is then not needed; if cutting should be faster, reduce the variant set or the AVIF effort. | `HeroImageService.RenderNextPendingAsync`, `HeroRenderService`, `HeroSaveTimingTests` | +| 26 | **A large photo never finished** (ASF test, 4.4 MB): every variant decoded the whole photo again and each ran under the 20 s decode limit, so on the Pi a variant timed out, the whole set was retried 3 times and the image was given up silently. | One decode for the whole set, a width is resized once for both formats and from the previous width; a job limit of 600 s for the set (the image library clock is set above it); a failing image waits for the next round instead of blocking the queue; progress per variant in the admin and the log; retry button. Measured on a 6000x4000 JPEG (3.1 MB): 21.1 s per set before, 6.4 s after, on a fast PC (thread limit made no difference). On the Pi roughly 6x: about 40 s. | Other limits via `Media__*`; skipping AVIF would save about half of the time again. | `MagickImageProcessor.RenderSet`, `HeroRenderService`, `HeroBigPhotoTimingTests` | +| 27 | The admin page must have **no inline script** (CSP `script-src 'self'`). | The theme script is `/theme-init.js`; a test and the smoke test check it. | n/a | `frontend/public/theme-init.js`, `frontend/src/csp.test.ts`, `scripts/smoke-site.sh` | diff --git a/docs/hero-module.md b/docs/hero-module.md index b8517fb..c850457 100644 --- a/docs/hero-module.md +++ b/docs/hero-module.md @@ -29,8 +29,9 @@ De admin toont alleen een badge Live of Niet live. | `Media__MaxUploadBytes` (10 MB), `Media__MaxPixels` (64 miljoen), `Media__MaxParallelJobs` (2), `Media__DecodeTimeoutSeconds` (20), `Media__MaxTotalBytes` (2 GB), `Media__RetentionDays` (7) | Grenzen en bewaartijd; standaardwaarden zijn bedoeld om te blijven. | | `Hero__TextOnlyRegion` | `true` voor een site waarvan de hero geen afbeeldingen heeft (SLP Software; staat in de committed instellingen van die site): een hero zonder slides wordt dan als tekst (titelregels, lead, knoppen) in de eerste HTML geschreven. Uit (standaard, ook voor ASF): een hero zonder slides laat de pagina ongemoeid. | | `Hero__TitlePrefix` | Merktekst die in de tekst-only-regio als eerste, visueel te verbergen deel van de h1 wordt geschreven (``), zodat crawlers en schermlezers de volledige titel lezen. SLP Software: `"SLP Software: "` (committed instelling); leeg (standaard, ook ASF) schrijft niets. De site verbergt de klasse zelf visueel. | +| `Media__RenderSetTimeoutSeconds` (600), `Media__StripTimeoutSeconds` (60) | De tijd die het knippen van alle varianten van één foto in de achtergrond mag duren, en het verwijderen van metadata bij een upload. Ruim gekozen: een grote foto op een Raspberry Pi kost minuten. | | `Hero__MaxPublishedSlides` | Technische bovengrens op het aantal gepubliceerde slides (standaard 50). | -| `RateLimiting__HeroPublic`, `__MediaPublic`, `__HeroUpload` | Snelheidsbeleid (standaard 120 per 60 s, 600 per 60 s, 20 per uur per gebruiker). | +| `RateLimiting__HeroPublic`, `__MediaPublic`, `__HeroUpload` | Snelheidsbeleid (standaard 120 per 60 s, 600 per 60 s, 100 uploads per uur per gebruiker: een reeks van 8-10 foto's mag de limiet niet opmaken). | ### Pi (test en later productie van SLP) diff --git a/frontend/index.html b/frontend/index.html index 58ffefa..0e34ca2 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -5,13 +5,8 @@ SlpModularCms - + +
diff --git a/frontend/public/theme-init.js b/frontend/public/theme-init.js new file mode 100644 index 0000000..6770ee5 --- /dev/null +++ b/frontend/public/theme-init.js @@ -0,0 +1,12 @@ +// Sets the light or dark theme before the first paint, so the page does not flash in the wrong theme. A file of its own +// (not an inline script) because the host allows scripts from its own origin only (Content-Security-Policy script-src 'self'). +(function () { + var stored = null; + try { + stored = localStorage.getItem('cms-theme'); + } catch (e) { + stored = null; + } + var theme = stored === 'light' || stored === 'dark' ? stored : (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'); + document.documentElement.classList.add(theme); +})(); diff --git a/frontend/src/csp.test.ts b/frontend/src/csp.test.ts new file mode 100644 index 0000000..baac15c --- /dev/null +++ b/frontend/src/csp.test.ts @@ -0,0 +1,28 @@ +import { readFileSync } from 'node:fs'; +import { resolve } from 'node:path'; +import { describe, expect, it } from 'vitest'; + +// The host sends Content-Security-Policy "script-src 'self'": an inline script in the admin page is blocked there, so the +// page must have none (the theme is set by /theme-init.js, a file of its own). +describe('the admin page and the Content-Security-Policy', () => { + const html = readFileSync(resolve(__dirname, '../index.html'), 'utf-8'); + + it('has no inline script', () => { + const inline = [...html.matchAll(/]*)>([\s\S]*?)<\/script>/gi)].filter(([, attributes]) => !/\bsrc\s*=/.test(attributes)); + + expect(inline).toEqual([]); + }); + + it('sets the theme from a file of its own, before the app', () => { + expect(html.indexOf('src="/theme-init.js"')).toBeGreaterThan(-1); + expect(html.indexOf('src="/theme-init.js"')).toBeLessThan(html.indexOf('src="/src/main.tsx"')); + const script = readFileSync(resolve(__dirname, '../public/theme-init.js'), 'utf-8'); + expect(script).toContain("localStorage.getItem('cms-theme')"); + expect(script).toContain('classList.add'); + }); + + it('has no inline event handlers or javascript: links either', () => { + expect(html).not.toMatch(/\son[a-z]+\s*=/i); + expect(html).not.toMatch(/javascript:/i); + }); +}); diff --git a/frontend/src/features/hero/components/ProcessingStatus.tsx b/frontend/src/features/hero/components/ProcessingStatus.tsx index b20c318..9213f55 100644 --- a/frontend/src/features/hero/components/ProcessingStatus.tsx +++ b/frontend/src/features/hero/components/ProcessingStatus.tsx @@ -23,6 +23,11 @@ export function ProcessingStatus({ hero }: { hero: HeroAdminDto }) { const done = total - waiting.length; const failed = waiting.filter((s) => s.image.failed); const working = waiting.length - failed.length; + // The photo the server is busy with right now, and how far along it is (a photo that waits has no step yet). + const busyIndex = waiting.findIndex((s) => s.image.renderTotal); + const busy = busyIndex === -1 ? null : waiting[busyIndex].image; + const fraction = busy && busy.renderTotal ? (busy.renderDone ?? 0) / busy.renderTotal : 0; + const percent = total === 0 ? 0 : Math.min(100, ((done + fraction) / total) * 100); return (
@@ -42,9 +47,26 @@ export function ProcessingStatus({ hero }: { hero: HeroAdminDto }) { className="h-2 w-full overflow-hidden rounded bg-muted" data-testid="hero-processing-bar" > -
+
+ {busy && ( +

+ {t('hero.processing.current', { n: done + 1, total, step: busy.renderDone ?? 0, steps: busy.renderTotal })} +

+ )}

{t('hero.processing.explain')}

+

+ {t('hero.processing.stuck')}{' '} + +

)} {failed.length > 0 && ( @@ -72,7 +94,7 @@ export function ProcessingStatus({ hero }: { hero: HeroAdminDto }) { * text. It lives in the row of the slide, so it stays mounted (and the animation keeps running) while the page asks the * server again every few seconds, and it disappears when the photo is ready. */ -export function PhotoPlaceholder({ failed, testId }: { failed: boolean; testId: string }) { +export function PhotoPlaceholder({ failed, testId, step }: { failed: boolean; testId: string; step?: { done: number; total: number } | null }) { const { t } = useTranslation(); return (
{failed ? null : } {failed ? t('hero.slides.failed') : t('hero.slides.processing')} + {!failed && step ? {step.done}/{step.total} : null} +
+ ); +} + +/** + * The photo of a slide whose crop is being made again (a changed focus point): the old picture stays visible underneath, + * with the same calm shimmer and a short text, so it is clear that the change is not on the website yet. + */ +export function RecutThumbnail({ src, failed, testId, step }: { src: string; failed: boolean; testId: string; step?: { done: number; total: number } | null }) { + const { t } = useTranslation(); + return ( +
+ + + {failed ? null : } + {failed ? t('hero.slides.failed') : t('hero.slides.recut')} + {!failed && step ? {step.done}/{step.total} : null} +
); } diff --git a/frontend/src/features/hero/mocks/handlers.ts b/frontend/src/features/hero/mocks/handlers.ts index b861807..8c9824d 100644 --- a/frontend/src/features/hero/mocks/handlers.ts +++ b/frontend/src/features/hero/mocks/handlers.ts @@ -59,6 +59,7 @@ export const resetMockHero = (): void => { uploads = 0; uploadFailure = null; retried = []; + recutting = new Set(); }; export const setMockHero = (next: HeroAdminDto): void => { @@ -67,6 +68,15 @@ export const setMockHero = (next: HeroAdminDto): void => { }; let retried: string[] = []; +let recutting = new Set(); +/** The background work of the mock server is done: images whose crop was being made are ready. */ +export const finishMockRecut = (): void => { + recutting.forEach((id) => { + if (images[id]) images[id] = { ...images[id], processing: false }; + hero = { ...hero, slides: hero.slides.map((s) => (s.imageId === id ? { ...s, image: { ...s.image, processing: false } } : s)) }; + }); + recutting = new Set(); +}; export const getRetriedImages = (): string[] => retried; export const getSavedHeroRequests = (): HeroSaveRequest[] => saved; @@ -84,7 +94,8 @@ export const heroHandlers = [ // A changed focus takes effect with the save of the hero. for (const edit of body.imageFocus ?? []) { const current = images[edit.imageId] ?? hero.slides.find((s) => s.imageId === edit.imageId)?.image ?? seedImage(edit.imageId); - images[edit.imageId] = { ...current, focus: edit.focus, wideFocus: edit.wide, tallFocus: edit.tall }; + images[edit.imageId] = { ...current, focus: edit.focus, wideFocus: edit.wide, tallFocus: edit.tall, processing: true }; + recutting.add(edit.imageId); } hero = { ...hero, diff --git a/frontend/src/features/hero/pages/HeroPage.test.tsx b/frontend/src/features/hero/pages/HeroPage.test.tsx index bbc2edc..17d6b23 100644 --- a/frontend/src/features/hero/pages/HeroPage.test.tsx +++ b/frontend/src/features/hero/pages/HeroPage.test.tsx @@ -5,7 +5,7 @@ import { http, HttpResponse } from 'msw'; import { server } from '@/mocks/server'; import { API_BASE } from '@/features/auth/mocks/fixtures'; import { renderApp, mockAuthenticated } from '@/test/utils'; -import { emptyHero, failNextUpload, getRetriedImages, getSavedHeroRequests, resetMockHero, setMockHero, slide } from '@/features/hero/mocks/handlers'; +import { emptyHero, failNextUpload, finishMockRecut, getRetriedImages, getSavedHeroRequests, resetMockHero, setMockHero, slide } from '@/features/hero/mocks/handlers'; import { _resetSetupStatusCache } from '@/router'; const T = { timeout: 10000 }; @@ -665,3 +665,74 @@ describe('Order by dragging, wording and calm waiting', () => { expect(screen.getByTestId('hero-slide-0-processing')).toBe(before); }, 20000); }); + + +describe('Progress of a photo that is being prepared', () => { + beforeEach(() => { + resetMockHero(); + }); + + it('shows which photo is being worked on and at which step', async () => { + const waiting = (n: number, step?: [number, number]) => ({ + ...slide(n), + image: { ...slide(n).image, variants: [], src: '', processing: true, renderDone: step?.[0] ?? null, renderTotal: step?.[1] ?? null }, + }); + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), waiting(2, [7, 16]), waiting(3)] }); + + await openPage(); + + expect(await screen.findByTestId('hero-processing-current', {}, T)).toHaveTextContent(/foto 2 van 3.*stap 7 van 16|photo 2 of 3.*step 7 of 16/); + expect(screen.getByTestId('hero-slide-1-processing-step')).toHaveTextContent('7/16'); + expect(screen.queryByTestId('hero-slide-2-processing-step')).not.toBeInTheDocument(); + }); +}); + + +describe('A changed focus point makes a new crop in the background', () => { + beforeEach(() => { + resetMockHero(); + }); + + it('shows that the new crop is not live yet (old picture underneath), counts it in the total, and clears it when done', async () => { + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] }); + await openPage(); + await userEvent.click(screen.getByTestId('hero-slide-0-focus')); + fireEvent.change(await screen.findByTestId('focus-x', {}, T), { target: { value: '25' } }); + await userEvent.click(screen.getByTestId('focus-save')); + await userEvent.click(screen.getByTestId('hero-save')); + + const recut = await screen.findByTestId('hero-slide-0-recut', {}, T); + expect(recut).toHaveClass('hero-shimmer'); + expect(recut.querySelector('img')).not.toBeNull(); + expect(recut).toHaveTextContent(/Nieuwe uitsnede|new crop/); + expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/1 van 2|1 of 2/); + expect(screen.queryByTestId('hero-slide-1-recut')).not.toBeInTheDocument(); + + // The server finishes the new crop; the page asks again by itself. + finishMockRecut(); + await waitFor(() => expect(screen.queryByTestId('hero-slide-0-recut')).not.toBeInTheDocument(), { timeout: 12000 }); + expect(screen.queryByTestId('hero-processing')).not.toBeInTheDocument(); + expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled(); + }, 25000); + + it('shows a failed new crop with the retry', async () => { + const failed = { ...slide(1), image: { ...slide(1).image, processing: true, failed: true } }; + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [failed] }); + + await openPage(); + + expect(await screen.findByTestId('hero-slide-0-recut', {}, T)).toHaveTextContent(/Mislukt|Failed/); + await userEvent.click(screen.getByTestId('hero-processing-retry')); + await waitFor(() => expect(getRetriedImages()).toEqual([failed.imageId]), T); + }); + + it('offers to try again for a photo that takes long', async () => { + const waiting = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true } }; + setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [waiting] }); + await openPage(); + + await userEvent.click(await screen.findByTestId('hero-processing-retry-all', {}, T)); + + await waitFor(() => expect(getRetriedImages()).toEqual([waiting.imageId]), T); + }); +}); diff --git a/frontend/src/features/hero/pages/HeroPage.tsx b/frontend/src/features/hero/pages/HeroPage.tsx index e443062..364f00b 100644 --- a/frontend/src/features/hero/pages/HeroPage.tsx +++ b/frontend/src/features/hero/pages/HeroPage.tsx @@ -14,7 +14,7 @@ import { FieldError } from '@/components/ui/FieldError'; import { FormErrorBanner } from '@/components/ui/FormErrorBanner'; import { NetworkError, ProblemDetailsError } from '@/lib/api-client'; import { FocusDialog } from '../components/FocusDialog'; -import { PhotoPlaceholder, ProcessingStatus } from '../components/ProcessingStatus'; +import { PhotoPlaceholder, ProcessingStatus, RecutThumbnail } from '../components/ProcessingStatus'; import { SortableSlide } from '../components/SortableSlide'; import { UnsavedChangesGuard } from '../components/UnsavedChangesGuard'; import { UploadProgress, type UploadItem } from '../components/UploadProgress'; @@ -425,7 +425,9 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) { return ( {image && image.variants.length === 0 ? ( - + + ) : image?.processing ? ( + ) : image ? ( ) : ( diff --git a/frontend/src/features/hero/services/types.ts b/frontend/src/features/hero/services/types.ts index 55cf116..6922051 100644 --- a/frontend/src/features/hero/services/types.ts +++ b/frontend/src/features/hero/services/types.ts @@ -27,6 +27,9 @@ export interface HeroImageDto { processing: boolean; /** The background renderer gave up on this image after a few tries; it can be retried. */ failed: boolean; + /** While the image is being cut: the step it is at, out of how many (null when it is not being cut right now). */ + renderDone?: number | null; + renderTotal?: number | null; } export interface HeroButtonDto { diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index 66e4d04..718b1b3 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -663,7 +663,9 @@ "altHint": "Describes the photo for people who cannot see it and for search engines.", "decorativeHint": "Choose this when the photo adds nothing to the content; a screen reader then skips it.", "drag": "Move image {{n}}", - "dragInstructions": "Press space to pick up the image, use the arrow keys to move it and press space again to drop it. Escape cancels." + "dragInstructions": "Press space to pick up the image, use the arrow keys to move it and press space again to drop it. Escape cancels.", + "recut": "Making the new crop…", + "recutHint": "You still see the old crop. The change is only on the website when this is done." }, "processing": { "count": "{{done}} of {{total}} photos processed", @@ -672,7 +674,9 @@ "failed_one": "Processing {{count}} photo failed.", "failed_other": "Processing {{count}} photos failed.", "failedHint": "The server tried three times. Try again, or upload the photo once more (the same photo is the same image).", - "retry": "Try again" + "retry": "Try again", + "current": "Working on photo {{n}} of {{total}}: step {{step}} of {{steps}}. A large photo can take a few minutes.", + "stuck": "Taking long?" }, "focus": { "title": "Focus and crop", diff --git a/frontend/src/i18n/locales/nl/translation.json b/frontend/src/i18n/locales/nl/translation.json index 1423607..fcb9733 100644 --- a/frontend/src/i18n/locales/nl/translation.json +++ b/frontend/src/i18n/locales/nl/translation.json @@ -663,7 +663,9 @@ "altHint": "Beschrijft de foto voor mensen die hem niet kunnen zien en voor zoekmachines.", "decorativeHint": "Kies dit als de foto niets toevoegt aan de inhoud; dan leest een schermlezer hem niet voor.", "drag": "Verplaats afbeelding {{n}}", - "dragInstructions": "Druk op spatie om de afbeelding op te pakken, gebruik de pijltjestoetsen om hem te verplaatsen en druk nogmaals op spatie om hem neer te zetten. Escape annuleert." + "dragInstructions": "Druk op spatie om de afbeelding op te pakken, gebruik de pijltjestoetsen om hem te verplaatsen en druk nogmaals op spatie om hem neer te zetten. Escape annuleert.", + "recut": "Nieuwe uitsnede wordt gemaakt…", + "recutHint": "Je ziet nog de oude uitsnede. De wijziging staat pas op de website als dit klaar is." }, "processing": { "count": "{{done}} van {{total}} foto’s verwerkt", @@ -672,7 +674,9 @@ "failed_one": "Het verwerken van {{count}} foto is mislukt.", "failed_other": "Het verwerken van {{count}} foto’s is mislukt.", "failedHint": "De server heeft het drie keer geprobeerd. Probeer het opnieuw, of upload de foto nog een keer (dezelfde foto is dezelfde afbeelding).", - "retry": "Opnieuw proberen" + "retry": "Opnieuw proberen", + "current": "Bezig met foto {{n}} van {{total}}: stap {{step}} van {{steps}}. Een grote foto kan enkele minuten duren.", + "stuck": "Duurt het lang?" }, "focus": { "title": "Focus en uitsnede", diff --git a/openapi/slpsoftware.v1.json b/openapi/slpsoftware.v1.json index 0a49c57..1e6f93e 100644 --- a/openapi/slpsoftware.v1.json +++ b/openapi/slpsoftware.v1.json @@ -702,6 +702,24 @@ "default": false, "type": "boolean" }, + "renderDone": { + "format": "int32", + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ] + }, + "renderTotal": { + "format": "int32", + "pattern": "^-?(?:0|[1-9]\\d*)$", + "type": [ + "null", + "integer", + "string" + ] + }, "src": { "type": "string" }, diff --git a/scripts/smoke-site.sh b/scripts/smoke-site.sh index 1bc9820..4904a78 100644 --- a/scripts/smoke-site.sh +++ b/scripts/smoke-site.sh @@ -57,6 +57,13 @@ if [ "$hero" = "200" ] || [ "$hero" = "204" ]; then pass "public hero (/api/v1/h expect_status /api/v1/hero/all 401 "the hero management read is protected" expect_status /media/hero/0123456789abcdef/wide-960.webp 404 "media that is not stored is a plain 404" +# The admin page must have no inline script: the host only allows scripts from its own origin (script-src 'self'). +admin_html="$(curl_get "$base/admin/" || true)" +if printf '%s' "$admin_html" | grep -q 'id="root"'; then + if printf '%s' "$admin_html" | grep -qiP ']*src=)[^>]*>'; then fail "the admin page has an inline script (blocked by the Content-Security-Policy)"; else pass "the admin page has no inline script"; fi + expect_status /admin/theme-init.js 200 "the theme script is a file of its own" +fi + # A test host is never indexed. expect_header / X-Robots-Tag noindex "noindex on the website" expect_body /robots.txt "Disallow: /" "robots.txt disallows everything" diff --git a/src/SlpModularCms.Core.Media.Magick/MagickImageProcessor.cs b/src/SlpModularCms.Core.Media.Magick/MagickImageProcessor.cs index a4d9f17..3028547 100644 --- a/src/SlpModularCms.Core.Media.Magick/MagickImageProcessor.cs +++ b/src/SlpModularCms.Core.Media.Magick/MagickImageProcessor.cs @@ -38,8 +38,11 @@ public sealed class MagickImageProcessor : IImageProcessor public Task RenderAsync(ReadOnlyMemory data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken) => RunAsync(() => Render(data, declared, variant), cancellationToken); + public Task> RenderSetAsync(ReadOnlyMemory data, ImageFormat declared, IReadOnlyList variants, Action? progress, CancellationToken cancellationToken) + => RunAsync(() => RenderSet(data, declared, variants, progress, cancellationToken), cancellationToken, _options.RenderSetTimeoutSeconds); + public Task StripMetadataAsync(ReadOnlyMemory data, ImageFormat declared, CancellationToken cancellationToken) - => RunAsync(() => StripMetadata(data, declared), cancellationToken); + => RunAsync(() => StripMetadata(data, declared), cancellationToken, _options.StripTimeoutSeconds); public async Task SelfTestAsync(CancellationToken cancellationToken) { @@ -75,12 +78,12 @@ public sealed class MagickImageProcessor : IImageProcessor // ----- work done on a pool thread with a time limit (the native limit below stops a runaway decode) ------------ - private async Task RunAsync(Func work, CancellationToken cancellationToken) + private async Task RunAsync(Func work, CancellationToken cancellationToken, int? timeoutSeconds = null) { try { return await Task.Run(work, cancellationToken) - .WaitAsync(TimeSpan.FromSeconds(Math.Max(1, _options.DecodeTimeoutSeconds)), cancellationToken); + .WaitAsync(TimeSpan.FromSeconds(Math.Max(1, timeoutSeconds ?? _options.DecodeTimeoutSeconds)), cancellationToken); } catch (TimeoutException) { @@ -133,33 +136,115 @@ public sealed class MagickImageProcessor : IImageProcessor private RenderedImage Render(ReadOnlyMemory data, ImageFormat declared, VariantSpec variant) { - var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 }; - using var image = ReadStrict(data, settings); + using var image = DecodeOriented(data, declared); + return Cut(image, variant); + } + /// + /// One decode for the whole set, and as little repeated work as possible: per crop the widths are cut from large to + /// small, each from the previous (smaller input than the full picture), and a width is resized once for all of its + /// formats (WebP and AVIF are encoded from the same pixels). + /// + private IReadOnlyList RenderSet(ReadOnlyMemory data, ImageFormat declared, IReadOnlyList variants, Action? progress, CancellationToken cancellationToken) + { + using var image = DecodeOriented(data, declared); + var results = new RenderedImage?[variants.Count]; + var done = 0; + + foreach (var group in variants.Select((spec, index) => (spec, index)).GroupBy(x => (x.spec.Ratio.Name, x.spec.Crop))) + { + var crop = group.Key.Crop; + if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1 + || crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height) + { + throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image."); + } + + // A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding. + IMagickImage current = image.Clone(); + try + { + current.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height)); + current.ResetPage(); + ToSrgb(current); + current.Strip(); + + foreach (var width in group.GroupBy(x => x.spec.Width).OrderByDescending(g => g.Key)) + { + // A cancelled or timed-out job stops at the next width; the native library itself cannot be interrupted. + cancellationToken.ThrowIfCancellationRequested(); + var first = width.First().spec; + if (current.Width != (uint)first.Width || current.Height != (uint)first.Height) + { + current.Resize(new MagickGeometry((uint)first.Width, (uint)first.Height) { IgnoreAspectRatio = true }); + } + + foreach (var (spec, index) in width) + { + results[index] = Encode(current, spec); + progress?.Invoke(++done, variants.Count); + } + } + } + finally + { + current.Dispose(); + } + } + + return results.Select(r => r!).ToList(); + } + + /// Writes already cut and resized pixels in the format and quality of the variant. + private static RenderedImage Encode(IMagickImage pixels, VariantSpec variant) + { + pixels.Quality = (uint)variant.Quality; + if (variant.Format == ImageFormat.Avif) + { + pixels.Settings.SetDefine(MagickFormat.Avif, "speed", "6"); + } + + using var stream = new MemoryStream(); + pixels.Write(stream, ToMagick(variant.Format)); + return new RenderedImage(stream.ToArray(), (int)pixels.Width, (int)pixels.Height, variant.Format); + } + + private static MagickImage DecodeOriented(ReadOnlyMemory data, ImageFormat declared) + { + var settings = new MagickReadSettings { Format = ToMagick(declared), FrameIndex = 0, FrameCount = 1 }; + var image = ReadStrict(data, settings); image.AutoOrient(); + return image; + } + + /// Crops a copy of the decoded picture, resizes and encodes it. + private static RenderedImage Cut(IMagickImage source, VariantSpec variant) + { var crop = variant.Crop; if (crop.X < 0 || crop.Y < 0 || crop.Width < 1 || crop.Height < 1 - || crop.X + crop.Width > image.Width || crop.Y + crop.Height > image.Height) + || crop.X + crop.Width > source.Width || crop.Y + crop.Height > source.Height) { throw new ImageRejectedException(ImageRejection.Corrupt, "The planned crop does not fit the decoded image."); } - image.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height)); - image.ResetPage(); - image.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true }); - ToSrgb(image); - image.Strip(); - image.Quality = (uint)variant.Quality; + // A copy of the decoded pixels is a plain memory copy, cheap next to decoding or encoding. + using var part = source.Clone(); + part.Crop(new MagickGeometry(crop.X, crop.Y, (uint)crop.Width, (uint)crop.Height)); + part.ResetPage(); + part.Resize(new MagickGeometry((uint)variant.Width, (uint)variant.Height) { IgnoreAspectRatio = true }); + ToSrgb(part); + part.Strip(); + part.Quality = (uint)variant.Quality; var format = ToMagick(variant.Format); if (variant.Format == ImageFormat.Avif) { - image.Settings.SetDefine(MagickFormat.Avif, "speed", "6"); + part.Settings.SetDefine(MagickFormat.Avif, "speed", "6"); } using var stream = new MemoryStream(); - image.Write(stream, format); - return new RenderedImage(stream.ToArray(), (int)image.Width, (int)image.Height, variant.Format); + part.Write(stream, format); + return new RenderedImage(stream.ToArray(), (int)part.Width, (int)part.Height, variant.Format); } private static byte[]? StripMetadata(ReadOnlyMemory data, ImageFormat declared) @@ -227,7 +312,7 @@ public sealed class MagickImageProcessor : IImageProcessor return image; } - private static void ToSrgb(MagickImage image) + private static void ToSrgb(IMagickImage image) { try { @@ -257,7 +342,8 @@ public sealed class MagickImageProcessor : IImageProcessor ResourceLimits.Width = (ulong)Math.Max(1, options.MaxSide); ResourceLimits.Height = (ulong)Math.Max(1, options.MaxSide); ResourceLimits.Memory = 1UL * 1024 * 1024 * 1024; - ResourceLimits.Time = (ulong)Math.Max(1, options.DecodeTimeoutSeconds); + // The library's own clock runs per picture from its creation: it must not end a set that the job timeout allows. + ResourceLimits.Time = (ulong)Math.Max(1, Math.Max(options.DecodeTimeoutSeconds, Math.Max(options.StripTimeoutSeconds, options.RenderSetTimeoutSeconds)) + 30); _limitsSet = true; } } diff --git a/src/SlpModularCms.Core/Media/ImageProcessing.cs b/src/SlpModularCms.Core/Media/ImageProcessing.cs index e85d3cd..3687d34 100644 --- a/src/SlpModularCms.Core/Media/ImageProcessing.cs +++ b/src/SlpModularCms.Core/Media/ImageProcessing.cs @@ -50,6 +50,15 @@ public sealed class MediaOptions public int DecodeTimeoutSeconds { get; set; } = 20; + /// The time allowed to remove metadata from an upload (it decodes and writes the whole picture again). + public int StripTimeoutSeconds { get; set; } = 60; + + /// + /// The time allowed to cut ALL variants of one photo in the background (one decode, then every variant). Generous on + /// purpose: a large photo on a Raspberry Pi needs minutes; a runaway job is stopped after this. + /// + public int RenderSetTimeoutSeconds { get; set; } = 600; + /// How long a free processing slot is awaited before the answer is "busy". public int BusyWaitSeconds { get; set; } = 3; @@ -81,6 +90,13 @@ public interface IImageProcessor /// Task RenderAsync(ReadOnlyMemory data, ImageFormat declared, VariantSpec variant, CancellationToken cancellationToken); + /// + /// Cuts all variants of a photo from ONE decode (the picture is decoded and oriented once, then every variant is + /// cropped, resized and encoded from it). is called with (variants done, variants in total) + /// after every variant. Stops between variants when cancelled. The result has the order of . + /// + Task> RenderSetAsync(ReadOnlyMemory data, ImageFormat declared, IReadOnlyList variants, Action? progress, CancellationToken cancellationToken); + /// /// Removes metadata from an original that carries any, keeping the pixels as they are as far as the format /// allows. Returns null when there is nothing to remove (the original is stored as received). diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroBigPhotoTimingTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroBigPhotoTimingTests.cs new file mode 100644 index 0000000..89acc61 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroBigPhotoTimingTests.cs @@ -0,0 +1,84 @@ +using System.Diagnostics; +using ImageMagick; +using SlpModularCms.Core.Media; +using Xunit.Abstractions; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// +/// A measurement, not a check: where the time goes when a large photo (about 6000x4000, a few MB) is cut into its set. +/// Runs only when HERO_TIMING_BIG=1; otherwise it passes at once. Set MAGICK_THREAD_LIMIT=1 to look at a single core. +/// +public class HeroBigPhotoTimingTests(ITestOutputHelper output) +{ + private static byte[] BigPhoto(int width, int height) + { + // Smooth gradients with a little noise: compresses like a photo (a few MB), unlike pure noise. + using var image = new MagickImage(MagickColors.SteelBlue, (uint)width, (uint)height); + image.AddNoise(NoiseType.Gaussian, 0.4); + using var clouds = new MagickImage("plasma:fractal", (uint)(width / 4), (uint)(height / 4)); + clouds.Resize((uint)width, (uint)height); + image.Composite(clouds, CompositeOperator.Blend, "40"); + image.Quality = 88; + using var stream = new MemoryStream(); + image.Write(stream, MagickFormat.Jpeg); + return stream.ToArray(); + } + + [Fact] + public async Task MeasureASetOfABigPhoto() + { + if (Environment.GetEnvironmentVariable("HERO_TIMING_BIG") != "1") + { + return; + } + + var width = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_W") ?? "6000"); + var height = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_H") ?? "4000"); + // Made before the processor exists: its resource limits (20 s) would stop the making of the fixture itself. + var bytes = BigPhoto(width, height); + using var rig = new HeroTestRig(); + if (int.TryParse(Environment.GetEnvironmentVariable("HERO_TIMING_THREADS"), out var threads)) + { + ResourceLimits.Thread = (ulong)threads; + output.WriteLine($"image library threads: {ResourceLimits.Thread}"); + } + output.WriteLine($"photo {width}x{height}, {bytes.Length / 1024.0 / 1024.0:0.0} MB"); + + var watch = Stopwatch.StartNew(); + var info = await rig.Processor.InspectAsync(bytes, ImageFormat.Jpeg, default); + output.WriteLine($"inspect: {watch.ElapsedMilliseconds} ms ({info.Width}x{info.Height})"); + + var ratios = new[] { Ratio.Wide, Ratio.Tall } + .Select(r => new RatioPlan(r, new FocusSetting(0.5, 0.5, 1).Normalize(), VariantPlanner.DefaultWidths(r))) + .ToList(); + var specs = VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput); + var byFormat = new Dictionary>(); + var total = Stopwatch.StartNew(); + foreach (var spec in specs) + { + watch.Restart(); + var rendered = await rig.Processor.RenderAsync(bytes, ImageFormat.Jpeg, spec, default); + var ms = watch.ElapsedMilliseconds; + if (!byFormat.TryGetValue(spec.Format, out var list)) + { + byFormat[spec.Format] = list = []; + } + + list.Add(ms); + output.WriteLine($" {spec.Ratio.Name,-4} {spec.Width,5} {spec.Format,-5}: {ms,6} ms, {rendered.Bytes.Length / 1024} KB"); + } + + output.WriteLine($"per variant, one decode each: {specs.Count} variants in {total.ElapsedMilliseconds} ms"); + foreach (var (format, list) in byFormat) + { + output.WriteLine($" {format}: {list.Count} variants, mean {list.Average():0} ms, total {list.Sum()} ms"); + } + + // The whole set from one decode. + watch.Restart(); + var steps = 0; + var set = await rig.Processor.RenderSetAsync(bytes, ImageFormat.Jpeg, specs, (_, _) => steps++, default); + output.WriteLine($"whole set from one decode: {specs.Count} variants in {watch.ElapsedMilliseconds} ms ({steps} progress steps, {set.Sum(r => r.Bytes.Length) / 1024} KB)"); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroFocusSaveTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroFocusSaveTests.cs index 9504ed6..9dc98ea 100644 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroFocusSaveTests.cs +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroFocusSaveTests.cs @@ -202,4 +202,66 @@ public class HeroFocusSaveTests limits.MaxUploadBytes.Should().Be(rig.Media.MaxUploadBytes); limits.MaxMegapixels.Should().Be((int)(rig.Media.MaxPixels / 1_000_000)); } + + [Fact] + public async Task AnImageThatCannotBeCut_FailsWithItsId_AndTheOthersAreStillCut() + { + using var rig = new HeroTestRig(); + var broken = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 1)); + var fine = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 2)); + // The original of the first photo is gone (a lost file): cutting it cannot work. + await rig.Store.DeleteAsync(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id)).OriginalKey, default); + + var failure = await Assert.ThrowsAsync(() => rig.Images.RenderNextPendingAsync(new HashSet(), default)); + failure.ImageId.Should().Be(Guid.Parse(broken.Id)); + + var handled = await rig.Images.RenderNextPendingAsync(new HashSet { failure.ImageId }, default); + + handled.Should().Be(Guid.Parse(fine.Id)); + HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(fine.Id))).Should().BeFalse(); + HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id))).Should().BeTrue(); + } + + [Fact] + public async Task ASetFromOneDecode_GivesTheSameSizesAndFormats_AsCuttingEveryVariantOnItsOwn_AndReportsEveryStep() + { + using var rig = new HeroTestRig(); + var photo = HeroTestRig.Photo(2400, 1600); + var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default); + var ratios = new[] { SlpModularCms.Core.Media.Ratio.Wide, SlpModularCms.Core.Media.Ratio.Tall } + .Select(r => new SlpModularCms.Core.Media.RatioPlan(r, new SlpModularCms.Core.Media.FocusSetting(0.3, 0.6, 1.5).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(r))) + .ToList(); + var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput); + var steps = new List<(int Done, int Total)>(); + + var set = await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (done, total) => steps.Add((done, total)), default); + + set.Should().HaveCount(specs.Count); + for (var i = 0; i < specs.Count; i++) + { + var single = await rig.Processor.RenderAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs[i], default); + (set[i].Width, set[i].Height, set[i].Format).Should().Be((single.Width, single.Height, single.Format), $"variant {i}"); + } + + steps.Should().HaveCount(specs.Count); + steps.Select(x => x.Done).Should().BeInAscendingOrder(); + steps.Last().Should().Be((specs.Count, specs.Count)); + } + + [Fact] + public async Task ACancelledSet_StopsBetweenVariants() + { + using var rig = new HeroTestRig(); + var photo = HeroTestRig.Photo(2400, 1600); + var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default); + var ratios = new[] { new SlpModularCms.Core.Media.RatioPlan(SlpModularCms.Core.Media.Ratio.Wide, new SlpModularCms.Core.Media.FocusSetting(0.5, 0.5, 1).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(SlpModularCms.Core.Media.Ratio.Wide)) }; + var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput); + using var cts = new CancellationTokenSource(); + var done = 0; + + var act = async () => await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (_, _) => { done++; cts.Cancel(); }, cts.Token); + + await act.Should().ThrowAsync(); + done.Should().BeLessThan(specs.Count); + } } diff --git a/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs b/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs index fa2fb6c..f74c5c5 100644 --- a/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs +++ b/src/SlpModularCms.Modules.Hero/Models/HeroModels.cs @@ -25,7 +25,9 @@ public record HeroImageDto( string Src, IReadOnlyList Variants, bool Processing = false, - bool Failed = false); + bool Failed = false, + int? RenderDone = null, + int? RenderTotal = null); public record HeroButtonDto(string Label, string Target); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs index be6076c..cda2e20 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs @@ -40,6 +40,9 @@ public interface IHeroImageService /// void ApplyFocus(HeroImage image, HeroFocusRequest request); + /// The variant the renderer is busy with: (done, total), or null when the image is not being cut right now. + (int Done, int Total)? RenderProgress(Guid imageId); + /// True when the background renderer gave up on the image after a few failed tries (until a retry or a restart). bool IsRenderFailed(Guid imageId); @@ -160,6 +163,8 @@ public class HeroImageService( public bool IsRenderFailed(Guid imageId) => signal?.IsGivenUp(imageId) ?? false; + public (int Done, int Total)? RenderProgress(Guid imageId) => signal?.GetProgress(imageId); + public async Task RetryRenderAsync(Guid imageId, CancellationToken cancellationToken) { _ = await repository.GetImageAsync(imageId, cancellationToken) ?? throw new HeroImageNotFoundException(); @@ -181,8 +186,13 @@ public class HeroImageService( } catch (Exception exception) when (exception is not OperationCanceledException) { + logger.LogError(exception, "Hero image {ImageId}: cutting the variants failed.", image.Id); throw new HeroImageRenderException(image.Id, exception); } + finally + { + signal?.ClearProgress(image.Id); + } } private async Task RenderAsync(HeroImage image, CancellationToken cancellationToken) @@ -193,6 +203,7 @@ public class HeroImageService( Dictionary files; List variants; var watch = System.Diagnostics.Stopwatch.StartNew(); + logger.LogInformation("Hero image {ImageId}: cutting the variants starts ({Width}x{Height}, {Bytes} bytes, {Reason}).", image.Id, image.Width, image.Height, image.Bytes, image.Variants.Count == 0 ? "new" : "focus changed"); using (await gate.EnterAsync(cancellationToken)) { var file = await store.OpenReadAsync(image.OriginalKey, cancellationToken) @@ -204,7 +215,13 @@ public class HeroImageService( } var storedFormat = ImageFormatExtensions.FromExtension(Path.GetExtension(image.OriginalKey).TrimStart('.')); - (files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, cancellationToken); + var lastStep = watch.Elapsed; + (files, variants) = await RenderSetAsync(image, memory.ToArray(), storedFormat, (done, total) => + { + signal?.SetProgress(image.Id, done, total); + logger.LogInformation("Hero image {ImageId}: variant {Done} of {Total} in {Milliseconds:0} ms.", image.Id, done, total, (watch.Elapsed - lastStep).TotalMilliseconds); + lastStep = watch.Elapsed; + }, cancellationToken); } await store.PutSetAsync(HeroMapping.Area, target, files, cancellationToken); @@ -260,22 +277,25 @@ public class HeroImageService( assign((f.X, f.Y, f.Zoom)); } - /// Cuts and encodes every planned variant of the image at its current focus. Returns the files by name and the variant list. - private async Task<(Dictionary Files, List Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, CancellationToken cancellationToken) + /// Cuts and encodes every planned variant of the image at its current focus (one decode for the whole set). + private async Task<(Dictionary Files, List Variants)> RenderSetAsync(HeroImage image, byte[] original, ImageFormat storedFormat, Action? progress, CancellationToken cancellationToken) { var ratios = new[] { Ratio.Wide, Ratio.Tall } .Select(r => new RatioPlan(r, image.FocusFor(r.Name), VariantPlanner.DefaultWidths(r))) .ToList(); var specs = VariantPlanner.Plan(image.Width, image.Height, ratios, processor.SupportedOutput); + progress?.Invoke(0, specs.Count); + + var rendered = await processor.RenderSetAsync(original, storedFormat, specs, progress, cancellationToken); var files = new Dictionary(); var variants = new List(); - foreach (var spec in specs) + for (var i = 0; i < specs.Count; i++) { - var rendered = await processor.RenderAsync(original, storedFormat, spec, cancellationToken); + var spec = specs[i]; var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format); - files[name] = rendered.Bytes; - variants.Add(new HeroVariant(spec.Ratio.Name, rendered.Width, rendered.Height, spec.Format.MimeType(), name, rendered.Bytes.LongLength)); + files[name] = rendered[i].Bytes; + variants.Add(new HeroVariant(spec.Ratio.Name, rendered[i].Width, rendered[i].Height, spec.Format.MimeType(), name, rendered[i].Bytes.LongLength)); } return (files, variants); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroRenderService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroRenderService.cs index b28f88b..1ba2329 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroRenderService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroRenderService.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Media; namespace SlpModularCms.Modules.Hero.Services; @@ -32,6 +34,14 @@ public sealed class HeroRenderSignal public IReadOnlySet GivenUp() => _failures.Where(f => f.Value >= MaxAttempts).Select(f => f.Key).ToHashSet(); + private readonly System.Collections.Concurrent.ConcurrentDictionary _progress = new(); + + public void SetProgress(Guid imageId, int done, int total) => _progress[imageId] = (done, total); + + public (int Done, int Total)? GetProgress(Guid imageId) => _progress.TryGetValue(imageId, out var p) ? p : null; + + public void ClearProgress(Guid imageId) => _progress.TryRemove(imageId, out _); + /// Forgets the failures of an image (after a success, a retry or a new upload of the same photo). public void Reset(Guid imageId) => _failures.TryRemove(imageId, out _); @@ -45,18 +55,28 @@ public sealed class HeroRenderSignal /// its new set) the moment its set is complete; until then the old set, or nothing, is published. Looks again when woken /// and at least every minute, and at start-up, so work interrupted by a restart is picked up. /// -public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSignal signal, ILogger logger) : BackgroundService +public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSignal signal, IOptions media, ILogger logger) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { - // Let the start-up (migrations, health check) finish first. + // Let the start-up (migrations, health check) finish first; whatever was still pending is picked up then. await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); + logger.LogInformation("Hero image renderer started."); while (!stoppingToken.IsCancellationRequested) { - await RunPassAsync(stoppingToken); + try + { + await RunPassAsync(stoppingToken); + } + catch (Exception exception) when (exception is not OperationCanceledException) + { + // Whatever happens, the renderer does not stop: it logs and looks again at the next wake-up. + logger.LogError(exception, "A round of the hero image renderer failed; it goes on."); + } + await signal.WaitAsync(TimeSpan.FromMinutes(1), stoppingToken); } } @@ -68,20 +88,32 @@ public sealed class HeroRenderService(IServiceScopeFactory scopes, HeroRenderSig private async Task RunPassAsync(CancellationToken stoppingToken) { + // An image that failed in this pass waits for the next pass (a minute later or the next wake-up): one difficult photo + // must not hold up the others, and it is not retried straight away. + var failedInThisPass = new HashSet(); while (!stoppingToken.IsCancellationRequested) { - var skip = signal.GivenUp(); + var skip = signal.GivenUp().Concat(failedInThisPass).ToHashSet(); Guid? handled = null; try { using var scope = scopes.CreateScope(); - handled = await scope.ServiceProvider.GetRequiredService().RenderNextPendingAsync(skip, stoppingToken); + // A hard limit around one image, a little above the time the image library itself is allowed: nothing can + // keep the renderer waiting for ever. + using var limit = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); + limit.CancelAfter(TimeSpan.FromSeconds(Math.Max(30, media.Value.RenderSetTimeoutSeconds) + 60)); + handled = await scope.ServiceProvider.GetRequiredService().RenderNextPendingAsync(skip, limit.Token); + } + catch (OperationCanceledException) when (!stoppingToken.IsCancellationRequested) + { + logger.LogError("Cutting a hero image took longer than the hard limit; the round is stopped and tried again at the next wake-up."); + return; } catch (HeroImageRenderException failure) { + failedInThisPass.Add(failure.ImageId); var attempts = signal.RecordFailure(failure.ImageId); logger.LogError(failure.InnerException, "Cutting the variants of hero image {ImageId} failed (attempt {Attempt} of {Max}).", failure.ImageId, attempts, HeroRenderSignal.MaxAttempts); - await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); continue; } catch (Exception exception) when (exception is not OperationCanceledException) diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs index 40294d5..f4fdd2d 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs @@ -38,13 +38,19 @@ public class HeroService( public async Task GetAsync(CancellationToken cancellationToken) => WithFailures(HeroMapping.ToAdmin(await repository.GetReadOnlyAsync(HeroLanguages.Default, cancellationToken), _time.GetUtcNow(), options.Value.MaxPublishedSlides)); - /// Marks the images the background renderer gave up on (that state lives in memory, not in the database). + /// Adds what only the running renderer knows (gave up on the image, which variant it is cutting); it lives in memory, not in the database. private HeroAdminDto WithFailures(HeroAdminDto dto) => dto with { - Slides = dto.Slides - .Select(s => imageService.IsRenderFailed(Guid.Parse(s.ImageId)) ? s with { Image = s.Image with { Failed = true } } : s) - .ToList(), + Slides = dto.Slides.Select(s => + { + var id = Guid.Parse(s.ImageId); + var progress = imageService.RenderProgress(id); + var failed = imageService.IsRenderFailed(id); + return failed || progress is not null + ? s with { Image = s.Image with { Failed = failed, RenderDone = progress?.Done, RenderTotal = progress?.Total } } + : s; + }).ToList(), }; public async Task SaveAsync(HeroSaveRequest request, Guid callerId, CancellationToken cancellationToken)