summaryrefslogtreecommitdiff
path: root/dichroism/src/image_api.rs
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-03 20:13:46 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-03 20:13:46 -0400
commit58c6bd29b2efd14d7ae386124adf0750e6179370 (patch)
tree1b5b11cf9e4320440d5c28a76d304fd1eda369a0 /dichroism/src/image_api.rs
parentb08cfe37a28e7c84f754b4b75be9e2c1cec4ab22 (diff)
downloadtheglassyladies-58c6bd29b2efd14d7ae386124adf0750e6179370.tar.xz
theglassyladies-58c6bd29b2efd14d7ae386124adf0750e6179370.zip
added test for converting image uri to files, updated debug script
Diffstat (limited to 'dichroism/src/image_api.rs')
-rw-r--r--dichroism/src/image_api.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/dichroism/src/image_api.rs b/dichroism/src/image_api.rs
new file mode 100644
index 0000000..d1649d5
--- /dev/null
+++ b/dichroism/src/image_api.rs
@@ -0,0 +1,40 @@
+use crate::result::Result;
+use base64::decode;
+use regex::Regex;
+
+lazy_static! {
+ static ref DATA_URI_RE: Regex =
+ Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)").expect("Couldn't parse Regex!");
+}
+
+pub fn data_uri_to_files(uri: &str) -> Result<()> {
+ let caps = if let Some(c) = DATA_URI_RE.captures(uri) {
+ c
+ } else {
+ todo!();
+ };
+
+ let data = caps
+ .name("data")
+ .expect("Data URI data extraction should never fail.")
+ .as_str();
+
+ let bytes = decode(data)?;
+ let img = image::load_from_memory(&bytes)?;
+ img.save("test_full.jpg")?;
+ let thumb = img.thumbnail(200, 200);
+ thumb.save("test_thumbnail.jpg")?;
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const TEST_DATA_URI: &str = include_str!("unit_test_data/img_data_uri.txt");
+
+ #[test]
+ fn test_data_uri_to_files() {
+ data_uri_to_files(TEST_DATA_URI).unwrap();
+ }
+}