Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ readme = "README.md"
repository = "https://github.com/katsumeshi/image-base64-rs.git"

[dependencies]
image = "0.12.3"
base64 = "0.4.0"
regex = "0.2.1"
rustc-serialize = "0.3.22"
rust-crypto = "0.2.36"
base64 = "0.21.0"

[dev-dependencies]
md5 = "0.7.0"
56 changes: 33 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
extern crate rustc_serialize;
extern crate regex;

use std::fs::File;
use rustc_serialize::base64::{FromBase64, ToBase64, MIME};
use rustc_serialize::hex::{ToHex};
use regex::Regex;
use std::io::Read;
use std::string::String;

pub fn get_file_type(hex: &str) -> &str {
if Regex::new(r"^ffd8ffe0").unwrap().is_match(hex) {
return "jpeg"
} else if Regex::new(r"^89504e47").unwrap().is_match(hex) {
return "png"
} else if Regex::new(r"^47494638").unwrap().is_match(hex) {
return "gif"
}
panic!("invalid file type")

pub enum FileType {
JPEG,
PNG,
GIF
}

impl FileType {
fn as_str(&self) -> &'static str {
match self {
FileType::JPEG => "jpeg",
FileType::PNG => "png",
FileType::GIF => "gif"
}
}
}

pub fn to_base64(path: &str) -> String {
impl From<&str> for FileType {
fn from(item: &str) -> Self {
match item {
"jpeg" => FileType::JPEG,
"png" => FileType::PNG,
"gif" => FileType::GIF,
_ => FileType::JPEG
}
}
}

pub fn to_base64(path: &str, file_type: FileType) -> String {
let mut file = File::open(path).unwrap();
let mut vec = Vec::new();
let _ = file.read_to_end(&mut vec);
let base64 = vec.to_base64(MIME);
let hex = vec.to_hex();
return format!("data:image/{};base64,{}", get_file_type(&hex), base64.replace("\r\n", ""));
let b64 = base64::encode(&vec);
return format!("data:image/{};base64,{}", file_type.as_str(), b64.replace("\r\n", ""));
}

pub fn from_base64(base64: String) -> Vec<u8> {
let offset = base64.find(',').unwrap_or(base64.len())+1;
pub fn from_base64(base64: String) -> Result<Vec<u8>, base64::DecodeError> {
let offset = base64.find(',').unwrap_or(base64.len()) + 1;
let mut value = base64;
value.drain(..offset);
return value.from_base64().unwrap();
}
return base64::decode(&value)
}
15 changes: 4 additions & 11 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
extern crate rustc_serialize;
extern crate regex;
extern crate crypto;
extern crate image_base64;

use std::fs::File;
use std::io::Read;
use std::string::String;
use std::path::Path;
use std::io::Write;
use crypto::md5::Md5;
use crypto::digest::Digest;
use std::fs;
use std::str;

Expand Down Expand Up @@ -40,7 +35,7 @@ fn image_to_base64(file_type : &str) {
Err(why) => panic!("couldn't read {}", why),
Ok(_) => {},
}
let base64 = image_base64::to_base64(&format!("res/{}.{}", FILE_NAME, file_type));
let base64 = image_base64::to_base64(&format!("res/{}.{}", FILE_NAME, file_type), image_base64::FileType::from(file_type));
assert_eq!(base64, buffer);
}

Expand Down Expand Up @@ -74,7 +69,7 @@ fn base64_to_image(file_type : &str) {
}
let img = image_base64::from_base64(base64);
let mut output = File::create(&Path::new(&format!("output/{}.{}", FILE_NAME, file_type))).unwrap();
output.write_all(img.as_slice()).unwrap();
output.write_all(img.unwrap().as_slice()).unwrap();
}

fn validate(file_type : &str) {
Expand All @@ -83,7 +78,6 @@ fn validate(file_type : &str) {
}

fn get_hash(dir : &str, file_type : &str) -> String {
let mut hasher = Md5::new();
let mut file = match File::open(&format!("{}/{}.{}", dir, FILE_NAME, file_type)) {
Err(why) => panic!("couldn't open {}", why),
Ok(file) => file,
Expand All @@ -94,8 +88,7 @@ fn get_hash(dir : &str, file_type : &str) -> String {
Ok(_) => {},
}
let file_arr = vector_as_u8_4_array(file_vec);
hasher.input(&file_arr);
hasher.result_str()
format!("{:x}", md5::compute(file_arr))
}

fn get_file_size(dir : &str, file_type : &str) -> u64 {
Expand All @@ -112,4 +105,4 @@ fn vector_as_u8_4_array(vector: Vec<u8>) -> [u8;4] {
*place = *element;
}
arr
}
}