pub struct FileContent<'s> {
pub content: &'s str,
pub extension: &'s str,
}
impl FileContent<'_> {
pub fn decode(&self) -> std::io::Result<Vec<u8>> {
use base64::Engine;
base64::engine::general_purpose::STANDARD
.decode(self.content)
.map_err(|e| {
std::io::Error::new(std::io::ErrorKind::InvalidData, e)
})
}
pub fn write(&self, path: &std::path::Path) -> std::io::Result<()> {
let path = path.with_extension(self.extension);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&path, self.decode()?)
}
}
pub(crate) fn mime_to_ext(mime: &str) -> &str {
mime2ext::mime2ext(mime).unwrap_or("bin")
}