Render photo thumbnails in the background

C# · ASP.NET Core · advanced · modification

Moves thumbnail rendering out of the upload request. `ThumbnailQueue` (new) is a bounded in-memory channel of photo ids with room for 100, and `ThumbnailWorker` (new) is a hosted `BackgroundService` that reads it and renders each photo in its own DI scope, logging and skipping a photo that fails. `PhotosController.Upload` saves the photo as before, then queues its id and answers `201`; the controller no longer takes the renderer or a logger. `Program.cs` registers the queue as a singleton and the worker as a hosted service. `ThumbnailWorkerTests.cs` (new) checks that the upload answers before the thumbnail is rendered and that a failed thumbnail does not stop the next one.

Snapshots is a family photo album app. ASP.NET Core 10 controllers on .NET 10 (C# 14, nullable reference types enabled, implicit usings), EF Core 10 with Npgsql, xUnit tests. Existing and unchanged: `AddSnapshotsAuth` (cookie sign-in through the OIDC provider; `User.GetUserId()` reads the `sub` claim), `AddSnapshotsData` (registers the scoped `SnapshotsDbContext` on Npgsql) and `AddPhotoStorage` (registers `IPhotoStorage` as a singleton over the S3 bucket and `IThumbnailRenderer` as scoped). `IThumbnailRenderer.RenderAsync(photo, ct)` reads the original from storage, writes a 320-pixel JPEG to `thumbnails/{photo.Id}.jpg` (overwriting an earlier one), returns that key and honours the token; it throws on a corrupt image or a storage error, and it finishes or throws within 10 seconds (usually about 150 ms). `Photo` has `int Id`, `AlbumId`, `UploaderId`, `OriginalKey`, a nullable `ThumbnailKey` and `UploadedAt`; `PhotoDto.From` gives `thumbnailUrl: null` when `ThumbnailKey` is null. The photo menu's Regenerate thumbnail action (`POST /api/photos/{id}/thumbnail` in `PhotoActionsController`) renders inline and saves `ThumbnailKey`. Deleting a photo removes its row, and a nightly storage cleanup deletes objects that no photo row references. The API runs as two instances behind a reverse proxy that times requests out after 60 seconds; each instance has its own queue and renders the uploads it accepted. The generic host runs with its defaults (an exception that escapes a `BackgroundService` stops the host; a 30-second shutdown timeout). `SnapshotsApiFactory` (existing test host, unchanged) runs the app on a throwaway SQLite file and takes service overrides.

Requirements

Files touched

Review this PR

C# practice