Skip to content

Commit

Permalink
H1rono/assimp fetch (#222)
Browse files Browse the repository at this point in the history
* Support URL with assimp enabled

* Store fetched content in named tempfile

* Update CSpell dictionary
  • Loading branch information
H1rono authored Oct 5, 2023
1 parent 566a8bb commit 453928f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/.cspell/rust-dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ assimp
bindgen
getrandom
structopt
tempfile
thiserror
urdf
ureq
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = ["examples/wasm"]

[features]
default = ["assimp"]
assimp = ["assimp-crate", "assimp-sys"]
assimp = ["assimp-crate", "assimp-sys", "tempfile"]

# Note: k, kiss3d, serde, structopt, tokio, urdf-rs, and wasm-bindgen are public dependencies.
[dependencies]
Expand All @@ -33,6 +33,7 @@ urdf-rs = "0.7.2"

assimp-crate = { package = "assimp", version = "0.3.1", optional = true }
assimp-sys = { version = "0.3.1", optional = true }
tempfile = { version = "3.8", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
axum = "0.6"
Expand Down
8 changes: 7 additions & 1 deletion src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ pub fn load_mesh(
let file_string = filename.as_ref();

#[cfg(feature = "assimp")]
if use_assimp && !file_string.starts_with("https://") && !file_string.starts_with("http://") {
if use_assimp {
let is_url = file_string.starts_with("https://") || file_string.starts_with("http://");
if is_url {
let file = crate::utils::fetch_tempfile(file_string)?;
let path = file.path().to_str().unwrap();
return load_mesh_assimp(path, scale, opt_color, group, use_texture);
}
return load_mesh_assimp(file_string, scale, opt_color, group, use_texture);
}

Expand Down
22 changes: 22 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ mod native {
mem::take(&mut self.package_path)
}
}

#[cfg(feature = "assimp")]
/// http request -> write to tempfile -> return that file
pub(crate) fn fetch_tempfile(url: &str) -> Result<tempfile::NamedTempFile> {
use std::io::{Read, Write};

const RESPONSE_SIZE_LIMIT: usize = 10 * 1_024 * 1_024;

let mut buf: Vec<u8> = vec![];
ureq::get(url)
.call()
.map_err(|e| crate::Error::Other(e.to_string()))?
.into_reader()
.take((RESPONSE_SIZE_LIMIT + 1) as u64)
.read_to_end(&mut buf)?;
if buf.len() > RESPONSE_SIZE_LIMIT {
return Err(crate::Error::Other(format!("{url} is too big")));
}
let mut file = tempfile::NamedTempFile::new()?;
file.write_all(&buf)?;
Ok(file)
}
}

#[cfg(target_family = "wasm")]
Expand Down

0 comments on commit 453928f

Please sign in to comment.