use std::path::{Path, PathBuf};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
pub const MAX_IMAGE_BYTES: u64 = 20 * 1024 * 1024;
pub const SUPPORTED_MIME_TYPES: &[&str] = &[
"image/png",
"image/jpeg",
"image/jpg",
"image/webp",
"image/gif",
"image/bmp",
];
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageAttachment {
pub path: PathBuf,
pub mime_type: String,
pub base64_data: String,
pub size_bytes: u64,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub dimensions: Option<(u32, u32)>,
}
#[derive(Debug)]
pub enum ImageError {
FileNotFound(PathBuf),
IoError(std::io::Error),
TooLarge { path: PathBuf, size: u64, max: u64 },
UnsupportedMime { path: PathBuf, mime: String },
EncodeError(String),
}
impl std::fmt::Display for ImageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ImageError::FileNotFound(p) => write!(f, "File not found: {}", p.display()),
ImageError::IoError(e) => write!(f, "I/O error: {}", e),
ImageError::TooLarge { path, size, max } => {
write!(
f,
"Image too large: {} ({} bytes, max {} bytes)",
path.display(),
size,
max
)
}
ImageError::UnsupportedMime { path, mime } => {
write!(
f,
"Unsupported image format '{}' for {}",
mime,
path.display()
)
}
ImageError::EncodeError(msg) => write!(f, "{}", msg),
}
}
}
impl std::error::Error for ImageError {}
fn guess_mime(ext: &str) -> Option<&'static str> {
match ext.to_lowercase().as_str() {
"png" => Some("image/png"),
"jpg" | "jpeg" => Some("image/jpeg"),
"webp" => Some("image/webp"),
"gif" => Some("image/gif"),
"bmp" => Some("image/bmp"),
"svg" => Some("image/svg+xml"),
_ => None,
}
}
const BASE64_CHARS: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
fn encode_base64(bytes: &[u8]) -> String {
let mut result = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let b0 = chunk[0];
let b1 = chunk.get(1).copied().unwrap_or(0);
let b2 = chunk.get(2).copied().unwrap_or(0);
let i0 = (b0 >> 2) as usize;
let i1 = (((b0 & 0x03) << 4) | (b1 >> 4)) as usize;
let i2 = if chunk.len() > 1 {
(((b1 & 0x0f) << 2) | (b2 >> 6)) as usize
} else {
64
};
let i3 = if chunk.len() > 2 {
(b2 & 0x3f) as usize
} else {
64
};
result.push(BASE64_CHARS[i0] as char);
result.push(BASE64_CHARS[i1] as char);
result.push(if i2 < 64 { BASE64_CHARS[i2] } else { b'=' } as char);
result.push(if i3 < 64 { BASE64_CHARS[i3] } else { b'=' } as char);
}
result
}
impl ImageAttachment {
pub fn load(path: PathBuf) -> Result<Self, ImageError> {
let bytes = std::fs::read(&path).map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
ImageError::FileNotFound(path.clone())
} else {
ImageError::IoError(e)
}
})?;
let size = bytes.len() as u64;
if size > MAX_IMAGE_BYTES {
return Err(ImageError::TooLarge {
path: path.clone(),
size,
max: MAX_IMAGE_BYTES,
});
}
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
let mime = guess_mime(ext).ok_or_else(|| ImageError::UnsupportedMime {
path: path.clone(),
mime: ext.to_string(),
})?;
let base64_data = encode_base64(&bytes);
Ok(ImageAttachment {
path,
mime_type: mime.to_string(),
base64_data,
size_bytes: size,
dimensions: None,
})
}
pub fn load_from_str(path_str: &str) -> Result<Self, ImageError> {
let base = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
Self::load_from_str_in(&base, path_str)
}
pub fn load_from_str_in(base_dir: &Path, path_str: &str) -> Result<Self, ImageError> {
let path = PathBuf::from_str(path_str)
.map_err(|_| ImageError::FileNotFound(PathBuf::from(path_str)))?;
let abs_path = if path.is_absolute() {
path
} else {
base_dir.join(&path)
};
Self::load(abs_path)
}
pub fn data_uri(&self) -> String {
format!("data:{};base64,{}", self.mime_type, self.base64_data)
}
pub fn display_name(&self) -> String {
self.path
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| self.path.to_string_lossy().to_string())
}
pub fn size_display(&self) -> String {
let bytes = self.size_bytes;
if bytes < 1024 {
format!("{} B", bytes)
} else if bytes < 1024 * 1024 {
format!("{:.1} KB", bytes as f64 / 1024.0)
} else {
format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
}
}
pub fn exists_on_disk(&self) -> bool {
self.path.is_file()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_base64() {
assert_eq!(encode_base64(b"f"), "Zg==");
assert_eq!(encode_base64(b"fo"), "Zm8=");
assert_eq!(encode_base64(b"foo"), "Zm9v");
assert_eq!(encode_base64(b"foob"), "Zm9vYg==");
assert_eq!(encode_base64(b"fooba"), "Zm9vYmE=");
assert_eq!(encode_base64(b"foobar"), "Zm9vYmFy");
assert_eq!(encode_base64(b""), "");
}
use std::io::Write;
fn make_png() -> Vec<u8> {
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90,
0x77, 0x53, 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0xF8, 0xFF, 0xFF, 0x3F, 0x00, 0x05, 0xFE, 0x02, 0xFE, 0xDC,
0xCC, 0x59, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
]
}
#[test]
fn test_load_png() {
let dir = tempfile::tempdir().unwrap();
let file_path = dir.path().join("test.png");
let png_bytes = make_png();
let mut f = std::fs::File::create(&file_path).unwrap();
f.write_all(&png_bytes).unwrap();
let img = ImageAttachment::load(file_path.clone()).unwrap();
assert_eq!(img.mime_type, "image/png");
assert!(!img.base64_data.is_empty());
assert_eq!(img.size_bytes, png_bytes.len() as u64);
assert!(img.data_uri().starts_with("data:image/png;base64,"));
}
#[test]
fn test_load_jpeg() {
let dir = tempfile::tempdir().unwrap();
let file_path = dir.path().join("photo.jpg");
std::fs::write(&file_path, b"\xff\xd8\xff\xe0").unwrap();
let img = ImageAttachment::load(file_path).unwrap();
assert_eq!(img.mime_type, "image/jpeg");
}
#[test]
fn test_load_unsupported_extension() {
let dir = tempfile::tempdir().unwrap();
let file_path = dir.path().join("doc.pdf");
std::fs::write(&file_path, b"not a pdf").unwrap();
let err = ImageAttachment::load(file_path).unwrap_err();
match err {
ImageError::UnsupportedMime { .. } => {}
other => panic!("expected UnsupportedMime, got {:?}", other),
}
}
#[test]
fn test_load_nonexistent() {
let path = PathBuf::from("/nonexistent/image.png");
let err = ImageAttachment::load(path).unwrap_err();
match err {
ImageError::FileNotFound(_) => {}
other => panic!("expected FileNotFound, got {:?}", other),
}
}
#[test]
fn test_display_name() {
let img = ImageAttachment {
path: PathBuf::from("/tmp/screenshot.png"),
mime_type: "image/png".to_string(),
base64_data: "aaaa".to_string(),
size_bytes: 100,
dimensions: None,
};
assert_eq!(img.display_name(), "screenshot.png");
assert_eq!(img.size_display(), "100 B");
}
#[test]
fn test_size_display() {
let img = ImageAttachment {
path: PathBuf::from("/tmp/big.png"),
mime_type: "image/png".to_string(),
base64_data: "x".repeat(1024 * 1024),
size_bytes: 1_500_000,
dimensions: None,
};
assert!(img.size_display().contains("1.4 MB"));
}
}