diff options
Diffstat (limited to 'dichroism/src/main.rs')
-rw-r--r-- | dichroism/src/main.rs | 53 |
1 files changed, 18 insertions, 35 deletions
diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 6072198..fef8096 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,52 +1,32 @@ #[macro_use] extern crate lazy_static; + use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; -use base64::decode; use listenfd::ListenFd; -use regex::Regex; -lazy_static! { - static ref DATA_URI_RE: Regex = - Regex::new("^data:image/(?P<format>png|jpg|jpeg);base64,(?P<data>.+)") - .expect("Couldn't parse Regex!"); -} +mod image_api; +mod result; #[get("/")] async fn hello() -> impl Responder { HttpResponse::Ok().body("Hey, this is an API!") } -#[post("/products")] -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 or 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 bytes = if let Ok(b) = decode(data) { - b - } else { - return HttpResponse::BadRequest().body("Invalid image data in data URI."); - }; +#[post("/images")] +async fn create_image(req_body: String) -> impl Responder { + if let Err(e) = image_api::data_uri_to_files(&req_body) { + return HttpResponse::BadRequest().body(format!( + "Unable to extract image from data URI: {}", + e.to_string() + )); + } - let img = image::load_from_memory(&bytes).unwrap(); - img.save("test.png").unwrap(); - HttpResponse::Ok().body(format!("created\n{}\n{}", format, data)) + HttpResponse::Ok().body("Image created.") } #[get("/products")] async fn get_products(_req_body: String) -> impl Responder { - HttpResponse::Ok().body("got product!") + HttpResponse::Ok().body("got products!") } #[actix_web::main] @@ -55,11 +35,14 @@ async fn main() -> std::io::Result<()> { let mut server = HttpServer::new(|| { App::new() .service(hello) - .service(create_product) + .service(create_image) .service(get_products) }); - server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() { + server = if let Some(l) = listenfd + .take_tcp_listener(0) + .expect("Unable to grab TCP listener!") + { // "Debug mode" with cargo watch auto-reloading server.listen(l)? } else { |