diff options
| author | Adam T. Carpenter <atc@53hor.net> | 2020-11-04 08:59:41 -0500 | 
|---|---|---|
| committer | Adam T. Carpenter <atc@53hor.net> | 2020-11-04 08:59:41 -0500 | 
| commit | 76ad709e0afed734c4331ddb8de91745a541a67d (patch) | |
| tree | be7b3d20387506ed23fa9c4f4f386ef3bbb1ae86 | |
| parent | dcc96d0b349583e5d6a0f25ae1f7a3ffa3769788 (diff) | |
| download | theglassyladies-76ad709e0afed734c4331ddb8de91745a541a67d.tar.xz theglassyladies-76ad709e0afed734c4331ddb8de91745a541a67d.zip  | |
basic photo upload working completely
| -rw-r--r-- | dichroism/Cargo.lock | 26 | ||||
| -rw-r--r-- | dichroism/Cargo.toml | 9 | ||||
| -rw-r--r-- | dichroism/src/handlers.rs | 14 | ||||
| -rw-r--r-- | dichroism/src/main.rs | 17 | ||||
| -rw-r--r-- | iridescence/src/components/admin/ProductEditCard.vue | 43 | 
5 files changed, 74 insertions, 35 deletions
diff --git a/dichroism/Cargo.lock b/dichroism/Cargo.lock index 06bae04..1fe4b81 100644 --- a/dichroism/Cargo.lock +++ b/dichroism/Cargo.lock @@ -36,6 +36,20 @@ dependencies = [  ]  [[package]] +name = "actix-cors" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf0c4345c9663a2822d42602391418fd5766f269109ec6bf1784b056a9356a7" +dependencies = [ + "actix-web", + "derive_more", + "futures-util", + "log", + "once_cell", + "tinyvec 1.0.1", +] + +[[package]]  name = "actix-http"  version = "2.0.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -738,6 +752,7 @@ dependencies = [  name = "dichroism"  version = "0.1.0"  dependencies = [ + "actix-cors",   "actix-multipart",   "actix-web",   "async-std", @@ -830,9 +845,9 @@ dependencies = [  [[package]]  name = "env_logger" -version = "0.7.1" +version = "0.8.1"  source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +checksum = "54532e3223c5af90a6a757c90b5c5521564b07e5e7a958681bcd2afad421cdcd"  dependencies = [   "atty",   "humantime", @@ -1132,12 +1147,9 @@ checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"  [[package]]  name = "humantime" -version = "1.3.0" +version = "2.0.1"  source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] +checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"  [[package]]  name = "idna" diff --git a/dichroism/Cargo.toml b/dichroism/Cargo.toml index 7dd6036..22b8a42 100644 --- a/dichroism/Cargo.toml +++ b/dichroism/Cargo.toml @@ -5,17 +5,18 @@ authors = ["Adam T. Carpenter <atc@53hor.net>"]  edition = "2018"  [dependencies] -actix-web = "3" +actix-cors = "0.5"  actix-multipart = "0.3" -mime = "0.3" -futures = "0.3" +actix-web = "3"  async-std = "1"  base64 = "0.13"  diesel = { version = "1", features = [ "sqlite", "r2d2" ] } -env_logger = "0.7" +env_logger = "0.8" +futures = "0.3"  image = "0.23"  listenfd = "0.3"  log = "0.4" +mime = "0.3"  once_cell = "1"  regex = "1"  serde = { version = "1.0", features = ["derive"] } 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<HttpResponse, Error> {      let mut responses: Vec<PhotoSetGet> = 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<u8> = 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::<SqliteConnection>::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 diff --git a/iridescence/src/components/admin/ProductEditCard.vue b/iridescence/src/components/admin/ProductEditCard.vue index 8d1eb3d..238c975 100644 --- a/iridescence/src/components/admin/ProductEditCard.vue +++ b/iridescence/src/components/admin/ProductEditCard.vue @@ -214,27 +214,44 @@ export default {          this.newProduct.quantity += amount;        }      }, -    previewFiles(event) { +    async previewFiles(event) {        let file = event.target.files[0];        if (!file) {          return;        } -      let reader = new FileReader(); +      const fd = new FormData(); +      fd.append(file.name, file); -      reader.onprogress = e => { -        if (e && e.lengthComputable) { -          this.fileProgress = parseInt((e.loaded / e.total) * 100, 10); -        } -      }; +      const response = await fetch("http://localhost:8000/photos", { +        method: "POST", +        body: fd +      }); +      console.log(response); +      //let reader = new FileReader(); + +      //reader.onprogress = e => { +      //  if (e && e.lengthComputable) { +      //    this.fileProgress = parseInt((e.loaded / e.total) * 100, 10); +      //  } +      //}; + +      //reader.onloadend = e => { +      //  this.fileProgress = 100; +      //  this.newProduct.imgPath = file.name; + +      //  let formData = new FormData(); +      //  formData.append(file.name, e.target.result); -      reader.onloadend = e => { -        console.log(e.target.result); -        this.fileProgress = 100; -        this.newProduct.imgPath = file.name; -      }; +      //  fetch("http://localhost:8000/photos", { +      //    method: "POST", +      //    body: formData +      //  }).then(r => { +      //    console.log(r); +      //  }); +      //}; -      reader.readAsDataURL(file); +      //reader.readAsBinaryString(file);      }    }  };  |