From b08cfe37a28e7c84f754b4b75be9e2c1cec4ab22 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 3 Oct 2020 13:35:45 -0400 Subject: restored debug script, successfully getting image from URI into file --- dichroism/src/main.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'dichroism/src') diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 77fd0a1..6072198 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,13 +1,14 @@ #[macro_use] extern crate lazy_static; use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; -use base64; +use base64::decode; use listenfd::ListenFd; use regex::Regex; lazy_static! { static ref DATA_URI_RE: Regex = - Regex::new("^data:image/(?Ppng|jpg|jpeg);base64,(?P.+)").unwrap(); + Regex::new("^data:image/(?Ppng|jpg|jpeg);base64,(?P.+)") + .expect("Couldn't parse Regex!"); } #[get("/")] @@ -20,21 +21,26 @@ async fn create_product(req_body: String) -> impl Responder { let caps = if let Some(c) = DATA_URI_RE.captures(&req_body) { c } else { - return HttpResponse::BadRequest().body("Invalid data URI"); + return HttpResponse::BadRequest().body("Invalid data URI or image format."); }; - let format = if let Some(f) = caps.name("format") { - f.as_str() - } else { - return HttpResponse::BadRequest().body("Invalid image format."); - }; + let format = caps + .name("format") + .expect("Data URI format extraction should never fail.") + .as_str(); + let data = caps + .name("data") + .expect("Data URI data extraction should never fail.") + .as_str(); - let data = if let Some(d) = caps.name("data") { - d.as_str() + let bytes = if let Ok(b) = decode(data) { + b } else { - return HttpResponse::BadRequest().body("No encoded data."); + return HttpResponse::BadRequest().body("Invalid image data in data URI."); }; + let img = image::load_from_memory(&bytes).unwrap(); + img.save("test.png").unwrap(); HttpResponse::Ok().body(format!("created\n{}\n{}", format, data)) } @@ -52,10 +58,13 @@ async fn main() -> std::io::Result<()> { .service(create_product) .service(get_products) }); + server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { + // "Debug mode" with cargo watch auto-reloading server.listen(l)? } else { - server.bind("127.0.0.1:8080")? + // "Release mode" + server.bind("127.0.0.1:8000")? }; server.run().await -- cgit v1.2.3