use std::path::Path;
use serde_json::{Value, json};
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn block(path: &Path) -> Option<Value> {
let mime = match path.extension()?.to_str()? {
"png" => "image/png",
"jpg" => "image/jpeg",
"webp" => "image/webp",
_ => return None,
};
let bytes = std::fs::read(path).ok()?;
Some(json!({ "type": "image", "data": base64(&bytes), "mimeType": mime }))
}
fn base64(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let n = chunk
.iter()
.enumerate()
.fold(0u32, |n, (i, &byte)| n | u32::from(byte) << (16 - 8 * i));
for i in 0..4 {
out.push(if i <= chunk.len() {
ALPHABET[(n >> (18 - 6 * i) & 63) as usize] as char
} else {
'='
});
}
}
out
}