Skip to content

Commit

Permalink
cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jun 18, 2024
1 parent 9a3693a commit b16cf98
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
34 changes: 18 additions & 16 deletions crates/eframe/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,42 +247,44 @@ pub fn percent_decode(s: &str) -> String {
.to_string()
}

use js_sys::Uint8Array;
use log::{debug, error};
use std::sync::Arc;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::window;

/// Load the data from a dropped file.
pub async fn get_data(dropped_file: &egui::DroppedFile) -> Result<Arc<[u8]>, String> {
pub async fn get_data(
dropped_file: &egui::DroppedFile,
drop_file: bool,
) -> Result<std::sync::Arc<[u8]>, String> {
use wasm_bindgen_futures::JsFuture;

let url = dropped_file.stream_url.clone().ok_or("No stream URL")?;

let window = window().ok_or("No Window object")?;
let window = web_sys::window().ok_or("No Window object")?;
let fetch = window.fetch_with_str(&url);

let response_value = JsFuture::from(fetch).await.map_err(|err| {
error!("Failed to fetch: {err:?}");
log::error!("Failed to fetch: {err:?}");
format!("Failed to fetch: {err:?}")
})?;

let response: web_sys::Response = response_value.dyn_into().map_err(|err| {
error!("Failed to cast response to web_sys::Response {err:?}");
log::error!("Failed to cast response to web_sys::Response {err:?}");
format!("Failed to cast response to web_sys::Response {err:?}")
})?;

let array_buffer_promise = response.array_buffer().map_err(|err| {
error!("Failed to get array buffer: {err:?}",);
log::error!("Failed to get array buffer: {err:?}",);
format!("Failed to get array buffer: {err:?}",)
})?;

let array_buffer = JsFuture::from(array_buffer_promise).await.map_err(|err| {
error!("Failed to convert to array buffer: {err:?}");
log::error!("Failed to convert to array buffer: {err:?}");
format!("Failed to convert to array buffer: {err:?}")
})?;

let bytes = Uint8Array::new(&array_buffer).to_vec();
debug!("Loaded {} bytes", bytes.len());
let bytes = js_sys::Uint8Array::new(&array_buffer).to_vec();
log::debug!("Loaded {} bytes", bytes.len());

if drop_file {
let _ = web_sys::Url::revoke_object_url(&url);
}

Ok(Arc::from(bytes.into_boxed_slice()))
Ok(std::sync::Arc::from(bytes.into_boxed_slice()))
}
5 changes: 3 additions & 2 deletions crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ impl WrapApp {
egui::Window::new("Dropped files")
.open(&mut open)
.show(ctx, |ui| {
for file in &self.dropped_files {
for file in &mut self.dropped_files {
let mut info = if let Some(path) = &file.path {
path.display().to_string()
} else if !file.name.is_empty() {
Expand All @@ -501,7 +501,7 @@ impl WrapApp {
{
let one_file = file.clone();
let func = async move {
let res = eframe::web::get_data(&one_file);
let res = eframe::web::get_data(&one_file, true);
match res.await {
Ok(data) => {
log::info!("Read {} bytes", data.len());
Expand All @@ -513,6 +513,7 @@ impl WrapApp {
}
};
wasm_bindgen_futures::spawn_local(func);
file.stream_url = None;
}
}
});
Expand Down

0 comments on commit b16cf98

Please sign in to comment.