Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webassembly/app.html: Fetch mesh from search-query url-param #1596

Merged
merged 12 commits into from
Sep 4, 2024
45 changes: 43 additions & 2 deletions webassembly/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ <h1 class="title">F3D Web</h1>
openFile(document.getElementById('file-name').innerHTML);
});

// load the file located in the virtual filesystem
openFile('f3d.vtp');

// setup the window size based on the canvas size
const main = document.getElementById('main');
Expand All @@ -223,6 +221,49 @@ <h1 class="title">F3D Web</h1>
// do a first render and start the interactor
Module.engineInstance.getWindow().render();
Module.engineInstance.getInteractor().start();

mwestphal marked this conversation as resolved.
Show resolved Hide resolved
// Parse search-query mesh url-param or load default mesh file
// const params = new URLSearchParams(window.location.search);
// Replace first hash with question mark to have real search query parsing and avoid leading # in first parsed urlparam
const params = new URLSearchParams(window.location.hash.replace('#', '?'));
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
const mesh_url_passed = params.get("mesh");
const extension_parsed = params.get("extension");
if (mesh_url_passed) {
const mesh_url = decodeURI(mesh_url_passed);
fetch(mesh_url)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
}
// Build filename given extension urlparam or response header content-disposition
let filename;
if (extension_parsed) {
filename = `mesh_urlparam.${extension_parsed}`;
} else {
// If extension is not provided by user, try to get it auto from content-disposition header of url extension
const contentDisposition = response.headers.get('content-disposition');
if (contentDisposition) {
filename = contentDisposition.split('filename=')[1].split(';')[0];
} else {
try {
filename = mesh_url.split('/').pop();
const url_extension = '.' + mesh_url.split('.').at(-1) ;
} catch {
throw new Error(`Could not infer file extension from content-disposition or url extension. Please enforce it via the extension urlparam`);
}
}
}
// Convert buffer to Uint8Array and openFile
response.arrayBuffer().then((buffer) => {
const uint8array = new Uint8Array(buffer);
Module.FS.writeFile(filename, uint8array);
openFile(filename);
});
})
} else {
// load the file located in the virtual filesystem
openFile('f3d.vtp');
}
})
.catch(error => console.error("Internal exception: " + error));
</script>
Expand Down