use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct Attachment {
pub(crate) filename: String,
pub(crate) content: Vec<u8>,
pub(crate) content_type: Option<String>,
pub(crate) inline: bool,
}
impl Attachment {
pub fn new(filename: impl Into<String>) -> Self {
Self {
filename: filename.into(),
content: Vec::new(),
content_type: None,
inline: false,
}
}
pub fn from_file(path: impl Into<PathBuf>) -> std::io::Result<Self> {
let path = path.into();
let filename = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("attachment")
.to_string();
let content = std::fs::read(&path)?;
let content_type = mime_guess::from_path(&path)
.first()
.map(|m| m.to_string());
Ok(Self {
filename,
content,
content_type,
inline: false,
})
}
pub fn content(mut self, content: Vec<u8>) -> Self {
self.content = content;
self
}
pub fn content_type(mut self, content_type: impl Into<String>) -> Self {
self.content_type = Some(content_type.into());
self
}
pub fn inline(mut self) -> Self {
self.inline = true;
self
}
}