STL load fetch rewrite

This commit is contained in:
Albert Møller Nielsen 2022-09-27 00:35:16 +02:00 • committed by GitHub
commit ab7e806b5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 33 deletions

View file

@ -328,7 +328,7 @@ function platformLoad(url, onload) {
} }
} }
function platformLoadSTL(url, onload, formdata) { function platformLoadSTL(url, onload, formdata, credentials) {
new load.STL().load(url, (vertices, filename) => { new load.STL().load(url, (vertices, filename) => {
if (vertices) { if (vertices) {
let widget = newWidget().loadVertices(vertices); let widget = newWidget().loadVertices(vertices);
@ -338,7 +338,7 @@ function platformLoadSTL(url, onload, formdata) {
onload(vertices, widget); onload(vertices, widget);
} }
} }
}, formdata, 1 / api.view.unit_scale()); }, formdata, 1 / api.view.unit_scale(), credentials);
} }
function platformLoadURL(url, options = {}) { function platformLoadURL(url, options = {}) {

View file

@ -23,39 +23,27 @@ function STL() {
this.colors = null; this.colors = null;
} }
SP.load = function(url, callback, formdata, scale) { SP.load = function(url, callback, formdata, scale, credentials) {
let stl = this, const stl = this;
xhr = new XMLHttpRequest();
function onloaded (event) { fetch(url, {
if (event.target.status === 200 || event.target.status === 0) { method: formdata ? 'POST' : 'GET',
stl.parse(event.target.response || event.target.responseText, scale); credentials: credentials ? credentials : 'same-origin',
let cd = undefined; body: formdata
if (xhr.getAllResponseHeaders().indexOf(CDH) > 0) { }).then(response => {
cd = xhr.getResponseHeader(CDH) if (response.status === 200 || response.status === 0) {
.split(';').map(v => v.trim()).filter(v => { response.arrayBuffer().then(buffer => {
return v.indexOf('filename=') === 0; stl.parse(buffer, scale);
}).map(v => {
return v.substring(10,v.length-1); const cdhVal = response.headers.get(CDH);
})[0]; if (callback) callback(stl.vertices, cdhVal);
} });
if (callback) callback(stl.vertices, cd);
} else { } else {
if (callback) callback(null, event.target.statusText); if (callback) callback(null, response.statusText);
} }
} }).catch(err => {
if (callback) callback(null, err)
xhr.addEventListener('load', onloaded, false); });
xhr.addEventListener('progress', function (event) { }, false);
xhr.addEventListener('error', function () { }, false);
if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain; charset=x-user-defined');
}
xhr.open(formdata ? 'POST' : 'GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send(formdata);
}; };
SP.encode = function(vertices, normals) { SP.encode = function(vertices, normals) {