Generate thumbnails in the background after an upload
C++ · C++ · intermediate · modification
Uploads currently keep only the original image, and the gallery scales it down in the browser. This PR adds `thumbs::Thumbnailer` (`src/thumbs/thumbnailer.hpp`, `src/thumbs/thumbnailer.cpp`): `request` builds the target path `<media_root>/thumbs/<id>_<edge>.jpg` for each configured edge size and queues one resize job per edge on an executor, so the upload request does not wait for image processing. `src/main.cpp` creates a two-worker `jobs::ThreadPool`, constructs the thumbnailer with the `VipsResizer`, registers an `on_stored` listener on the upload handler that calls `request`, and shuts the pool down after the server stops. `tests/thumbnailer_test.cpp` (new) checks, with a resizer that records its calls, the source path, target path and edge `request` produces for one upload with two edge sizes and for two uploads in a row.
Media service in C++20, standard library only apart from the existing libvips wrapper, built with CMake (build files unchanged: it builds `src/**/*.cpp` into the service and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 locally and GCC 13 in CI with `-Wall -Wextra -Wconversion` treated as errors. Threading: the HTTP server (`http::Server`, unchanged) handles requests on several threads, so `on_stored` listeners and `Thumbnailer::request` can run concurrently; `server.run()` returns after SIGTERM once in-flight requests have finished, and no listener runs after that. Existing and unchanged: `jobs::Executor` (`src/jobs/executor.hpp`) is an interface with `virtual void submit(std::function<void()> job)`, which may run the job later on another thread; `jobs::ThreadPool` (`src/jobs/thread_pool.hpp`) implements it with a fixed number of workers, its `submit` is thread-safe and returns at once, and `shutdown()` runs every queued job and then joins the workers (the destructor calls it too). `imaging::Resizer` (`src/imaging/resizer.hpp`) is an interface with `void resize(const std::filesystem::path& src, const std::filesystem::path& dst, std::uint32_t max_edge) noexcept`, safe to call from several threads at once; `VipsResizer` (same header, the existing libvips wrapper) implements it, writes a JPEG whose longer edge is `max_edge` pixels, and on failure logs and leaves no file (the gallery falls back to the original when a thumbnail is missing). `media::Upload` (`src/media/upload.hpp`) holds `id`, a 26-character ULID in Crockford base32 validated by the upload handler and unique per upload, and `original`, the absolute path of the stored original. `http::UploadHandler::on_stored` (`src/http/upload_handler.hpp`) registers a listener that the handler calls on the request thread, after the original is on disk, with a `const media::Upload&` to an object that lives only until the handler returns; listeners are registered in `main` before `server.run()`. `config::Config::thumb_edges` (filled by the existing, unchanged `config::load_from_args` from the deployed service config, like `listen` and `media_root`) holds 1 to 4 distinct edge sizes, each 16 to 4096, and `<media_root>/thumbs` exists (created at deploy). Allocation failure is treated as fatal across the service and is out of scope. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed. `tests/inline_executor.hpp` (existing) provides `InlineExecutor`, a `jobs::Executor` that runs each job inside `submit`.
Requirements
- Add `thumbs::Thumbnailer::request(const media::Upload& upload)`: for each configured edge size, in configuration order, it queues one job on the thumbnailer's `jobs::Executor` that calls `Resizer::resize(upload.original, <media_root>/thumbs/<upload.id>_<edge>.jpg, edge)`. It returns as soon as the jobs are queued, without waiting for any of them to run.
- Each job resizes the original of the upload it was queued for into that upload's target path for the job's edge, so every upload ends up with one thumbnail per configured edge.
- `main` wires it: a `jobs::ThreadPool` with 2 workers, a `Thumbnailer` over that pool, the `VipsResizer`, the configured media root and edge sizes, and an `on_stored` listener on the upload handler that calls `request` for every stored upload. After `server.run()` returns, the pool is shut down so the queued thumbnails are finished before anything they use is destroyed.
- Unchanged: the upload handler still answers 201 as soon as the original is stored, and the upload response does not wait for thumbnails. Ship unit tests for `request` using the repo's `tests/check.hpp` helper.
Files touched
- src/thumbs/thumbnailer.hpp
- src/thumbs/thumbnailer.cpp
- src/main.cpp
- tests/thumbnailer_test.cpp