diff --git a/webassembly/app.html b/webassembly/app.html index 9b5c6dad6d..7deb4e5d68 100644 --- a/webassembly/app.html +++ b/webassembly/app.html @@ -213,8 +213,6 @@

F3D Web

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'); @@ -224,6 +222,49 @@

F3D Web

// do a first render and start the interactor Module.engineInstance.getWindow().render(); Module.engineInstance.getInteractor().start(); + + // Parse model from url-param search query hash via model url and extension + 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 (contentDisposition) { + // If extension is not provided by user, try to get it auto from content-disposition header of url extension + return contentDisposition.split('filename=')[1].split(';')[0]; + } else { + return model_url.split('/').pop(); + } + throw new Error(`Could not parse filename/extension from either urlparam extension, response header content-disposition, nor filename present in url`); + } + function load_from_url(){ + // Parse search-query model url-param or load default model 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("model"); + 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));