#[allow(unused_imports)]
use std::collections::BTreeMap;
#[cfg(feature = "inline-images")]
use crate::inline_images::{InlineImageBuild, InlineImageCollector, InlineImageFormat, InlineImageSource};
#[cfg(feature = "inline-images")]
type InlineCollectorHandle = std::rc::Rc<std::cell::RefCell<InlineImageCollector>>;
#[cfg(feature = "inline-images")]
#[allow(clippy::items_after_statements)]
#[allow(clippy::manual_let_else)]
pub fn handle_inline_data_image(
collector_ref: &InlineCollectorHandle,
src: &str,
alt: &str,
title: Option<&str>,
attributes: BTreeMap<String, String>,
) {
let Some(body) = crate::converter::media::data_url_body(src.trim_end()) else {
return;
};
let mut collector = collector_ref.borrow_mut();
let index = collector.next_index();
let Some((header, payload)) = body.split_once(',') else {
collector.warn_skip(index, "missing data URI separator");
return;
};
if payload.trim().is_empty() {
collector.warn_skip(index, "empty data URI payload");
return;
}
let Some((subtype, inline_name)) = parse_image_header(&mut collector, index, header) else {
return;
};
let Some(decoded) = decode_payload(&mut collector, index, payload) else {
return;
};
let format = inline_image_format(&subtype);
let description = non_empty_trimmed(alt).or_else(|| title.and_then(non_empty_trimmed));
let filename_candidate = attributes
.get("data-filename")
.cloned()
.or_else(|| attributes.get("filename").cloned())
.or_else(|| attributes.get("data-name").cloned())
.or(inline_name);
let dimensions = collector.infer_dimensions(index, &decoded, &format);
let image = collector.build_image(InlineImageBuild {
data: decoded,
format,
filename: filename_candidate,
description,
dimensions,
source: InlineImageSource::ImgDataUri,
attributes,
});
collector.push_image(index, image);
}
#[cfg(feature = "inline-images")]
fn parse_image_header(
collector: &mut InlineImageCollector,
index: usize,
header: &str,
) -> Option<(String, Option<String>)> {
if header.is_empty() {
collector.warn_skip(index, "missing MIME type");
return None;
}
let mut segments = header.split(';');
let mime = segments.next().unwrap_or("");
let Some((top_level, subtype)) = mime.split_once('/') else {
collector.warn_skip(index, "missing MIME subtype");
return None;
};
if !top_level.eq_ignore_ascii_case("image") {
collector.warn_skip(index, format!("unsupported MIME type {mime}"));
return None;
}
let subtype = subtype.trim();
if subtype.is_empty() {
collector.warn_skip(index, "missing MIME subtype");
return None;
}
let mut is_base64 = false;
let mut inline_name = None;
for segment in segments {
if segment.eq_ignore_ascii_case("base64") {
is_base64 = true;
} else if let Some(value) = segment
.strip_prefix("name=")
.or_else(|| segment.strip_prefix("filename="))
{
inline_name = non_empty_trimmed(value.trim_matches('"'));
}
}
if !is_base64 {
collector.warn_skip(index, "missing base64 encoding marker");
return None;
}
Some((subtype.to_string(), inline_name))
}
#[cfg(feature = "inline-images")]
fn decode_payload(collector: &mut InlineImageCollector, index: usize, payload: &str) -> Option<Vec<u8>> {
use base64::{Engine as _, engine::general_purpose::STANDARD};
let payload = payload.trim();
if payload.is_empty() {
collector.warn_skip(index, "empty data URI payload");
return None;
}
let max_size = collector.max_decoded_size();
let max_encoded = max_size.saturating_div(3).saturating_mul(4).saturating_add(4);
if payload.len() as u64 > max_encoded {
collector.warn_skip(
index,
format!(
"encoded payload ({} bytes) exceeds configured max ({max_size})",
payload.len()
),
);
return None;
}
let Ok(decoded) = STANDARD.decode(payload) else {
collector.warn_skip(index, "invalid base64 payload");
return None;
};
if decoded.is_empty() {
collector.warn_skip(index, "empty base64 payload");
return None;
}
if decoded.len() as u64 > max_size {
collector.warn_skip(
index,
format!(
"decoded payload ({} bytes) exceeds configured max ({max_size})",
decoded.len()
),
);
return None;
}
Some(decoded)
}
#[cfg(feature = "inline-images")]
fn inline_image_format(subtype: &str) -> InlineImageFormat {
match subtype.to_ascii_lowercase().as_str() {
"png" => InlineImageFormat::Png,
"jpeg" | "jpg" => InlineImageFormat::Jpeg,
"gif" => InlineImageFormat::Gif,
"bmp" => InlineImageFormat::Bmp,
"webp" => InlineImageFormat::Webp,
"svg+xml" => InlineImageFormat::Svg,
subtype => InlineImageFormat::Other(subtype.to_string()),
}
}
#[cfg(feature = "inline-images")]
fn non_empty_trimmed(value: &str) -> Option<String> {
let trimmed = value.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}