Avatar Thumbnail Processing Explained — Node.js Sharp, Fixed Sizes, Private Storage
Short answer: save the untouched avatar under a new versioned key, generate square 32, 64, 128, and 256 pixel variants with Sharp, keep every object private, and store those keys in the user record. Presign a variant for short-lived delivery; stream it through the application when access must be checked on every request. That policy favors predictable access control over a clever image service.…
The Node.js Sharp private avatar thumbnail pipeline should save an immutable asset set for each update to an avatar. This is achieved by creating a unique assetId for every update and storing the original image along with four fixed size WebP versions (32.webp, 64.webp, 128.webp, and 256.webp) alongside it. The user record points to the assetId and the five keys, but never to a permanent URL.
This approach handles avatar updates by making replacement a database decision, not an overwrite. Object versioning is not available for rollback in this storage method, so a fresh assetId ensures that the new upload does not destroy the old source. Once all required objects are written, the new database reference should only be published.
If processing halts midway, the user's reference should still point to the previous complete set, allowing a later cleanup job to remove unreferenced keys after an age threshold. The original bytes should be kept intact, as Sharp will normalize orientation before cropping the image to a square. The fit option "cover" ensures that every consumer receives the same geometry.
Four fixed outputs are intentionally chosen, as they make cache behavior and upper bounds on file dimensions easier to reason about, while arbitrary runtime resizing adds a user-controlled work queue to the delivery path. The crop itself is a product choice, and a center crop is recommended as it is reproducible. However, if the UI later adds a user-selected focal point, crop metadata should be kept with the asset record and regenerated from the original image, never destructively cropping the only source image.
The private upload worker should be built first, accepting an image buffer, writing the original image, creating the four WebP squares, and returning keys suitable for a transactional user-record update. The worker uses only the verified object-write route, with explicit PUT, Bearer authentication, idempotency key, bounded 429 retry, and response-body error handling to make network behavior visible rather than magical.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to