mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
## Summary Two gaps in the thumbnail regeneration from [#9174](https://github.com/block/buzz/pull/9174): - **Its MinIO tests never ran in CI.** `api::media::tests::postgres_tests::external_infra_minio_tests` is excluded from the PostgreSQL lane, which skips `external_infra*` modules, and the backend integration job's only media MinIO step selects the admin tests. A sibling step now runs them against the same Postgres and MinIO. - **Rebuilding a thumbnail skipped the 25 MP pixel cap.** Upload validation rejects images over 25 megapixels before they reach the decoder. A thumbnail rebuilt from a stored original goes straight to `generate_image_metadata_sync`, so that cap didn't apply. The check is now a shared `check_image_pixels` that upload validation and thumbnail generation both run before decoding. On a rebuild, an oversized original gets the same 404 as any other regeneration failure. ### Related issue Follow-up to [#9174](https://github.com/block/buzz/pull/9174), from its review. ### Testing - `missing_thumbnail_is_not_regenerated_when_unsafe` now stores a valid 5001x5000 PNG as an image original without thumbnails and requests one through the media route. On `main` the relay rebuilds and serves it; with this change the request is a 404 and no thumbnail is written. - The new CI step runs both `external_infra_minio_tests` cases one at a time, since they share the fixture image's storage keys. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: John Tennant <jtennant@squareup.com> Co-authored-by: Simone <0828be588d5c9c4d092c5b1cf01761d5aef63d6b104807fd8e8284d517dd1a99@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.0 KiB
Rust
101 lines
3.0 KiB
Rust
//! Synchronous thumbnail generation and blurhash encoding.
|
|
|
|
use std::io::Cursor;
|
|
|
|
use image::ImageFormat;
|
|
|
|
use crate::config::MediaConfig;
|
|
use crate::error::MediaError;
|
|
use crate::storage::BlobMeta;
|
|
|
|
/// A stored thumbnail variant: `{sha256}.thumb.{ext}`.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct Thumb {
|
|
pub ext: &'static str,
|
|
pub mime: &'static str,
|
|
}
|
|
|
|
/// Lossy WebP keeps alpha, so upload descriptors reference it.
|
|
pub const WEBP: Thumb = Thumb {
|
|
ext: "webp",
|
|
mime: "image/webp",
|
|
};
|
|
|
|
/// JPEG drops alpha. It stays for clients that derive `{sha256}.thumb.jpg`.
|
|
pub const JPEG: Thumb = Thumb {
|
|
ext: "jpg",
|
|
mime: "image/jpeg",
|
|
};
|
|
|
|
/// Every variant written for an image.
|
|
pub const THUMBS: [Thumb; 2] = [WEBP, JPEG];
|
|
|
|
/// One image's thumbnail bytes, in [`THUMBS`] order.
|
|
pub type ThumbBytes = [Vec<u8>; THUMBS.len()];
|
|
|
|
/// libwebp quality: ~13 KB for a 320px transparent avatar, about JPEG's size.
|
|
const WEBP_QUALITY: f32 = 80.0;
|
|
|
|
impl Thumb {
|
|
/// Storage key and `/media/` path segment for `sha256`'s thumbnail.
|
|
pub fn key(self, sha256: &str) -> String {
|
|
format!("{sha256}.thumb.{}", self.ext)
|
|
}
|
|
|
|
/// Split `{sha256}.thumb.{ext}` into its (unvalidated) hash and variant.
|
|
pub fn parse(name: &str) -> Option<(&str, Thumb)> {
|
|
let (sha256, ext) = name.split_once(".thumb.")?;
|
|
THUMBS
|
|
.into_iter()
|
|
.find(|t| t.ext == ext)
|
|
.map(|t| (sha256, t))
|
|
}
|
|
}
|
|
|
|
/// Generate thumbnails and blurhash from image bytes (CPU-bound, sync).
|
|
///
|
|
/// Returns `(metadata, thumbnails)`, with no thumbnails for non-images.
|
|
/// Caller handles S3 writes after `spawn_blocking` returns.
|
|
pub fn generate_image_metadata_sync(
|
|
config: &MediaConfig,
|
|
sha256: &str,
|
|
bytes: &[u8],
|
|
mime: &str,
|
|
ext: &str,
|
|
) -> Result<(BlobMeta, Option<ThumbBytes>), MediaError> {
|
|
if !mime.starts_with("image/") {
|
|
return Ok((BlobMeta::default(), None));
|
|
}
|
|
|
|
crate::validation::check_image_pixels(bytes)?;
|
|
let img = image::load_from_memory(bytes)?;
|
|
let (w, h) = (img.width(), img.height());
|
|
|
|
// Thumbnail: 320px max dimension, preserve aspect ratio
|
|
let thumb = img.thumbnail(320, 320);
|
|
let rgba = thumb.to_rgba8();
|
|
let webp = webp::Encoder::from_rgba(&rgba, rgba.width(), rgba.height())
|
|
.encode_simple(false, WEBP_QUALITY)
|
|
.map_err(|_| MediaError::Internal)?
|
|
.to_vec();
|
|
let mut jpeg = Vec::new();
|
|
thumb.write_to(&mut Cursor::new(&mut jpeg), ImageFormat::Jpeg)?;
|
|
|
|
// Blurhash from thumbnail (faster than full image)
|
|
let bh =
|
|
blurhash::encode(4, 3, thumb.width(), thumb.height(), rgba.as_raw()).unwrap_or_default();
|
|
|
|
Ok((
|
|
BlobMeta {
|
|
dim: format!("{w}x{h}"),
|
|
blurhash: bh,
|
|
thumb_url: format!("{}/{}", config.public_base_url, WEBP.key(sha256)),
|
|
ext: ext.to_string(),
|
|
mime_type: mime.to_string(),
|
|
size: bytes.len() as u64,
|
|
..BlobMeta::default() // uploaded_at set by caller (process_upload)
|
|
},
|
|
Some([webp, jpeg]),
|
|
))
|
|
}
|