44 lines
1.9 KiB
JavaScript
44 lines
1.9 KiB
JavaScript
// Single source for the app's mount path. Vite injects import.meta.env.BASE_URL
|
|
// from the build-time `base` (default '/', or e.g. '/ops/' for a subpath IIS
|
|
// mount). Every root-absolute URL to a Flask-served asset or route must go
|
|
// through withBase() so it resolves under the mount instead of the server root.
|
|
export const BASE_URL = import.meta.env.BASE_URL
|
|
|
|
// Prefix a root-absolute path (e.g. '/ge-aerospace-logo.svg', '/api', '/tv')
|
|
// with the mount base. Leaves full URLs (http, data:) untouched.
|
|
export function withBase(path) {
|
|
if (!path) return path
|
|
if (/^([a-z]+:)?\/\//i.test(path) || path.startsWith('data:')) return path
|
|
return BASE_URL + String(path).replace(/^\//, '')
|
|
}
|
|
|
|
// Resolve a stored file/link path for use in an <a href>. Full URLs (http,
|
|
// https, file:, mailto:, ...) and UNC / protocol-relative paths (\\server,
|
|
// //host) open as-is. A path relative to the app (e.g. 'installers/x.exe' or
|
|
// './installers/x.exe') is mounted under the base so it does NOT resolve
|
|
// against the current route - '/shopdb/applications/6' + 'installers/x.exe'
|
|
// wrongly became '/shopdb/applications/installers/x.exe'.
|
|
export function fileHref(path) {
|
|
if (!path) return null
|
|
const trimmed = String(path).trim()
|
|
if (!trimmed) return null
|
|
if (/^[a-z][a-z0-9+.-]*:/i.test(trimmed)
|
|
|| trimmed.startsWith('\\\\')
|
|
|| trimmed.startsWith('//')) {
|
|
return trimmed
|
|
}
|
|
return withBase('/' + trimmed.replace(/^(\.?\/)+/, ''))
|
|
}
|
|
|
|
// Inverse of withBase: turn a browser pathname (which includes the mount
|
|
// base, e.g. '/ops/computers') into a router path ('/computers'). Router
|
|
// navigation already applies the base; feeding it an un-stripped pathname
|
|
// double-prefixes ('/ops/ops/...').
|
|
export function stripBase(path) {
|
|
if (!path) return path
|
|
if (BASE_URL !== '/' && path.startsWith(BASE_URL)) {
|
|
return '/' + path.slice(BASE_URL.length)
|
|
}
|
|
return path
|
|
}
|