implement s3 upload from drag and drop

This commit is contained in:
Johannes Heuel
2023-03-04 20:26:57 +01:00
parent dbceb95154
commit 4709e2a0ab
2 changed files with 55 additions and 12 deletions

View File

@@ -3,7 +3,7 @@ use crate::gallery::Grid;
use crate::hooks::use_user_context;
use gloo_net::http::Request;
use js_sys::Array;
use js_sys::{Array, Promise};
use serde::{Deserialize, Serialize};
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
use web_sys::{
@@ -76,10 +76,6 @@ pub fn home() -> Html {
if let Ok(fse) = d.clone().dyn_into::<File>() {
console_log!(&fse);
files.push(fse.clone());
wasm_bindgen_futures::spawn_local(async move {
let promise = upload_file(fse, "bla".to_owned());
let _result = wasm_bindgen_futures::JsFuture::from(promise).await.unwrap();
});
} else if let Ok(a) = d.clone().dyn_into::<js_sys::Array>() {
for i in 0..a.length() {
let f = a.get(i);
@@ -108,6 +104,7 @@ pub fn home() -> Html {
news.iter().for_each(|x| new_files.push(x.clone()));
fs.iter().for_each(|x| files.push(x.clone()));
}
let uiae: Vec<String> = files
.iter()
.map(|x| {
@@ -119,8 +116,8 @@ pub fn home() -> Html {
})
.collect();
console_log!("end", uiae.join("\n"));
let url = "/api/photos/upload";
let photos: PhotosUrlsWrapper = Request::post(url)
let photos: PhotosUrlsWrapper = Request::post("/api/photos/upload")
.json(&PhotosWrapper { photos: uiae })
.unwrap()
.send()
@@ -130,6 +127,21 @@ pub fn home() -> Html {
.await
.unwrap();
console_log!("{}", serde_json::to_string(&photos).unwrap());
console_log!("", files.len());
let mut promises: Vec<Promise> = Vec::new();
for (file, (_, url)) in files.iter().zip(photos.photos) {
console_log!("uploading: ", &file.name(), &url);
promises.push(upload_file(file.clone(), url.clone()));
}
for promise in promises {
match wasm_bindgen_futures::JsFuture::from(promise).await {
Ok(result) => console_log!(result),
_ => console_log!("errooooor"),
};
}
console_log!("all uploaded");
});
});