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
47 changes: 45 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,51 @@ <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 mesh from url-param search query hash
function filename_for_model_url(model_url, extension_parsed, contentDisposition) {
// Build filename given extension urlparam or response header content-disposition
if (extension_parsed) {
return `model_urlparam.${extension_parsed}`;
} else {
// If extension is not provided by user, try to get it auto from content-disposition header of url extension
if (contentDisposition) {
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
return contentDisposition.split('filename=')[1].split(';')[0];
} else {
return model_url.split('/').pop();
}
}
return filename;
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
}
function load_from_url(){
// 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(/^#/, '?'));
const model_url_passed = params.get("mesh");
jo-chemla marked this conversation as resolved.
Show resolved Hide resolved
const extension_parsed = params.get("extension");
if (model_url_passed) {
const model_url = decodeURI(model_url_passed);
fetch(model_url)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
}
const contentDisposition = response.headers.get('content-disposition');
const filename = filename_for_model_url(model_url, extension_parsed, contentDisposition);
// Convert buffer to Uint8Array and openFile
response.arrayBuffer().then((buffer) => {
Module.FS.writeFile(filename, new Uint8Array(buffer));
openFile(filename);
});
})
} else {
// load the file located in the virtual filesystem
openFile('f3d.vtp');
}
}
addEventListener("hashchange", (event) => {load_from_url()});
load_from_url();
})
.catch(error => console.error("Internal exception: " + error));
</script>
Expand Down