Skip to content

Commit

Permalink
Merge pull request #9 from vyzn-tech/Bldrs_647
Browse files Browse the repository at this point in the history
bldrs-ai#647 VYZN: Support for HTTPS data provider
  • Loading branch information
Adrian62D authored Apr 14, 2023
2 parents ea97449 + cc74ba9 commit f044d56
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/ShareRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,59 @@ export default function ShareRoutes({installPrefix, appPrefix}) {
/>
}
/>
<Route
path='v/src/*'
element={
<FetchFromUrl appPrefix={appPrefix}/>
}
/>
</Route>
</Routes>
)
}

/**
* Fetches an IFC file from a remote URL (https only) and then loads it as a local file.
*
* Example:
* A file located at https://example.org/pathto/myfile.ifc can be loaded into Bldrs via
* the following URL http://bldrs.ai/share/v/src/example.org%2Fpathto%2Fmyfile.ifc
*
* @param {string} appPrefix e.g. /share is the prefix for this component.
* @return {(null)}
*/
function FetchFromUrl({appPrefix}) {
const location = useLocation()
const navigate = useNavigate()

useEffect(() => {
const fetchFromUrlAndRedirect = async () => {
const locationParts = location.pathname.split('/')
const srcIndex = locationParts.indexOf('src')
if (srcIndex >= 0) {
const urlIndex = srcIndex + 1
if (urlIndex < locationParts.length) {
const encodedUrl = locationParts.slice(urlIndex).join('/')
const decodedUrl = decodeURIComponent(encodedUrl)
const fullUrl = `https://${decodedUrl}`
const fetchResponse = await fetch(fullUrl)
const blob = await fetchResponse.blob()

let localBlobUrl = URL.createObjectURL(blob)
const parts = localBlobUrl.split('/')
localBlobUrl = parts[parts.length - 1]

window.removeEventListener('beforeunload', handleBeforeUnload)
navigate(`${appPrefix}/v/new/${localBlobUrl}.ifc`)
}
}
}
fetchFromUrlAndRedirect()
})

return (null)
}


/**
* Forward page from /share to /share/v/p per spect at:
Expand Down

0 comments on commit f044d56

Please sign in to comment.