You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
994 B
56 lines
994 B
use super::*;
|
|
use std::{
|
|
str,
|
|
io::{
|
|
self,
|
|
Write,
|
|
},
|
|
};
|
|
|
|
const HEADER_BASE64_JPEG: &'static str = "/9j/";
|
|
const HEADER_BASE64_PNG: &'static str = "iVBO";
|
|
const HEADER_BASE64_GIF: &'static str = "R0lG";
|
|
|
|
/// An image type header
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
pub enum ImageType
|
|
{
|
|
Png,
|
|
Jpeg,
|
|
Gif,
|
|
}
|
|
|
|
impl Default for ImageType
|
|
{
|
|
fn default() -> Self
|
|
{
|
|
Self::Jpeg
|
|
}
|
|
}
|
|
|
|
impl str::FromStr for ImageType
|
|
{
|
|
type Err = error::Error;
|
|
|
|
/// Determine image type from base64
|
|
fn from_str(from: &str) -> Result<Self, Self::Err>
|
|
{
|
|
if from.len() > 4 {
|
|
Ok(match &from[..4] {
|
|
HEADER_BASE64_GIF => Self::Gif,
|
|
HEADER_BASE64_PNG => Self::Png,
|
|
HEADER_BASE64_JPEG => Self::Jpeg,
|
|
_ => return Err(error::Error::UnknownFormat),
|
|
})
|
|
} else {
|
|
Err(error::Error::InvalidFormat)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Calculate the required data size from base64 input size
|
|
pub const fn data_size(base64: usize) -> usize
|
|
{
|
|
((4 * base64 / 3) + 3) & !3
|
|
}
|