From 76ad709e0afed734c4331ddb8de91745a541a67d Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Wed, 4 Nov 2020 08:59:41 -0500 Subject: basic photo upload working completely --- dichroism/src/handlers.rs | 14 ++++++-------- dichroism/src/main.rs | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'dichroism/src') diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs index 43c11d2..ccaf347 100644 --- a/dichroism/src/handlers.rs +++ b/dichroism/src/handlers.rs @@ -106,17 +106,15 @@ async fn post_photo( ) -> Result { let mut responses: Vec = Vec::new(); - if let Ok(Some(mut field)) = payload.try_next().await { - // bail if a non-JPEG file was going to be uploaded - if field.content_type() != &mime::IMAGE_JPEG { - return Ok(HttpResponse::BadRequest().body("File must be a JPEG image.")); - } - + while let Some(mut field) = payload + .try_next() + .await + .map_err(|e| HttpResponse::BadRequest().body(e.to_string()))? + { // grab all bytes let mut data: Vec = Vec::new(); while let Some(chunk) = field.next().await { - let chunk = chunk?; - data.extend(chunk); + data.extend(chunk?); } // create new photo_set diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 68b7091..6c76f0a 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -3,7 +3,8 @@ extern crate serde; #[macro_use] extern crate diesel; -use actix_web::{App, HttpServer}; +use actix_cors::Cors; +use actix_web::{middleware::Logger, App, HttpServer}; use config::CONFIG_INSTANCE as CONFIG; use diesel::prelude::SqliteConnection; use diesel::r2d2::ConnectionManager; @@ -25,18 +26,28 @@ mod types; #[actix_web::main] async fn main() -> Result<()> { - // Initialize DB connection pool. + // Init logging + std::env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); + + // Init DB connection pool let manager = ConnectionManager::::new(&CONFIG.db_url); let pool = Pool::builder().build(manager)?; - // Initialize application server. + // Init application server let mut server = HttpServer::new(move || { + // Init CORS policy + let cors = Cors::permissive(); + App::new() .data(pool.clone()) + .wrap(cors) + .wrap(Logger::default()) .service(handlers::hello) .service(handlers::get_products) .service(handlers::patch_product) .service(handlers::post_product) + .service(handlers::post_photo) }); // If using listenfd, bind to it instead of the configured address to allow for cargo watch -- cgit v1.2.3