use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_media_rendition",
app = "cms",
admin(
list_display = "media_id, filter_spec, width, height, last_used_at",
ordering = "-last_used_at",
list_filter = "filter_spec",
)
)]
pub struct MediaRendition {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(fk = "cms_media", on = "id", index)]
pub media_id: i64,
#[rustango(max_length = 64, index)]
pub content_hash: String,
#[rustango(max_length = 64, index)]
pub filter_spec: String,
pub width: i32,
pub height: i32,
#[rustango(max_length = 128)]
pub mime: String,
#[rustango(max_length = 512)]
pub storage_key: String,
pub size: i64,
pub last_used_at: chrono::DateTime<chrono::Utc>,
#[rustango(auto_now_add)]
pub created_at: Auto<DateTime<Utc>>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FilterOp {
Fill { w: u32, h: u32 },
Max { w: u32, h: u32 },
Width(u32),
Height(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct EncodingOps {
pub format: Option<EncodeFormat>,
pub quality: Option<u8>,
pub background: Option<(u8, u8, u8)>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EncodeFormat {
Jpeg,
Png,
Webp,
Avif,
Auto,
}
impl EncodeFormat {
fn parse(s: &str) -> Option<Self> {
Some(match s {
"jpeg" | "jpg" => Self::Jpeg,
"png" => Self::Png,
"webp" => Self::Webp,
"avif" => Self::Avif,
"auto" => Self::Auto,
_ => return None,
})
}
fn canonical(self) -> &'static str {
match self {
Self::Jpeg => "jpeg",
Self::Png => "png",
Self::Webp => "webp",
Self::Avif => "avif",
Self::Auto => "auto",
}
}
fn mime(self) -> &'static str {
match self {
Self::Jpeg => "image/jpeg",
Self::Png => "image/png",
Self::Avif => "image/avif",
Self::Webp | Self::Auto => "image/webp",
}
}
fn ext(self) -> &'static str {
match self {
Self::Jpeg => "jpg",
Self::Png => "png",
Self::Avif => "avif",
Self::Webp | Self::Auto => "webp",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FilterPipeline {
pub geometric: FilterOp,
pub encoding: EncodingOps,
}
#[derive(Debug, thiserror::Error)]
pub enum FilterError {
#[error("unknown filter spec `{0}` — expected fill-WxH | max-WxH | width-W | height-H")]
Unknown(String),
#[error("invalid dimensions in `{0}`")]
BadDimensions(String),
#[error("invalid encoding op `{0}` — expected format-X | quality-N | bgcolor-XYZ")]
BadEncoding(String),
}
impl FilterPipeline {
pub fn parse(raw: &str) -> Result<Self, FilterError> {
let mut parts = raw.split('|');
let head = parts.next().unwrap_or("");
let geometric = FilterOp::parse(head)?;
let mut encoding = EncodingOps::default();
for seg in parts {
let seg = seg.trim();
if seg.is_empty() {
continue;
}
if let Some(rest) = seg.strip_prefix("format-") {
encoding.format = Some(
EncodeFormat::parse(rest)
.ok_or_else(|| FilterError::BadEncoding(seg.to_owned()))?,
);
} else if let Some(rest) = seg.strip_prefix("quality-") {
let n: u8 = rest
.parse()
.map_err(|_| FilterError::BadEncoding(seg.to_owned()))?;
if !(1..=100).contains(&n) {
return Err(FilterError::BadEncoding(seg.to_owned()));
}
encoding.quality = Some(n);
} else if let Some(rest) = seg.strip_prefix("bgcolor-") {
encoding.background = Some(
parse_hex_rgb(rest).ok_or_else(|| FilterError::BadEncoding(seg.to_owned()))?,
);
} else {
return Err(FilterError::BadEncoding(seg.to_owned()));
}
}
Ok(Self {
geometric,
encoding,
})
}
pub fn canonical(&self) -> String {
let mut out = self.geometric.canonical();
if let Some(fmt) = self.encoding.format {
out.push('|');
out.push_str("format-");
out.push_str(fmt.canonical());
}
if let Some(q) = self.encoding.quality {
out.push_str(&format!("|quality-{q}"));
}
if let Some((r, g, b)) = self.encoding.background {
out.push_str(&format!("|bgcolor-{r:02x}{g:02x}{b:02x}"));
}
out
}
}
fn parse_hex_rgb(s: &str) -> Option<(u8, u8, u8)> {
let s = s.trim_start_matches('#');
let hex = match s.len() {
3 => {
let mut out = String::with_capacity(6);
for c in s.chars() {
out.push(c);
out.push(c);
}
out
}
6 => s.to_owned(),
_ => return None,
};
crate::theme::parse_rgb6(&hex)
}
impl FilterOp {
pub fn parse(raw: &str) -> Result<Self, FilterError> {
let raw = raw.trim();
let parse_pair = |s: &str| -> Result<(u32, u32), FilterError> {
let (w, h) = s
.split_once('x')
.ok_or_else(|| FilterError::BadDimensions(raw.to_owned()))?;
let w: u32 = w
.parse()
.map_err(|_| FilterError::BadDimensions(raw.to_owned()))?;
let h: u32 = h
.parse()
.map_err(|_| FilterError::BadDimensions(raw.to_owned()))?;
if w == 0 || h == 0 || w > 8192 || h > 8192 {
return Err(FilterError::BadDimensions(raw.to_owned()));
}
Ok((w, h))
};
if let Some(rest) = raw.strip_prefix("fill-") {
let (w, h) = parse_pair(rest)?;
return Ok(Self::Fill { w, h });
}
if let Some(rest) = raw.strip_prefix("max-") {
let (w, h) = parse_pair(rest)?;
return Ok(Self::Max { w, h });
}
if let Some(rest) = raw.strip_prefix("width-") {
let n: u32 = rest
.parse()
.map_err(|_| FilterError::BadDimensions(raw.to_owned()))?;
if n == 0 || n > 8192 {
return Err(FilterError::BadDimensions(raw.to_owned()));
}
return Ok(Self::Width(n));
}
if let Some(rest) = raw.strip_prefix("height-") {
let n: u32 = rest
.parse()
.map_err(|_| FilterError::BadDimensions(raw.to_owned()))?;
if n == 0 || n > 8192 {
return Err(FilterError::BadDimensions(raw.to_owned()));
}
return Ok(Self::Height(n));
}
Err(FilterError::Unknown(raw.to_owned()))
}
pub fn canonical(&self) -> String {
match self {
Self::Fill { w, h } => format!("fill-{w}x{h}"),
Self::Max { w, h } => format!("max-{w}x{h}"),
Self::Width(n) => format!("width-{n}"),
Self::Height(n) => format!("height-{n}"),
}
}
}
pub fn apply(
original: &image::DynamicImage,
op: FilterOp,
focal: Option<(f32, f32)>,
) -> image::DynamicImage {
use image::imageops::FilterType;
let (sw, sh) = (original.width(), original.height());
match op {
FilterOp::Fill { w, h } => {
let scale_w = w as f32 / sw as f32;
let scale_h = h as f32 / sh as f32;
let scale = scale_w.max(scale_h);
let new_w = (sw as f32 * scale).round() as u32;
let new_h = (sh as f32 * scale).round() as u32;
let resized = original.resize_exact(new_w, new_h, FilterType::Lanczos3);
let (fx, fy) = focal.unwrap_or((0.5, 0.5));
let fx = fx.clamp(0.0, 1.0);
let fy = fy.clamp(0.0, 1.0);
let max_x = new_w.saturating_sub(w);
let max_y = new_h.saturating_sub(h);
let target_x = (fx * new_w as f32) - (w as f32 / 2.0);
let target_y = (fy * new_h as f32) - (h as f32 / 2.0);
let x_off = target_x.clamp(0.0, max_x as f32) as u32;
let y_off = target_y.clamp(0.0, max_y as f32) as u32;
resized.crop_imm(x_off, y_off, w.min(new_w), h.min(new_h))
}
FilterOp::Max { w, h } => original.resize(w, h, FilterType::Lanczos3),
FilterOp::Width(n) => {
let h = (sh as f32 * (n as f32 / sw as f32)).round() as u32;
original.resize_exact(n, h.max(1), FilterType::Lanczos3)
}
FilterOp::Height(n) => {
let w = (sw as f32 * (n as f32 / sh as f32)).round() as u32;
original.resize_exact(w.max(1), n, FilterType::Lanczos3)
}
}
}
pub fn encode(
image: &image::DynamicImage,
encoding: &EncodingOps,
source_mime: &str,
) -> Result<(Vec<u8>, String, String), String> {
use image::codecs::jpeg::JpegEncoder;
let format = encoding.format.unwrap_or_else(|| {
if source_mime == "image/jpeg" || source_mime == "image/jpg" {
EncodeFormat::Jpeg
} else if source_mime == "image/webp" {
EncodeFormat::Webp
} else {
EncodeFormat::Png
}
});
let needs_flatten = matches!(format, EncodeFormat::Jpeg) || encoding.background.is_some();
let prepared: image::DynamicImage = if needs_flatten && encoding.background.is_some() {
let (r, g, b) = encoding.background.unwrap();
flatten_onto_background(image, r, g, b)
} else if matches!(format, EncodeFormat::Jpeg) && image.color().has_alpha() {
flatten_onto_background(image, 255, 255, 255)
} else {
image.clone()
};
let mut out: Vec<u8> = Vec::new();
match format {
EncodeFormat::Jpeg => {
let quality = encoding.quality.unwrap_or(85);
let mut cur = std::io::Cursor::new(&mut out);
let mut enc = JpegEncoder::new_with_quality(&mut cur, quality);
enc.encode_image(&prepared)
.map_err(|e| format!("encode jpeg: {e}"))?;
}
EncodeFormat::Png => {
let mut cur = std::io::Cursor::new(&mut out);
prepared
.write_to(&mut cur, image::ImageFormat::Png)
.map_err(|e| format!("encode png: {e}"))?;
}
EncodeFormat::Webp | EncodeFormat::Auto => {
let mut cur = std::io::Cursor::new(&mut out);
prepared
.write_to(&mut cur, image::ImageFormat::WebP)
.map_err(|e| format!("encode webp: {e}"))?;
}
EncodeFormat::Avif => {
#[cfg(feature = "avif")]
{
let quality = encoding.quality.unwrap_or(61);
let mut cur = std::io::Cursor::new(&mut out);
let enc =
image::codecs::avif::AvifEncoder::new_with_speed_quality(&mut cur, 4, quality);
prepared
.write_with_encoder(enc)
.map_err(|e| format!("encode avif: {e}"))?;
}
#[cfg(not(feature = "avif"))]
{
let _ = &prepared;
return Err(
"AVIF output requires building rustango-cms with the `avif` feature".to_owned(),
);
}
}
}
Ok((out, format.mime().to_owned(), format.ext().to_owned()))
}
fn flatten_onto_background(src: &image::DynamicImage, r: u8, g: u8, b: u8) -> image::DynamicImage {
use image::GenericImageView;
let (w, h) = src.dimensions();
let mut buf = image::RgbaImage::from_pixel(w, h, image::Rgba([r, g, b, 255]));
let rgba = src.to_rgba8();
image::imageops::overlay(&mut buf, &rgba, 0, 0);
image::DynamicImage::ImageRgba8(buf)
}
pub async fn crop_image_off_runtime(
bytes: Vec<u8>,
source_mime: String,
(x, y, w, h): (u32, u32, u32, u32),
) -> Result<(Vec<u8>, String), String> {
rustango::__private_runtime::tokio::task::spawn_blocking(move || {
crop_image(&bytes, &source_mime, x, y, w, h)
})
.await
.map_err(|e| format!("crop worker failed: {e}"))?
}
pub async fn strip_exif_off_runtime(bytes: Vec<u8>, mime: String) -> (Vec<u8>, Option<(u32, u32)>) {
if !mime.starts_with("image/") || mime == "image/svg+xml" {
return (bytes, None);
}
rustango::__private_runtime::tokio::task::spawn_blocking(move || {
let (stripped, dims) = strip_exif_if_image(&bytes, &mime);
(stripped.unwrap_or(bytes), dims)
})
.await
.unwrap_or_else(|e| std::panic::resume_unwind(e.into_panic()))
}
pub fn crop_image(
bytes: &[u8],
source_mime: &str,
x: u32,
y: u32,
w: u32,
h: u32,
) -> Result<(Vec<u8>, String), String> {
if source_mime == "image/svg+xml" {
return Err(
"Cropping SVG sources isn't supported — clone first, edit the markup directly."
.to_owned(),
);
}
let img = image::load_from_memory(bytes).map_err(|e| format!("decode source: {e}"))?;
let (sw, sh) = (img.width(), img.height());
if w == 0 || h == 0 {
return Err("Crop rectangle must have non-zero width + height.".to_owned());
}
if x.saturating_add(w) > sw || y.saturating_add(h) > sh {
return Err(format!(
"Crop rectangle ({x},{y},{w},{h}) is outside the source ({sw}×{sh})."
));
}
let cropped = img.crop_imm(x, y, w, h);
let mut out: Vec<u8> = Vec::new();
let out_mime = if source_mime == "image/jpeg" || source_mime == "image/jpg" {
let mut cur = std::io::Cursor::new(&mut out);
cropped
.write_to(&mut cur, image::ImageFormat::Jpeg)
.map_err(|e| format!("encode jpeg: {e}"))?;
"image/jpeg".to_owned()
} else if source_mime == "image/webp" {
let mut cur = std::io::Cursor::new(&mut out);
cropped
.write_to(&mut cur, image::ImageFormat::WebP)
.map_err(|e| format!("encode webp: {e}"))?;
"image/webp".to_owned()
} else {
let mut cur = std::io::Cursor::new(&mut out);
cropped
.write_to(&mut cur, image::ImageFormat::Png)
.map_err(|e| format!("encode png: {e}"))?;
"image/png".to_owned()
};
Ok((out, out_mime))
}
#[must_use]
pub fn strip_exif_if_image(bytes: &[u8], mime: &str) -> (Option<Vec<u8>>, Option<(u32, u32)>) {
if !mime.starts_with("image/") || mime == "image/svg+xml" {
return (None, None);
}
let img = match image::load_from_memory(bytes) {
Ok(img) => img,
Err(e) => {
tracing::warn!(
target: "rustango_cms::rendition",
mime, error = %e,
"EXIF strip: decode failed, keeping original bytes"
);
return (None, None);
}
};
let dims = (img.width(), img.height());
let format = match mime {
"image/jpeg" | "image/jpg" => image::ImageFormat::Jpeg,
"image/png" => image::ImageFormat::Png,
"image/webp" => image::ImageFormat::WebP,
"image/gif" => image::ImageFormat::Gif,
"image/avif" => image::ImageFormat::Avif,
_ => image::ImageFormat::Png,
};
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
{
let mut cur = std::io::Cursor::new(&mut out);
if let Err(e) = img.write_to(&mut cur, format) {
tracing::warn!(
target: "rustango_cms::rendition",
mime, error = %e,
"EXIF strip: re-encode failed, keeping original bytes"
);
return (None, Some(dims));
}
}
(Some(out), Some(dims))
}
pub fn register_tera_function(tera: &mut tera::Tera) {
tera.register_function("rcms_image_url", RcmsImageUrlFn);
tera.register_function("rcms_image_srcset", RcmsImageSrcsetFn);
tera.register_function("rcms_image_srcset_dpi", RcmsImageSrcsetDpiFn);
tera.register_function("rcms_picture", RcmsPictureFn);
}
struct RcmsImageUrlFn;
impl tera::Function for RcmsImageUrlFn {
fn call(
&self,
args: &std::collections::HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
rcms_image_url(args)
}
fn is_safe(&self) -> bool {
true
}
}
fn rcms_image_url(
args: &std::collections::HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
let media_id = parse_media_id_arg(args)
.ok_or_else(|| tera::Error::msg("rcms_image_url: `media_id` (i64) is required"))?;
let filter = args
.get("filter")
.and_then(tera::Value::as_str)
.ok_or_else(|| tera::Error::msg("rcms_image_url: `filter` (string) is required"))?;
let op =
FilterOp::parse(filter).map_err(|e| tera::Error::msg(format!("rcms_image_url: {e}")))?;
let v = args.get("v").and_then(tera::Value::as_str);
Ok(tera::Value::String(rendition_url(
media_id,
&op.canonical(),
v,
)))
}
impl FilterOp {
fn nominal_width(self) -> Option<u32> {
match self {
Self::Fill { w, .. } | Self::Max { w, .. } | Self::Width(w) => Some(w),
Self::Height(_) => None,
}
}
fn scaled(self, factor: u32) -> Self {
let cap = |n: u32| n.saturating_mul(factor).min(8192);
match self {
Self::Fill { w, h } => Self::Fill {
w: cap(w),
h: cap(h),
},
Self::Max { w, h } => Self::Max {
w: cap(w),
h: cap(h),
},
Self::Width(w) => Self::Width(cap(w)),
Self::Height(h) => Self::Height(cap(h)),
}
}
}
fn parse_media_id_arg(args: &std::collections::HashMap<String, tera::Value>) -> Option<i64> {
args.get("media_id").and_then(|v| {
v.as_i64().or_else(|| {
v.as_str()
.map(str::trim)
.filter(|s| !s.is_empty())
.and_then(|s| s.parse::<i64>().ok())
})
})
}
fn parse_filters_arg(
args: &std::collections::HashMap<String, tera::Value>,
fn_name: &str,
) -> tera::Result<Vec<FilterPipeline>> {
let arr = args
.get("filters")
.and_then(|v| v.as_array())
.ok_or_else(|| {
tera::Error::msg(format!(
"{fn_name}: `filters` (array of spec strings) is required"
))
})?;
arr.iter()
.map(|v| {
let s = v.as_str().ok_or_else(|| {
tera::Error::msg(format!("{fn_name}: each filter must be a string"))
})?;
FilterPipeline::parse(s).map_err(|e| tera::Error::msg(format!("{fn_name}: {e}")))
})
.collect()
}
#[must_use]
pub fn rendition_url_for(media_id: i64, spec: &str, content_hash: Option<&str>) -> Option<String> {
let pipeline = FilterPipeline::parse(spec).ok()?;
Some(rendition_url(media_id, &pipeline.canonical(), content_hash))
}
fn rendition_url(media_id: i64, canonical_spec: &str, v: Option<&str>) -> String {
let base = match v {
Some(v) if !v.is_empty() => format!(
"/__media__/{canonical_spec}/{media_id}?v={}",
&v[..12.min(v.len())]
),
_ => format!("/__media__/{canonical_spec}/{media_id}"),
};
maybe_sign(base, canonical_spec, media_id)
}
static SIGNING_KEY: std::sync::OnceLock<Option<Vec<u8>>> = std::sync::OnceLock::new();
pub fn set_signing_key(key: impl Into<Vec<u8>>) {
let key = key.into();
if let Err(rejected) = SIGNING_KEY.set(Some(key)) {
if SIGNING_KEY.get() != Some(&rejected) {
tracing::error!(
target: "rustango_cms::rendition",
"set_signing_key called after the key was already fixed (set earlier, or read \
from RCMS_RENDITION_SIGNING_KEY); this call has no effect — set it before serving"
);
}
}
}
fn signing_key() -> Option<&'static [u8]> {
SIGNING_KEY
.get_or_init(|| {
crate::config::var("RENDITION_SIGNING_KEY").map(String::into_bytes)
})
.as_deref()
}
fn rendition_sig(key: &[u8], canonical_spec: &str, media_id: i64) -> String {
let msg = format!("{canonical_spec}:{media_id}");
let full = crate::signing::hmac_sha256_hex(key, msg.as_bytes());
full[..16].to_owned()
}
fn sig_ok(key: &[u8], canonical_spec: &str, media_id: i64, supplied: Option<&str>) -> bool {
let expected = rendition_sig(key, canonical_spec, media_id);
supplied.is_some_and(|s| crate::signing::constant_time_eq(s.as_bytes(), expected.as_bytes()))
}
fn maybe_sign(url: String, canonical_spec: &str, media_id: i64) -> String {
match signing_key() {
Some(key) => {
let sig = rendition_sig(key, canonical_spec, media_id);
let sep = if url.contains('?') { '&' } else { '?' };
format!("{url}{sep}s={sig}")
}
None => url,
}
}
#[must_use]
pub fn verify_rendition_sig(canonical_spec: &str, media_id: i64, sig: Option<&str>) -> bool {
match signing_key() {
None => true,
Some(key) => sig_ok(key, canonical_spec, media_id, sig),
}
}
fn build_srcset(media_id: i64, specs: &[FilterPipeline], v: Option<&str>) -> String {
specs
.iter()
.filter_map(|sp| {
sp.geometric
.nominal_width()
.map(|w| format!("{} {w}w", rendition_url(media_id, &sp.canonical(), v)))
})
.collect::<Vec<_>>()
.join(", ")
}
fn build_dpi_srcset(
media_id: i64,
base: &FilterPipeline,
densities: &[u32],
v: Option<&str>,
) -> String {
let mut ds: Vec<u32> = densities.iter().copied().filter(|d| *d > 0).collect();
ds.sort_unstable();
ds.dedup();
ds.iter()
.map(|d| {
let spec = FilterPipeline {
geometric: base.geometric.scaled(*d),
encoding: base.encoding,
};
format!("{} {d}x", rendition_url(media_id, &spec.canonical(), v))
})
.collect::<Vec<_>>()
.join(", ")
}
fn attr_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}
fn format_mime(fmt: &str) -> Option<&'static str> {
Some(match fmt {
"avif" => "image/avif",
"webp" | "auto" => "image/webp",
"jpeg" | "jpg" => "image/jpeg",
"png" => "image/png",
_ => return None,
})
}
fn build_picture(
media_id: i64,
specs: &[FilterPipeline],
formats: &[String],
alt: &str,
sizes: Option<&str>,
v: Option<&str>,
) -> String {
let sizes_attr = sizes
.filter(|s| !s.is_empty())
.map(|s| format!(" sizes=\"{}\"", attr_escape(s)))
.unwrap_or_default();
let mut out = String::from("<picture>");
for fmt in formats {
let (Some(mime), Some(parsed)) = (format_mime(fmt), EncodeFormat::parse(fmt)) else {
continue;
};
let variant: Vec<FilterPipeline> = specs
.iter()
.map(|sp| {
let mut s = *sp;
s.encoding.format = Some(parsed);
s
})
.collect();
let srcset = build_srcset(media_id, &variant, v);
if !srcset.is_empty() {
out.push_str(&format!(
"<source type=\"{mime}\" srcset=\"{srcset}\"{sizes_attr}>"
));
}
}
let base_srcset = build_srcset(media_id, specs, v);
let fallback = specs
.iter()
.max_by_key(|sp| sp.geometric.nominal_width().unwrap_or(0))
.map(|sp| rendition_url(media_id, &sp.canonical(), v))
.unwrap_or_default();
let srcset_attr = if base_srcset.is_empty() {
String::new()
} else {
format!(" srcset=\"{base_srcset}\"")
};
out.push_str(&format!(
"<img src=\"{fallback}\"{srcset_attr}{sizes_attr} alt=\"{}\" loading=\"lazy\" decoding=\"async\"></picture>",
attr_escape(alt),
));
out
}
struct RcmsImageSrcsetFn;
impl tera::Function for RcmsImageSrcsetFn {
fn call(
&self,
args: &std::collections::HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
let media_id = parse_media_id_arg(args)
.ok_or_else(|| tera::Error::msg("rcms_image_srcset: `media_id` (i64) is required"))?;
let specs = parse_filters_arg(args, "rcms_image_srcset")?;
let v = args.get("v").and_then(tera::Value::as_str);
Ok(tera::Value::String(build_srcset(media_id, &specs, v)))
}
fn is_safe(&self) -> bool {
true
}
}
struct RcmsImageSrcsetDpiFn;
impl tera::Function for RcmsImageSrcsetDpiFn {
fn call(
&self,
args: &std::collections::HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
let media_id = parse_media_id_arg(args).ok_or_else(|| {
tera::Error::msg("rcms_image_srcset_dpi: `media_id` (i64) is required")
})?;
let filter = args
.get("filter")
.and_then(tera::Value::as_str)
.ok_or_else(|| {
tera::Error::msg("rcms_image_srcset_dpi: `filter` (string) is required")
})?;
let base = FilterPipeline::parse(filter)
.map_err(|e| tera::Error::msg(format!("rcms_image_srcset_dpi: {e}")))?;
let densities: Vec<u32> = args
.get("densities")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|x| x.as_u64().map(|n| n as u32))
.collect()
})
.unwrap_or_else(|| vec![1, 2]);
let v = args.get("v").and_then(tera::Value::as_str);
Ok(tera::Value::String(build_dpi_srcset(
media_id, &base, &densities, v,
)))
}
fn is_safe(&self) -> bool {
true
}
}
struct RcmsPictureFn;
impl tera::Function for RcmsPictureFn {
fn call(
&self,
args: &std::collections::HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
let media_id = parse_media_id_arg(args)
.ok_or_else(|| tera::Error::msg("rcms_picture: `media_id` (i64) is required"))?;
let specs = parse_filters_arg(args, "rcms_picture")?;
let formats: Vec<String> = args
.get("formats")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|x| x.as_str().map(str::to_owned))
.collect()
})
.unwrap_or_else(|| vec!["avif".to_owned(), "webp".to_owned()]);
let alt = args.get("alt").and_then(tera::Value::as_str).unwrap_or("");
let sizes = args.get("sizes").and_then(tera::Value::as_str);
let v = args.get("v").and_then(tera::Value::as_str);
Ok(tera::Value::String(build_picture(
media_id, &specs, &formats, alt, sizes, v,
)))
}
fn is_safe(&self) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[rustango::__private_runtime::tokio::test(flavor = "current_thread")]
async fn image_work_runs_off_the_runtime() {
use rustango::__private_runtime::tokio;
use std::sync::atomic::{AtomicUsize, Ordering};
let img = image::RgbImage::from_fn(1200, 900, |x, y| image::Rgb([(x % 251) as u8, (y % 241) as u8, 90]));
let mut jpeg = Vec::new();
image::DynamicImage::ImageRgb8(img)
.write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg)
.expect("encode");
let ticks = std::sync::Arc::new(AtomicUsize::new(0));
let t = std::sync::Arc::clone(&ticks);
let ticker = tokio::spawn(async move {
loop {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
t.fetch_add(1, Ordering::Relaxed);
}
});
let (stored, dims) = strip_exif_off_runtime(jpeg, "image/jpeg".into()).await;
let after_strip = ticks.load(Ordering::Relaxed);
let (cropped, _) = crop_image_off_runtime(stored, "image/jpeg".into(), (0, 0, 600, 450))
.await
.expect("crop");
let after_crop = ticks.load(Ordering::Relaxed);
ticker.abort();
assert_eq!(dims, Some((1200, 900)));
assert!(!cropped.is_empty());
assert!(after_strip >= 2, "strip stalled the runtime ({after_strip} ticks)");
assert!(after_crop >= after_strip + 2, "crop stalled the runtime");
}
#[test]
fn bgcolor_with_non_ascii_bytes_is_an_error_not_a_panic() {
assert!(FilterPipeline::parse("width-10|bgcolor-a\u{e9}\u{20ac}").is_err());
assert!(FilterPipeline::parse("width-10|bgcolor-a\u{e9}").is_err());
assert!(FilterPipeline::parse("width-10|bgcolor-fff").is_ok());
}
#[test]
fn parses_fill() {
assert_eq!(
FilterOp::parse("fill-300x200").unwrap(),
FilterOp::Fill { w: 300, h: 200 }
);
}
#[test]
fn parses_max() {
assert_eq!(
FilterOp::parse("max-1200x900").unwrap(),
FilterOp::Max { w: 1200, h: 900 }
);
}
#[test]
fn parses_width() {
assert_eq!(FilterOp::parse("width-800").unwrap(), FilterOp::Width(800));
}
#[test]
fn parses_height() {
assert_eq!(
FilterOp::parse("height-600").unwrap(),
FilterOp::Height(600)
);
}
#[test]
fn rejects_garbage() {
assert!(FilterOp::parse("nope").is_err());
assert!(FilterOp::parse("fill-30000x30000").is_err()); assert!(FilterOp::parse("fill-0x0").is_err());
assert!(FilterOp::parse("fill-300").is_err());
}
#[test]
fn canonical_roundtrips() {
for raw in ["fill-300x200", "max-1200x900", "width-800", "height-600"] {
let parsed = FilterOp::parse(raw).unwrap();
assert_eq!(parsed.canonical(), raw);
}
}
fn fresh_tera() -> tera::Tera {
let mut tera = tera::Tera::default();
register_tera_function(&mut tera);
tera
}
fn render_one(src: &str, ctx_values: &[(&str, tera::Value)]) -> tera::Result<String> {
let mut tera = fresh_tera();
tera.add_raw_template("t.html", src)?;
let mut ctx = tera::Context::new();
for (k, v) in ctx_values {
ctx.insert(*k, v);
}
tera.render("t.html", &ctx)
}
#[test]
fn image_url_accepts_int_id() {
let out = render_one(
r#"{{ rcms_image_url(media_id=42, filter="width-1600") }}"#,
&[],
)
.unwrap();
assert_eq!(out, "/__media__/width-1600/42");
}
#[test]
fn image_url_accepts_stringified_id() {
let out = render_one(
r#"{{ rcms_image_url(media_id=v, filter="width-1600") }}"#,
&[("v", tera::Value::String("99".to_owned()))],
)
.unwrap();
assert_eq!(out, "/__media__/width-1600/99");
}
#[test]
fn image_url_trims_whitespace_around_string_id() {
let out = render_one(
r#"{{ rcms_image_url(media_id=v, filter="max-1200x900") }}"#,
&[("v", tera::Value::String(" 17 ".to_owned()))],
)
.unwrap();
assert_eq!(out, "/__media__/max-1200x900/17");
}
#[test]
fn image_url_empty_string_id_errors() {
let err = render_one(
r#"{{ rcms_image_url(media_id=v, filter="width-1600") }}"#,
&[("v", tera::Value::String("".to_owned()))],
)
.unwrap_err();
assert!(
format!("{err:?}").contains("media_id"),
"expected media_id error, got `{err:?}`"
);
}
#[test]
fn image_url_missing_filter_errors() {
let err = render_one(r#"{{ rcms_image_url(media_id=42) }}"#, &[]).unwrap_err();
assert!(
format!("{err:?}").contains("filter"),
"expected filter error, got `{err:?}`"
);
}
#[test]
fn image_url_with_content_hash_buster() {
let out = render_one(
r#"{{ rcms_image_url(media_id=42, filter="width-800", v="abc123def456hash") }}"#,
&[],
)
.unwrap();
assert_eq!(out, "/__media__/width-800/42?v=abc123def456");
}
#[test]
fn pipeline_parses_bare_resize() {
let p = FilterPipeline::parse("fill-300x200").unwrap();
assert_eq!(p.geometric, FilterOp::Fill { w: 300, h: 200 });
assert_eq!(p.encoding, EncodingOps::default());
}
#[test]
fn pipeline_parses_chained_encoding() {
let p = FilterPipeline::parse("fill-300x200|format-webp|quality-75|bgcolor-fff").unwrap();
assert_eq!(p.geometric, FilterOp::Fill { w: 300, h: 200 });
assert_eq!(p.encoding.format, Some(EncodeFormat::Webp));
assert_eq!(p.encoding.quality, Some(75));
assert_eq!(p.encoding.background, Some((255, 255, 255)));
}
#[test]
fn pipeline_canonical_roundtrips() {
let raw = "max-1200x900|format-avif|quality-80";
let parsed = FilterPipeline::parse(raw).unwrap();
assert_eq!(parsed.encoding.format, Some(EncodeFormat::Avif));
assert_eq!(parsed.canonical(), raw);
let raw = "fill-300x200|format-webp|quality-75|bgcolor-ffffff";
let parsed = FilterPipeline::parse(raw).unwrap();
assert_eq!(parsed.canonical(), raw);
}
#[test]
#[cfg(not(feature = "avif"))]
fn avif_encode_errors_clearly_without_feature() {
let img = image::DynamicImage::new_rgb8(2, 2);
let ops = EncodingOps {
format: Some(EncodeFormat::Avif),
quality: None,
background: None,
};
let err = encode(&img, &ops, "image/png").unwrap_err();
assert!(err.contains("avif"), "got: {err}");
}
#[test]
#[cfg(feature = "avif")]
fn avif_encode_produces_avif_bytes() {
let img = image::DynamicImage::new_rgb8(8, 8);
let ops = EncodingOps {
format: Some(EncodeFormat::Avif),
quality: Some(50),
background: None,
};
let (bytes, mime, ext) = encode(&img, &ops, "image/png").unwrap();
assert!(!bytes.is_empty());
assert_eq!(mime, "image/avif");
assert_eq!(ext, "avif");
}
#[test]
fn pipeline_rejects_garbage_segments() {
assert!(FilterPipeline::parse("fill-300x200|format-jpegxl").is_err());
assert!(FilterPipeline::parse("fill-300x200|quality-0").is_err());
assert!(FilterPipeline::parse("fill-300x200|quality-101").is_err());
assert!(FilterPipeline::parse("fill-300x200|bgcolor-xxxx").is_err());
assert!(FilterPipeline::parse("fill-300x200|nonsense").is_err());
}
#[test]
fn parse_hex_rgb_three_and_six_digit() {
assert_eq!(super::parse_hex_rgb("fff"), Some((255, 255, 255)));
assert_eq!(super::parse_hex_rgb("ff0000"), Some((255, 0, 0)));
assert_eq!(super::parse_hex_rgb("#abcdef"), Some((171, 205, 239)));
assert_eq!(super::parse_hex_rgb("ff"), None);
assert_eq!(super::parse_hex_rgb("ffffff00"), None); }
}
#[cfg(test)]
mod responsive_tests {
use super::{build_dpi_srcset, build_picture, build_srcset, FilterPipeline};
fn specs(raw: &[&str]) -> Vec<FilterPipeline> {
raw.iter()
.map(|s| FilterPipeline::parse(s).unwrap())
.collect()
}
#[test]
fn srcset_emits_url_and_width_descriptor() {
let out = build_srcset(42, &specs(&["fill-400x300", "fill-800x600"]), None);
assert_eq!(
out,
"/__media__/fill-400x300/42 400w, /__media__/fill-800x600/42 800w"
);
}
#[test]
fn srcset_skips_height_only_specs() {
let out = build_srcset(7, &specs(&["height-200", "width-600"]), None);
assert_eq!(out, "/__media__/width-600/7 600w");
}
#[test]
fn srcset_appends_cache_buster_truncated() {
let out = build_srcset(1, &specs(&["width-300"]), Some("0123456789abcdef"));
assert_eq!(out, "/__media__/width-300/1?v=0123456789ab 300w");
}
#[test]
fn dpi_srcset_scales_width_per_density() {
let base = FilterPipeline::parse("width-400").unwrap();
assert_eq!(
build_dpi_srcset(42, &base, &[1, 2], None),
"/__media__/width-400/42 1x, /__media__/width-800/42 2x"
);
}
#[test]
fn dpi_srcset_scales_both_fill_dims_and_dedups_densities() {
let base = FilterPipeline::parse("fill-200x100").unwrap();
let out = build_dpi_srcset(7, &base, &[2, 1, 2, 0], None);
assert_eq!(
out,
"/__media__/fill-200x100/7 1x, /__media__/fill-400x200/7 2x"
);
}
#[test]
fn dpi_srcset_caps_at_8192() {
let base = FilterPipeline::parse("width-5000").unwrap();
assert_eq!(
build_dpi_srcset(1, &base, &[2], None),
"/__media__/width-8192/1 2x"
);
}
#[test]
fn rendition_url_for_parses_and_rejects_bad_spec() {
assert_eq!(
super::rendition_url_for(42, "width-800", Some("abc123def456ZZ")),
Some("/__media__/width-800/42?v=abc123def456".to_owned())
);
assert_eq!(super::rendition_url_for(42, "not-a-spec", None), None);
}
#[test]
fn picture_has_source_per_format_and_img_fallback() {
let html = build_picture(
42,
&specs(&["width-400", "width-800"]),
&["avif".to_owned(), "webp".to_owned()],
"A <photo> & more",
Some("100vw"),
None,
);
assert!(html.contains(r#"<source type="image/avif" srcset="/__media__/width-400|format-avif/42 400w, /__media__/width-800|format-avif/42 800w" sizes="100vw">"#), "{html}");
assert!(html.contains(r#"<source type="image/webp" srcset="/__media__/width-400|format-webp/42 400w, /__media__/width-800|format-webp/42 800w" sizes="100vw">"#), "{html}");
assert!(
html.contains(r#"<img src="/__media__/width-800/42""#),
"{html}"
);
assert!(
html.contains(r#"alt="A <photo> & more""#),
"{html}"
);
assert!(
html.starts_with("<picture>") && html.ends_with("</picture>"),
"{html}"
);
}
#[test]
fn picture_skips_unknown_format_token() {
let html = build_picture(
5,
&specs(&["width-500"]),
&["bogus".to_owned(), "webp".to_owned()],
"",
None,
None,
);
assert!(!html.contains("bogus"), "{html}");
assert!(html.contains(r#"type="image/webp""#), "{html}");
}
#[test]
fn tera_functions_render_end_to_end() {
let mut tera = tera::Tera::default();
super::register_tera_function(&mut tera);
tera.add_raw_template(
"s.html",
r#"{{ rcms_image_srcset(media_id=42, filters=["width-400", "width-800"]) }}"#,
)
.unwrap();
assert_eq!(
tera.render("s.html", &tera::Context::new()).unwrap(),
"/__media__/width-400/42 400w, /__media__/width-800/42 800w"
);
tera.add_raw_template(
"p.html",
r#"{{ rcms_picture(media_id=7, filters=["width-600"], formats=["webp"], alt="hi") }}"#,
)
.unwrap();
let p = tera.render("p.html", &tera::Context::new()).unwrap();
assert!(p.contains(r#"type="image/webp""#), "{p}");
assert!(p.contains(r#"alt="hi""#), "{p}");
assert!(p.starts_with("<picture>"), "{p}");
}
}
#[cfg(test)]
mod sign_tests {
use super::{maybe_sign, rendition_sig, sig_ok, verify_rendition_sig};
#[test]
fn sig_is_deterministic_16_hex_and_input_sensitive() {
let k = b"secret";
let a = rendition_sig(k, "fill-400x300", 42);
assert_eq!(a.len(), 16);
assert!(a.chars().all(|c| c.is_ascii_hexdigit()), "{a}");
assert_eq!(a, rendition_sig(k, "fill-400x300", 42)); assert_ne!(a, rendition_sig(k, "fill-400x300", 43)); assert_ne!(a, rendition_sig(k, "fill-401x300", 42)); assert_ne!(a, rendition_sig(b"other-key", "fill-400x300", 42)); }
#[test]
fn sig_ok_accepts_correct_rejects_forged_or_missing() {
let k = b"secret";
let good = rendition_sig(k, "width-800", 7);
assert!(sig_ok(k, "width-800", 7, Some(&good)));
assert!(!sig_ok(k, "width-800", 7, Some("deadbeefdeadbeef"))); assert!(!sig_ok(k, "width-800", 7, None)); assert!(!sig_ok(k, "width-800", 8, Some(&good))); }
#[test]
fn disabled_signing_is_open_and_unsigned() {
assert_eq!(
maybe_sign("/__media__/width-800/7".to_owned(), "width-800", 7),
"/__media__/width-800/7"
);
assert!(verify_rendition_sig("width-800", 7, None));
}
}