From c6d09bba459e5065663d59d06e0c5a101708e707 Mon Sep 17 00:00:00 2001
From: netpro2k <netpro2k@gmail.com>
Date: Mon, 9 Jul 2018 17:30:58 -0700
Subject: [PATCH] Fix google poly resolving and relative gltf asset loading

---
 src/components/gltf-model-plus.js |  5 +++++
 src/utils/media-utils.js          | 20 +++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/components/gltf-model-plus.js b/src/components/gltf-model-plus.js
index b11959224..fad11f667 100644
--- a/src/components/gltf-model-plus.js
+++ b/src/components/gltf-model-plus.js
@@ -1,3 +1,4 @@
+import { isFarsparkUrl, decodeFarsparkUrl } from "../utils/media-utils";
 const GLTFCache = {};
 
 AFRAME.GLTFModelPlus = {
@@ -186,6 +187,10 @@ function cachedLoadGLTF(src, preferredTechnique, onProgress) {
   if (!GLTFCache[src]) {
     GLTFCache[src] = new Promise((resolve, reject) => {
       const gltfLoader = new THREE.GLTFLoader();
+      // Farspark urls can't handle relative paths. Use the original url as the base path.
+      if (isFarsparkUrl(src)) {
+        gltfLoader.path = THREE.LoaderUtils.extractUrlBase(decodeFarsparkUrl(src));
+      }
       gltfLoader.preferredTechnique = preferredTechnique;
       gltfLoader.load(src, resolve, onProgress, reject);
     });
diff --git a/src/utils/media-utils.js b/src/utils/media-utils.js
index 9c17813b9..65c13bb01 100644
--- a/src/utils/media-utils.js
+++ b/src/utils/media-utils.js
@@ -1,6 +1,5 @@
 const whitelistedHosts = [/^.*\.reticulum\.io$/, /^.*hubs\.mozilla\.com$/, /^hubs\.local$/];
 const isHostWhitelisted = hostname => !!whitelistedHosts.filter(r => r.test(hostname)).length;
-
 let resolveMediaUrl = "/api/v1/media";
 if (process.env.NODE_ENV === "development") {
   resolveMediaUrl = `https://${process.env.DEV_RETICULUM_SERVER}${resolveMediaUrl}`;
@@ -18,9 +17,24 @@ export const resolveFarsparkUrl = async url => {
   }).then(r => r.json())).raw;
 };
 
-const fetchContentType = async url => fetch(url, { method: "HEAD" }).then(r => r.headers.get("content-type"));
-let interactableId = 0;
+const farsparkRegex = /^farspark.*\.reticulum\.io$/;
+export const isFarsparkUrl = url => farsparkRegex.test(new URL(url).hostname);
+export const decodeFarsparkUrl = url => {
+  const parts = url.split("/");
+  return atob(parts[parts.length - 1]);
+};
 
+const staticContentMappings = {
+  "poly.googleapis.com": "model/gltf"
+};
+const fetchContentType = async url => {
+  const staticContentType = staticContentMappings[new URL(decodeFarsparkUrl(url)).hostname];
+  return staticContentType
+    ? Promise.resolve(staticContentType)
+    : fetch(url, { method: "HEAD" }).then(r => r.headers.get("content-type"));
+};
+
+let interactableId = 0;
 const offset = { x: 0, y: 0, z: -1.5 };
 export const spawnNetworkedImage = (src, contentType) => {
   const scene = AFRAME.scenes[0];
-- 
GitLab