UNPKG

@thebigcrunch/sdk

Version:
111 lines (93 loc) 3.51 kB
import { getConfig } from "./config"; const isS3AssetEnabled = () => getConfig().assetProvider === "s3"; /** * if s3 asset is enabled, it will return s3 domain name otherwise return cdn domain */ const getCdnUrlPrefix = () => { if (isS3AssetEnabled()) { return `${getConfig().s3Domain}/`; } const protocol = getConfig().nginxMainPort === "443" ? "https" : "http"; return `${protocol}://${getConfig().cdnDomain}:${ getConfig().nginxMainPort }/`; }; /** * this method is used to make sure assets path is compitable between local and s3 * local nginx path is using `cell_images` while s3 path is `images/cell` * @param {*} url */ const convertUrlToPath = url => { if (isS3AssetEnabled()) { const items = url.split("_"); if (items.length > 1) { return `${items[1]}/${items[0]}s/`; } } return `${url}/`; }; const getImageUrlPrefix = () => `${getCdnUrlPrefix()}${convertUrlToPath(getConfig().imageUrl)}`; const getVideoUrlPrefix = () => `${getCdnUrlPrefix()}${convertUrlToPath(getConfig().videoUrl)}`; const getAudioUrlPrefix = () => `${getCdnUrlPrefix()}${convertUrlToPath(getConfig().audioUrl)}`; const removeSlash = space => { if (space && space.value.charAt(0) === "/") { return space.value.substring(1); } return space.value; }; export const getSpaceImageUrl = function getSpaceImageUrl(space) { // hack hack hack for CSIRO TODO FIX FLASH 20/04/2018 if (space.value.indexOf("://") !== -1) { return space.value; } return `${getImageUrlPrefix()}${removeSlash(space)}`; }; export const getSpaceVideoUrl = function getSpaceVideoUrl(space) { return `${getVideoUrlPrefix()}${removeSlash(space)}`; }; export const getSpaceAudioUrl = function getSpaceAudioUrl(space) { return `${getAudioUrlPrefix()}${removeSlash(space)}`; }; export const getAppImageUrl = function getAppImageUrl(appname) { return `${getCdnUrlPrefix()}${ getConfig().appStoreImagesUrl }/${appname}.png`; }; const getWebServerUrl = function getWebServerUrl() { let port = ""; if (!getConfig().isS3AssetEnabled) { port = `:${getConfig().nginxMainPort}`; } else if (window.location.port !== "") { port = `:${window.location.port}`; } return `${getConfig().sdkConnectionUrl}${port}`; }; export const getAppStoreEndPoint = function getAppStoreEndPoint() { return `${getWebServerUrl()}/${getConfig().appStoreEndPoint}`; }; export const getSpaceVizzyUrl = function getSpaceVizzyUrl(spaceId) { return `${getWebServerUrl()}/${getConfig().vizziesUrl}/${spaceId}`; }; export const getSpaceVizzySourceUrl = function getSpaceVizzySourceUrl(spaceId) { return `${getWebServerUrl()}/${getConfig().vizziesUrl}/source/${spaceId}`; }; export const getWebViewLink = function getWebViewLink(space) { return `${getConfig().webViewUrl}/v/${space.encodedUuid}`; }; export const getProfilePictureUrl = function getProfilePictureUrl(user) { // Hard coded, should be configurable later. return `${getCdnUrlPrefix()}profilepictures/users/${user.profilePicture}`; }; // Sometimes users don't have usernames. This should be! So in the mean time // lets make it handle both cases. export const getUserProfileUrl = function getUserProfileUrl(user) { let url = getConfig().webViewUrl; if (!user) return url; else return user.username ? `${url}/u/${user.username}` : `${url}/u/id/${user.id}`; };