Skip to content

Commit

Permalink
Store fetched content in named tempfile
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Oct 5, 2023
1 parent fec2f4d commit 8de4546
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
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
20 changes: 7 additions & 13 deletions src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use tracing::*;
#[cfg(not(target_family = "wasm"))]
fn load_mesh_assimp(
file_string: &str,
data: &str,
scale: na::Vector3<f32>,
opt_urdf_color: &Option<na::Point3<f32>>,
group: &mut SceneNode,
Expand All @@ -24,7 +23,7 @@ fn load_mesh_assimp(
importer.pre_transform_vertices(|x| x.enable = true);
importer.collada_ignore_up_direction(true);
let (meshes, textures, colors) =
convert_assimp_scene_to_kiss3d_mesh(&importer.read_string(data)?);
convert_assimp_scene_to_kiss3d_mesh(&importer.read_file(file_string)?);
info!(
"num mesh, texture, colors = {} {} {}",
meshes.len(),
Expand Down Expand Up @@ -105,17 +104,12 @@ pub fn load_mesh(
#[cfg(feature = "assimp")]
if use_assimp {
let is_url = file_string.starts_with("https://") || file_string.starts_with("http://");
let buf = if is_url {
crate::utils::fetch_read(file_string)?
} else {
use std::io::Read;
let mut file = std::fs::File::open(file_string)?;
let mut buf = vec![];
file.read_to_end(&mut buf)?;
buf
};
let data = String::from_utf8(buf)?;
return load_mesh_assimp(file_string, &data, scale, opt_color, group, use_texture);
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);
}

let filename = Path::new(file_string);
Expand Down
10 changes: 6 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ mod native {
}

#[cfg(feature = "assimp")]
/// http request -> return content
pub fn fetch_read(url: &str) -> Result<Vec<u8>> {
use std::io::Read;
/// 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;

Expand All @@ -123,7 +123,9 @@ mod native {
if buf.len() > RESPONSE_SIZE_LIMIT {
return Err(crate::Error::Other(format!("{url} is too big")));
}
Ok(buf)
let mut file = tempfile::NamedTempFile::new()?;
file.write_all(&buf)?;
Ok(file)
}
}

Expand Down

0 comments on commit 8de4546

Please sign in to comment.