Newer
Older
navi-1 / webclient / src / utils / contentLinks.js
const DEV_API_PORT = '8000'

export function contentFileUrl(url) {
  if (!url) return ''

  let parsed
  try {
    parsed = new URL(url, window.location.origin)
  } catch {
    return url
  }

  if (
    import.meta.env.DEV &&
    parsed.port === DEV_API_PORT &&
    ['localhost', '127.0.0.1'].includes(parsed.hostname)
  ) {
    const path = parsed.pathname.startsWith('/api/') ? parsed.pathname : `/api${parsed.pathname}`
    return `${path}${parsed.search}${parsed.hash}`
  }

  if (parsed.origin === window.location.origin) {
    return `${parsed.pathname}${parsed.search}${parsed.hash}`
  }

  return parsed.href
}

export function absoluteContentUrl(url) {
  if (!url) return ''
  return new URL(url, window.location.origin).href
}

export function versionedContentUrl(url, version) {
  const normalized = contentFileUrl(url)
  if (!normalized || !version) return normalized
  return `${normalized}${normalized.includes('?') ? '&' : '?'}v=${encodeURIComponent(version)}`
}

export function viewerContentUrl(viewerType, fileUrl) {
  const viewer = new URL(`/content-viewers/${viewerType}.html`, window.location.origin)
  viewer.searchParams.set('url', fileUrl)
  return viewer.href
}