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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ repository = "https://github.com/katsumeshi/image-base64-rs.git"
[dependencies]
image = "0.12.3"
base64 = "0.4.0"
regex = "0.2.1"
reqwest = { version = "0.10.6", features = ["blocking"]}
rustc-serialize = "0.3.22"
rust-crypto = "0.2.36"
47 changes: 31 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
extern crate reqwest;
extern crate rustc_serialize;
extern crate regex;

use std::fs::File;
use reqwest::blocking;
use rustc_serialize::base64::{FromBase64, ToBase64, MIME};
use rustc_serialize::hex::{ToHex};
use regex::Regex;
use rustc_serialize::hex::ToHex;
use std::fs::File;
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")
match &hex[..8] {
r"ffd8ffe0" => "jpeg",
r"89504e47" => "png",
r"47494638" => "gif",
_ => panic!("invalid file type"),
}
}

pub fn to_base64(path: &str) -> String {
Expand All @@ -25,12 +23,29 @@ pub fn to_base64(path: &str) -> String {
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", ""));
return format!(
"data:image/{};base64,{}",
get_file_type(&hex),
base64.replace("\r\n", "")
);
}

pub fn from_base64(base64: String) -> Vec<u8> {
let offset = base64.find(',').unwrap_or(base64.len())+1;
let offset = base64.find(',').unwrap_or(base64.len()) + 1;
let mut value = base64;
value.drain(..offset);
return value.from_base64().unwrap();
}
value.from_base64().unwrap()
}

pub fn from_url(image_url: &str) -> String {
let result = reqwest::blocking::get(image_url).unwrap().bytes().unwrap();

let base64 = result.to_base64(MIME);
let hex = result.to_hex();

format!(
"data:image/{};base64,{}",
get_file_type(&hex),
base64.replace("\r\n", "")
)
}
41 changes: 39 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate rustc_serialize;
extern crate regex;
extern crate crypto;
extern crate image_base64;

Expand Down Expand Up @@ -112,4 +111,42 @@ fn vector_as_u8_4_array(vector: Vec<u8>) -> [u8;4] {
*place = *element;
}
arr
}
}

#[test]
fn png_url_to_base64() {
url_to_base64("png");
}

#[test]
fn jpg_url_to_base64() {
url_to_base64("jpg");
}

#[test]
fn gif_url_to_base64() {
url_to_base64("gif");
}

fn url_to_base64(file_type: &str) {
let result = image_base64::from_url(
format!(
"{}{}",
"https://raw.githubusercontent.com/katsumeshi/image-base64-rs/master/res/nyan.",
file_type
)
.as_ref(),
);

let mut file = match File::open(format!("res/{}_data", file_type)) {
Err(why) => panic!("couldn't open {}", why),
Ok(file) => file,
};
let mut buffer = String::new();
match file.read_to_string(&mut buffer) {
Err(why) => panic!("couldn't read {}", why),
Ok(_) => {}
}

assert_eq!(result, buffer);
}